4

I want to be able to maintain certain objects between application restarts.

To do that, I want to write specific cached items out to disk in Global.asax Application_End() function and re-load them back on Application_Start().

I currently have a cache helper class, which uses the following method to return the cached value:

return HttpContext.Current.Cache[key];

Problem: during Application_End(), HttpContext.Current is null since there is no web request (it's an automated cleanup procedure) - therefore, I cannot access .Cache[] to retrieve any of the items to save to disk.

Question: how can I access the cache items during Application_End()?

Carl J.
  • 343
  • 1
  • 3
  • 8

4 Answers4

3

If you want to get access to cache object before it will be disposed, you need to use somethink like this to add object to cache:

Import namespace System.Web.Caching to your application where you are using adding objects to cache.

//Add callback method to delegate
var onRemove = new CacheItemRemovedCallback(RemovedCallback);

//Insert object to cache
HttpContext.Current.Cache.Insert("YourKey", YourValue, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, onRemove);

And when this object is going to be disposed will be called following method:

private void RemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
    //Use your logic here

    //After this method cache object will be disposed
}
Dima Shmidt
  • 895
  • 4
  • 13
1

I strongly urge you to rethink your approach. You may want to describe specifics of what are you trying to do, so we might help you with that. But if you are totally set on it, then you can simply save values on disk when you actually set them, i.e. your helper class would looks something like this:

public static class CacheHelper
{
    public static void SetCache(string key, object value)
    {
        HttpContext.Current.Cache[key] = value;
        if (key == "some special key")
            WriteValueOnDisk(value);
    }
}
Ilia G
  • 10,043
  • 2
  • 40
  • 59
  • The application does a ton of "dirty" writing/updating (site health, telemetry, etc), etc - and updates are happening too often to write them to disk all the time. Just want to preserve them between application restarts. – Carl J. Dec 12 '10 at 02:54
1

You can access the cache through HttpRuntime.Cache when you don't have an HttpContext available. However, at Application_End, i believe the cache is already flushed.

The solution Dima Shmidt outlines would be the best approach to store your cached values. That is by adding your items to cache with a CacheItemRemovedCallback, and store the values to disk there.

Magnus
  • 1,022
  • 8
  • 12
0

As an alternative solution you could store the data in Application object (Application[key]) or simply create a static class and use it to keep your data within app - in this case the data would sill be available upon Application_End.

Denis Ivin
  • 5,594
  • 1
  • 26
  • 25
  • Thanks, Denis - the data arrays might be too large - is there any other way to access cache? The problem is that it's not available because there is no request - but the cache must still be accessible at least somehow? – Carl J. Dec 12 '10 at 02:57
  • Well, another suggestion would be to provide CacheItemRemovedCallback (http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcallback.aspx) when adding items to the cache - the method provided in the callback is automatically fired right before app's end.. works well unless you manually clear the cache somewhere else in the app. – Denis Ivin Dec 12 '10 at 03:17
  • Since the cache plays a big role in your app - why not to take advantage of using AppFabric Caching/Velocity (http://www.hanselman.com/blog/InstallingConfiguringAndUsingWindowsServerAppFabricAndTheVelocityMemoryCacheIn10Minutes.aspx).. I think it automatically preserves the cache between restarts plus gives many more benefits. – Denis Ivin Dec 12 '10 at 03:22
  • If you are using static class you should also care about locks – Eugeniu Torica Jun 11 '11 at 10:29