我的项目在Windows Server 2012 R2 IIS8.5上使用ASP.NET MVC5 ( .net 4.5.1 )
我有一个获取和设置缓存的类
public class TheCache
{
#region Instance
private static TheCache caches;
private TheCache() { }
public static TheCache Instance
{
get
{
if (null == caches)
caches = new TheCache();
return caches;
}
}
#endregion
// Set the cache
public void SetCache(key, data, time){
MemoryCache.Default.Set(key, data, time);
}
// Get the cache
public object GetCache(key){
return MemoryCache.Default.Get(key);
}
}
现在的问题是,我在控制器中有一个动作
public ActionResult SomeActionInController(){
// this place always execute in every request
if (null == TheCache.Instance.GetCache("key")
TheCache.Instance.SetCache("key", "value", Datetime.Now.AddHours(1));
}
但我会在每个请求中得到null 。所以为什么?有什么办法可以解决吗?谢谢你。