Issue using HttpRuntime.Cache

asp.net, c#, caching

Solution

When you say every two minutes the value inserted is set to null, does that mean just the item you're interested in or every single item in the cache?

I ask this because the cache only exists as long as the application is running. If the application is restarted, the cache goes away. This would explain the behavior if everything goes away every 2 minutes. In that case you have a different problem on your hands: why does the application restart every 2 minutes.

If it's only SOME items then it could be a memory issue. The cache cleans itself up in response to low memory. I believe there's a way to set priority on values inserted. But this should only be a problem when you're low on memory.

If this still doesn't solve your problem, there is a way to discover why an item is being removed. It is explained here.

Problem

Am using following .net code to add objects to cache: ``` public static void Add<T>(string key, T dataToCache) { try { ApplicationLog.Instance.WriteInfoFormat("Inserting item with key {0} into Cache...", key); HttpRuntime.Cache.Insert( key, dataToCache, null, DateTime.Now.AddDays(7), System.Web.Caching.Cache.NoSlidingExpiration); } catch (Exception ex) { ApplicationLog.Instance.WriteException(ex); } } ``` and here is my code to retrieve values from cache: ``` public static T Get<T>(string key) { try { if (Exists(key)) { ApplicationLog.Instance.WriteInfoFormat("Retrieving item with key {0} from Cache...", key); return (T)HttpRuntime.Cache[key]; } else { ApplicationLog.Instance.WriteInfoFormat("Item with key {0} does not exist in Cache.", key); return default(T); } } catch(Exception ex) { ApplicationLog.Instance.WriteException(ex); return default(T); } } public static bool Exists(string key) { bool retVal = false; try { retVal= HttpRuntime.Cache[key] != null; } catch (Exception ex) { ApplicationLog.Instance.WriteException(ex); } return retVal; } ``` But i find that after every 2 minutes or so,the cached object value is getting set to null resulting in pulling that value from database again. What am i missing here?

Original source

Related problems