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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stanley |
last post by:
has anyone managed to use cache application block in a web application?
thanks
stanley
|
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...
|
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...
|
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,...
|
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...
| |
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?). ...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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 ...
| |