473,473 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ASP.Net Cache Callback Problem

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

================================================== ==========================
=======================
--
Joe Fallon


Nov 18 '05 #1
1 2491
Well, for some strange reason it seems to be working now.
Oh well.
--
Joe Fallon

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
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

================================================== ========================== =======================
--
Joe Fallon

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stanley | last post by:
has anyone managed to use cache application block in a web application? thanks stanley
3
by: Brian Vallelunga | last post by:
I'm having a problem using the Cache object with asp.net. Here is what I have: 1) I put something in cache and set its callback method. 2) The object times out after X minutes and calls the...
1
by: Glenn | last post by:
Hi, I have a config XML file that I am using from the application cache. I have configured the entry with a remove callback to re-populate cache automatically when the XML file changes. All is...
4
by: NWx | last post by:
Hi, I' trying to implement a callback method when a cache object expires I want to do this to automatically logout user after a timeout (for demo purposes) My thought is, when user logon,...
0
by: Chris Snyder | last post by:
I have a page that displays real-time data (road sensors updated every 5 minutes), but the query to display the data takes 15-20 seconds (big table in a DB w/o indexes, not my fault). This is...
1
by: William Sullivan | last post by:
I'm trying to nail down some issues with the cache in my application. Currently, I have an object that stands between my business logic and database logic called CacheLogic (cute, no?). ...
2
by: SCG | last post by:
Hi, I have had a site running for 6 months now. This morning I started observing the following behaviour pretty much out of the blue: I load a blob of XML into the ASP.NET cache with a timeout...
4
by: SCG | last post by:
Hi, I have had a site running for 6 months now. This morning I started observing the following behaviour pretty much out of the blue: I load a blob of XML into the ASP.NET cache with a...
1
by: an | last post by:
I've implemented a call-back function to repopulate the stored Cache objects when they expires or are removed it works great but in one situation only it doesen't work (that is the callback...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.