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