System.Web.Cache Question

S

Scott Meddows

I need to cache a large collection of object (that expires and can reload) in a webservice for performance reason. I want the data
to presist over different users so I can't use the context.cache object provided. So, I created a new instance of the cache object
myself.

However, I am having problems assigning it data. I receive a NullReferenceException on the add method. Has anyone ever run across
this before? I have put all the variables in the watch window and they all resolve to valid values....

Thanks.

PS - I Have this in my global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
'Fires when the application is started
UpdateDocCache()

UpdateFacilityCache()

End Sub

--------------- Module code ---------------------

Imports System.Configuration.ConfigurationSettings
Imports System.Web.Caching

Module GlobalModule
Public acache As System.Web.Caching.Cache = New System.Web.Caching.Cache

Public Sub FacilityCacheExpired(ByVal key As String, ByVal value As Object, ByVal reason As
System.Web.Caching.CacheItemRemovedReason)
UpdateFacilityCache()
End Sub

Public Sub DocCacheExpired(ByVal key As String, ByVal value As Object, ByVal reason As
System.Web.Caching.CacheItemRemovedReason)
UpdateDocCache()
End Sub

Public Sub UpdateFacilityCache()
Try
If IsNothing(acache.Item("AllFacilities")) Then
acache.Add("Test", 1, New Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration, TimeSpan.MaxValue,
CacheItemPriority.Default, AddressOf DocCacheExpired)
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Try
acache.Add("AllFacilities", FacilityData.Load, New Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(CInt(AppSettings.Get("FacilityCacheRefreshHours")), CInt(AppSettings.Get("FacilityCacheRefreshMinutes")),
CInt(AppSettings.Get("FacilityCacheRefreshSeconds"))), Caching.CacheItemPriority.Default, AddressOf FacilityCacheExpired)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
' If there is a problem in the web.config file entries then default load the cache.
acache.Add("AllFacilities", FacilityData.Load, New Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(0, 5, 30), Caching.CacheItemPriority.Default, AddressOf FacilityCacheExpired)
End Try
End Sub

Public Sub UpdateDocCache()
Try
If acache.Item("AllFacilities") Is Nothing Then
acache.Add("Test", 1, New Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration, TimeSpan.MaxValue,
CacheItemPriority.Default, AddressOf DocCacheExpired)
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Try
acache.Add("AllDocs", DoctorData.LoadAllDocs, New Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(CInt(AppSettings.Get("DocCacheRefreshHours")), CInt(AppSettings.Get("DocCacheRefreshMinutes")),
CInt(AppSettings.Get("DocCacheRefreshSeconds"))), Caching.CacheItemPriority.Default, AddressOf DocCacheExpired)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
' If there is a problem in the web.config file entries then default load the cache.
acache.Add("AllDocs", DoctorData.LoadAllDocs, New Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(0, 5, 0), Caching.CacheItemPriority.Default, AddressOf DocCacheExpired)
End Try
End Sub
End Module
 
R

Raghu

I tried to do the same thing. It appears that whether you create your own
cache object or use the one provided by context, the internal cache may be
singleton. The null exception may be related to context.

Just curious, why can not you use context cache when multiple users are
involved?

Scott Meddows said:
I need to cache a large collection of object (that expires and can reload)
in a webservice for performance reason. I want the data
to presist over different users so I can't use the context.cache object
provided. So, I created a new instance of the cache object
myself.

However, I am having problems assigning it data. I receive a
NullReferenceException on the add method. Has anyone ever run across
this before? I have put all the variables in the watch window and they
all resolve to valid values....
Thanks.

PS - I Have this in my global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
'Fires when the application is started
UpdateDocCache()

UpdateFacilityCache()

End Sub

--------------- Module code ---------------------

Imports System.Configuration.ConfigurationSettings
Imports System.Web.Caching

Module GlobalModule
Public acache As System.Web.Caching.Cache = New System.Web.Caching.Cache

Public Sub FacilityCacheExpired(ByVal key As String, ByVal value As Object, ByVal reason As
System.Web.Caching.CacheItemRemovedReason)
UpdateFacilityCache()
End Sub

Public Sub DocCacheExpired(ByVal key As String, ByVal value As Object, ByVal reason As
System.Web.Caching.CacheItemRemovedReason)
UpdateDocCache()
End Sub

Public Sub UpdateFacilityCache()
Try
If IsNothing(acache.Item("AllFacilities")) Then
acache.Add("Test", 1, New
Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
TimeSpan.MaxValue,
CacheItemPriority.Default, AddressOf DocCacheExpired)
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Try
acache.Add("AllFacilities", FacilityData.Load, New
Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(CInt(AppSettings.Get("FacilityCacheRefreshHours")), CInt(AppSettings.Get("FacilityCacheRefreshMinutes")),
CInt(AppSettings.Get("FacilityCacheRefreshSeconds"))),
Caching.CacheItemPriority.Default, AddressOf FacilityCacheExpired)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
' If there is a problem in the web.config file entries then default load the cache.
acache.Add("AllFacilities", FacilityData.Load, New
Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(0, 5, 30), Caching.CacheItemPriority.Default, AddressOf FacilityCacheExpired)
End Try
End Sub

Public Sub UpdateDocCache()
Try
If acache.Item("AllFacilities") Is Nothing Then
acache.Add("Test", 1, New
Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
TimeSpan.MaxValue,
CacheItemPriority.Default, AddressOf DocCacheExpired)
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Try
acache.Add("AllDocs", DoctorData.LoadAllDocs, New
Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
New TimeSpan(CInt(AppSettings.Get("DocCacheRefreshHours")), CInt(AppSettings.Get("DocCacheRefreshMinutes")),
CInt(AppSettings.Get("DocCacheRefreshSeconds"))),
Caching.CacheItemPriority.Default, AddressOf DocCacheExpired)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
' If there is a problem in the web.config file entries then default load the cache.
acache.Add("AllDocs", DoctorData.LoadAllDocs, New
Caching.CacheDependency("C:\Blah.txt"), Cache.NoAbsoluteExpiration,
 
S

Scott Meddows

I thought it would just use the cache in that one http request. I guess I'm not too clear on the definition of "context".
 
S

Scott Meddows

Okay, I used the context object but my callback in my webservice doesn't have a context object associated with it.

Is there a way to refresh the cache or to create an instance of my own somehow?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top