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?