Asp.Net 2.0 Replace item in cache

Last post 05-14-2008 9:35 AM by GavH. 2 replies.

Sort Posts:

  • Asp.Net 2.0 Replace item in cache

    05-14-2008, 6:12 AM
    • Loading...
    • GavH
    • Joined on 10-12-2006, 9:06 AM
    • Posts 78

    Hi,

    I have a large dataset that I keep cached, however I have occasion to update that cache item from time to time.

     

    Currently we use something like the following:

     

    DSDiaryTableAdapters.tEventsTableAdapter dta = new DSDiaryTableAdapters.tEventsTableAdapter();
                    dTab = dta.GetData();
                    //update cache
    
                    HttpContext.Current.Cache.Add("FULLEVENTS_MYSQL",
                        dTab, null, DateTime.Now.AddMinutes(30),System.Web.Caching.Cache.NoSlidingExpiration,
                        System.Web.Caching.CacheItemPriority.Normal, null);

     

    This method updates the item in the cache on 3 development machines, (XP, and Vista) but on our server (2K3) it does not, the code executes without error but the item in the cache is still the previous (pre updated) item.

    If we add the line

    HttpContext.Current.Cache.Remove("FULLEVENTS_MYSQL");

    before the Cache.Add then the update works.

    I was just wondering if anyone could enlighten me as to why this is required on the server but not on the dev machines.

    Under what conditions does Cache.Add not replace an item under the same key?

    Thanks 


     

  • Re: Asp.Net 2.0 Replace item in cache

    05-14-2008, 7:06 AM
    Answer

    User Cache.Insert to replace value for an existing key.

    The Add and Insert methods have the same signature, but there are subtle differences between them. First, calling the Add method returns an object that represents the cached item, while calling Insert does not. Second, their behavior is different if you call these methods and add an item to the Cache that is already stored there. The Insert method replaces the item, while the Add method fails.

    Vishal
  • Re: Asp.Net 2.0 Replace item in cache

    05-14-2008, 9:35 AM
    • Loading...
    • GavH
    • Joined on 10-12-2006, 9:06 AM
    • Posts 78

     Thanks Vishal,

     

    Insert does indeed replace the cache item in all environments. 

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter