ASP.Net Cache Callback Problem

J

Joe Fallon

I am trying to setup a cache that refreshes itself every hour. (My sample
code is for every minute so I can test it.)
I have found some examples that I thought worked but they all seem to fail.
(They only work when I click the submit button - not when the cache
expires.)

In my Login form I have:
Global.AddItemToCache("CacheTime", Date.Now, 60)


This fills the cache initially.
Whenever I click a refresh button the cached value is displayed. When the
cache expires and I click Refresh, I get a new value.

BUT - if I just wait (about a minute or two) and the cache expires by itself
and I set a breakpoint inside RefreshCache sub then I get a
NullReferenceException error and the cache is not re-loaded.
I am trying this with a simple Date object but I really want to do it with a
complex Business Object. But I get the same error.

Has anyone successfully used cache expiration??
I appreciate any feedback.
Thanks!

============================================================================
=======================

Note: Google research found this which I hoped would resolve the problem.
But it does not change things.
'http://urbanasylum.dynu.com/JustTheFacts/archives/2003_07.html

'When You Don't Have HttpContext To Stand On

'System.Web.HttpContext.Current.Cache()

'This is a very subtle problem which caused me a lot of pain!!!

'HttpContext is ONLY available when servicing an incoming request!

'The expiration policy only occurs on a background thread so *there is no
incoming request*.

'Touching the cache property through Context.Cache inside global.asax is
going to throw a NullReferenceException and bite you!

'The appropriate way to hit the Cache is to use the static Cache property of
the HttpRuntime class. (That is what IL code uses!)

'MS bug - the same property is used in multiple classes but only one of them
works!

============================================================================
=======================

I configured global.asax to look like this:

Imports System.Web.Caching

Imports System.Web.HttpRuntime



Public Shared onRemove As CacheItemRemovedCallback



Public Shared Sub RefreshCache(ByVal Key As String, ByVal Value As Object,
ByVal Reason As CacheItemRemovedReason)

Select Case Key

Case "CacheTime"

AddItemToCache("CacheTime", Date.Now, 60)

Case Else

End Select

End Sub



Public Shared Sub AddItemToCache(ByVal Key As String, ByVal Value As Object,
ByVal NumMinutes As Integer)

onRemove = New CacheItemRemovedCallback(AddressOf RefreshCache)

Try

If IsNothing(HttpRuntime.Cache(Key)) Then

HttpRuntime.Cache.Insert(Key, Value, Nothing,
DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High,
onRemove)

End If

Catch ex As Exception

ex.ToString()

End Try

End Sub

============================================================================
=======================
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top