Allen Chen said:
Quote from Trapulo =================================================
- to save on db may I use a standard binaryformatter to serialize objects
to
a sql image field?
=================================================
I suggest you use varbinary(max). Please refer to the following
documentation for the reason:
http://msdn.microsoft.com/en-us/library/ms187993.aspx
mmm I'm still using the old SQL 2000 and varbinary can contain only 8K. My
cached data can be bigger...
Quote from Trapulo =================================================
- do you think that can be a good idea to clean expired objects on
Application_End event?
=================================================
I think we don't have to clean the expired objects. ASP.NET cache has
already done this for us. We can just set the expiration time when adding
new cache items.
Yes: but items in SQL Server?
Quote from Trapulo =================================================
I have this error:
Run-time exception thrown :
System.Reflection.TargetParameterCountException - Parameter count
mismatch.
=================================================
I'm not sure why this exception will be thrown here. I cannot reproduce it
on my side. Could you provide your code so that I can have some test?
BTW, my code is pseudocode. Please use HttpRuntime.Cache to initialize the
_cache.
Ah
I did think that you want use a separate in-memory variable
However, this is my draft. I have errors every time I try to access _cache.
I added a boolean that disable in-memory cache only to go on with
development.
Imports System.Web
Public Class WcfCache
Shared Sub New()
If EnableInMemoryCache Then _cache = HttpContext.Current.Cache
End Sub
''' <summary>
''' Maximum objects that in-memory can store
''' </summary>
''' <remarks></remarks>
Private Const cMemoryMaxObjects As Int32 = 20
' ** disabilito la cache in memory perchè dà un sacco di errori
assurdi....
Private Shared _enableInMemoryCache As Boolean = True
Public Shared Property EnableInMemoryCache() As Boolean
Get
Return _enableInMemoryCache
End Get
Set(ByVal value As Boolean)
_enableInMemoryCache = value
End Set
End Property
Private Shared _cache As Caching.Cache ' = HttpContext.Current.Cache
Private Shared Function GetDbConnection() As System.Data.IDbConnection
Return
Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("Cache").CreateConnection()
End Function
Public Shared Sub AddCache(ByVal key As String, ByVal data As Object,
ByVal absoluteExpiration As Date, ByVal cacheItemPriority As
Caching.CacheItemPriority)
If EnableInMemoryCache Then
If (_cache.Count > cMemoryMaxObjects) Then
AddToDbCache(key, data, absoluteExpiration)
Else
_cache.Add(key, data, Nothing, absoluteExpiration,
Caching.Cache.NoSlidingExpiration, cacheItemPriority, Nothing)
AddToDbCache(key, data, absoluteExpiration)
End If
Else
AddToDbCache(key, data, absoluteExpiration)
End If
End Sub
Private Shared Sub AddToDbCache(ByVal key As String, ByVal data As
Object, ByVal absoluteExpiration As Date)
Dim dal As New DAL.WcfCache.WcfCacheDataContext(GetDbConnection)
Dim newItem As New BusinessEntities.WcfCache.WcfCache
Dim c As BusinessEntities.WcfCache.WcfCache = (From item As
BusinessEntities.WcfCache.WcfCache In dal.WcfCaches _
Where item.Key = key).FirstOrDefault
If c IsNot Nothing Then
dal.WcfCaches.DeleteOnSubmit(c)
dal.SubmitChanges()
End If
Dim serizer As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Using ms As New System.IO.MemoryStream()
serizer.Serialize(ms, data)
Dim Result(CInt(ms.Length - 1)) As Byte
ms.Position = 0
ms.Read(Result, 0, CInt(ms.Length))
newItem.Value = New Data.Linq.Binary(Result)
End Using
newItem.Key = key
newItem.Expiration = absoluteExpiration
dal.WcfCaches.InsertOnSubmit(newItem)
dal.SubmitChanges()
End Sub
Public Shared Function GetCache(ByVal key As String) As Object
If EnableInMemoryCache Then
If _cache.Item(key) IsNot Nothing Then
Return _cache.Item(key)
End If
End If
Dim dal As New DAL.WcfCache.WcfCacheDataContext(GetDbConnection)
Dim c As BusinessEntities.WcfCache.WcfCache = (From item As
BusinessEntities.WcfCache.WcfCache In dal.WcfCaches _
Where item.Key = key AndAlso item.Expiration >=
Now).FirstOrDefault
If c IsNot Nothing Then
Using ms As New System.IO.MemoryStream(c.Value.ToArray)
Dim serizer As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
GetCache = serizer.Deserialize(ms)
End Using
Else
Return Nothing
End If
End Function
End Class