C# 在ObjectCache中存储项:无法获取旧值
我使用MemoryCache.ObjectCache的默认实例在MVC应用程序中存储数据;每次我保存一些项目并立即阅读它时,我看到项目按预期存储。但当Cashing类的新实例(我是指使用ObjectCache的类)创建时,我无法访问旧值。我实现了singleton,只为ObjectCache保留一个实例,但这对我没有帮助C# 在ObjectCache中存储项:无法获取旧值,c#,asp.net-mvc-4,objectcache,C#,Asp.net Mvc 4,Objectcache,我使用MemoryCache.ObjectCache的默认实例在MVC应用程序中存储数据;每次我保存一些项目并立即阅读它时,我看到项目按预期存储。但当Cashing类的新实例(我是指使用ObjectCache的类)创建时,我无法访问旧值。我实现了singleton,只为ObjectCache保留一个实例,但这对我没有帮助 //here is single instance of caching object public class CacheSingleton { private st
//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);
}
谁能解释一下为什么每次缓存类的新实例最后一个代码我都不能访问旧值。谢谢。cacheTime的值是多少?您能否确认它们足够大,以防止项目被清除?