ASP.NET Cache: Strange caching behavior

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

I have implemented some piece of ASP.NET code weeks ago. This code uses the cache as shown bellow:

Cache.Remove(“My”);

Cache.Add(“MyKey”, myObject, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), System.Web.Caching.CacheItemPriority.Default, null);

During testing I found out, that this code sometimes does not work. It means after the method Add(…) is executed no entry in the cache is added.

I tried to find some solution in internet. After 10 minutes intensive searching I decided to do some work around. However, first I had to wait few weeks that the same problem appears again.

Currently I have no idea how to explain this "sophisticated functionality", but the code bellow will help:

Cache.Remove(“MyKey”);
 

Cache.Add(“MyKey, vars, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), System.Web.Caching.CacheItemPriority.Default, null);
 

If(Cache.Count == 0)
{
  Cache.Add("dummy", "anything", null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), System.Web.Caching.CacheItemPriority.Default, null);

  Cache.Add(“MyKey”, vars, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), System.Web.Caching.CacheItemPriority.Default, null);

 Cache.Remove("dummy");
}

Remarks: Please be aware of using of the Cache in clustered environment!


Posted Apr 03 2006, 03:31 PM by Damir Dobric
Filed under:

Comments

Damir Dobric wrote re: ASP.NET Cache: Strange caching behavior
on 04-13-2006 17:04
I have experienced that restart of the default web server helps if the solution above does not work.
Scott Munro wrote Cache Items Disappearing!
on 04-18-2006 11:12
We (damir and I) have recently experienced some strange behaviour regarding the System.Web.Caching.Cache...
developers.de is a .Net Community Blog powered by daenet GmbH.