Click to See Complete Forum and Search --> : : How to Clear the Cache[] , on Application Error?


kumaran_sb
May 4th, 2003, 06:38 PM
Hi Guys

I've been trying to figure this out for a couple of days now..
I have an app which uses a cached dataset(Cache["DataSet"])..Now whenever there is an application error
say some exception is thrown, I have a feeling it screws up the Cache Object..

It looks like there is an entry for "DataSet" in the Cache but it holds a null reference..
So is there anyway, reload the Cache or remove this reference from the Cache on some error..??

Note that the Cache object is out of scope in the Global.asax!! OR am i mistaken?

Regards
K

V. Lorenzo
May 7th, 2003, 06:17 AM
Hi:

There're some ways to implement an objects cache in a web app, but ost of the time I use an 'Application' state object based cache.

Say, I create a proxy class with static methods and properties, e.g:

public class CacheTools
{
public static Hashtable Cache
{
get
{
Hashtable ChacheObject = (Hashtable)HttpContext.Current.Application["CACHEOBJECT"];
if (ChacheObject == null)
{
ChacheObject = new Hastable();
HttpContext.Current.Application["CACHEOBJECT"] = ChacheObject;
}
}
}
}

Now, in some other place, you could make a call like:

object MyObject = CacheTools.Cache[ "MyObject" ];

This works most of the time but, unfortuantedly, there are some cases (methods calls) where the HttpContext.Current does not exist, the called thread is not related to a web request.

The good thing about this is that (theoretically, at least), the Application state object is guaranteed to be preserved even under an ASP windows process (aspnet_wp.exe) restart.

Unfortunately, in practice, this is not always true, so in some cases it would be necessary to look for some other solution like remote objects exported by a local application.

Bye