CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2003
    Location
    NY
    Posts
    2

    Question : How to Clear the Cache[] , on Application Error?

    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

  2. #2
    Join Date
    Feb 2002
    Location
    Spain
    Posts
    148
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured