Apologies for the long post, this is driving me up the wall, I'm sure it's something pretty simple but can't put my finger on it and can't find any help on the web.
I'm trying to implement a Callback for an item I put into the Cache. I have tried the following:
1] Putting the code into Global.asax like as per these examples:
http://www.eggheadcafe.com/articles/20030418.asp
http://www.eggheadcafe.com/articles/20030416.asp
2] Putting the above code into a web page to see whether it will work
3] Creating a seperate object (see code below) and creating and using that in my web page Page_Load method:
public class mcCacheExpire
{
//
// This holds a reference to the method to call back
//
private CacheItemRemovedCallback OnRemove = null;
public mcCacheExpire()
{
OnRemove += new CacheItemRemovedCallback( CacheDumped );
}
public void AddCacheItem( int i, int iMinutes )
{
// Add to the cache with key value "A"
System.Web.HttpRuntime.Cache.Insert( "A", i, null,
// DateTime.Now.AddMinutes( iMinutes ), System.Web.Caching.Cache.NoSlidingExpiration,
DateTime.Now.AddMinutes( 10 ), TimeSpan.Zero,
CacheItemPriority.Normal, OnRemove );
//
// Debug
//
easier.writeToFile( "Session_End.txt", "ADDED A CACHE ITEM FOR: " + i + " at: " );
}
public void CacheDumped(string k, object v, CacheItemRemovedReason r)
{
easier.writeToFile( "Session_End.txt", "DUMPED THE FOLLOWING CACHE ITEM: " + k + ", FOR OBJECT: " + v.ToString() + ", FOR THE FOLLOWING REASON: " + r.ToString() );
}
}
The above code was amending from this site:
http://aspalliance.com/69
Basically the Callback is never called (actually it worked for a couple of times) when timing out. If I call Cache.Remove ( cachekey ) the Callback is called.
So basically why wouldn't the Callback be called? Is there a server setting or web.config setting that has to be set when using the Cache to store data? Is the object going out of scope? If so, how should I implement so it doesn't go out of scope, etc?
I asked my admin about this and they said:
"The application should be ok on your site as it is isolated from all other apps on the server. Do you think there may be other settings in the app pool which should help? Most of the time they are left as default with the settings, so I would imagine it would be ok as it is." - any thoughts?
I can add more code if necessary.
Also my Session_End function in Global.asax is never called even though Session_Start is - why would this be? My web.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true"/>
<sessionState mode="InProc"
cookieless="false"
timeout="20"/>
</system.web>
</configuration>
Thanks for your help,
Titch