I use MemoryCache.Default instance of ObjectCache for storing data in MVC application; Every time I save some item and than immedeatly read it I see that items are stored as expected.But when new instance of Cashing class ( I mean class that used ObjectCache) is created I cant access old values.I have imlemented singleton for keeping only one instance for ObjectCache but this doesnt help me.
//here is single instance of caching object
public class CacheSingleton
{
private static CacheSingleton instance;
private CacheSingleton() { cache = MemoryCache.Default; }
public static CacheSingleton Instance
{
get
{
if (instance == null)
{
instance = new CacheSingleton();
}
return instance;
}
}
public ObjectCache cache { get; private set; }
}
// and here is piece of caching class
.........
private ObjectCache cache;
public DefaultCacheProvider()
{
cache = CacheSingleton.Instance.cache;
}
public object Get(string key)
{
return cache[key];
}
public void Set(string key, object data, int cacheTime, bool isAbsoluteExpiration)
{
CacheItemPolicy policy = new CacheItemPolicy();
if (isAbsoluteExpiration)
{
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
}
else
{
policy.SlidingExpiration = TimeSpan.FromMinutes(cacheTime);
}
cache.Add(new CacheItem(key, data), policy);
}
public bool IsSet(string key)
{
return (cache[key] != null);
}
public void Invalidate(string key)
{
cache.Remove(key);
}
Can anybody explain why with every new instance of caching class (last code) I cant access old values.Thanks.