B
Brian Vallelunga
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 callback method
specified.
3) The callback method gets the newest version of the item from the db and
then calls a helper method to put the item back into cache. It looks like
this:
//Pseudocode
// Called once by app
Initialize()
{
Data myData = DataTier.GetData();
AddDataToCache("key", myData);
}
// Puts item into cache with 10 minute expiration AddDataToCache(string key,
object data) {
// Delegate for expiring callback
CacheItemRemovedCallback onRemove;
onRemove = new CacheItemRemovedCallback(RemovedCallback);
// Add data to the cache
HttpContext.Current.Cache.Add(key, data, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero, CacheItemPriority.High, onRemove);
}
// Called when item expires
RemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
Data myData = DataTier.GetData();
AddDataToCache(key, myData);
// Any code here won't be called!
}
That's basically the scenario. Ideally, I would initialize the system once,
and then it would automatically refresh the data within it every time the
cache expired.
When debugging though, after the cache times out, the item is not put back
in. It is very strange. RemovedCallback is called, but if I put a trace
statement after the AddDataToCache method call, the trace is never written.
Does anyone have an idea as to what is happening?
Thanks,
Brian
have:
1) I put something in cache and set its callback method.
2) The object times out after X minutes and calls the callback method
specified.
3) The callback method gets the newest version of the item from the db and
then calls a helper method to put the item back into cache. It looks like
this:
//Pseudocode
// Called once by app
Initialize()
{
Data myData = DataTier.GetData();
AddDataToCache("key", myData);
}
// Puts item into cache with 10 minute expiration AddDataToCache(string key,
object data) {
// Delegate for expiring callback
CacheItemRemovedCallback onRemove;
onRemove = new CacheItemRemovedCallback(RemovedCallback);
// Add data to the cache
HttpContext.Current.Cache.Add(key, data, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero, CacheItemPriority.High, onRemove);
}
// Called when item expires
RemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
Data myData = DataTier.GetData();
AddDataToCache(key, myData);
// Any code here won't be called!
}
That's basically the scenario. Ideally, I would initialize the system once,
and then it would automatically refresh the data within it every time the
cache expired.
When debugging though, after the cache times out, the item is not put back
in. It is very strange. RemovedCallback is called, but if I put a trace
statement after the AddDataToCache method call, the trace is never written.
Does anyone have an idea as to what is happening?
Thanks,
Brian