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
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