【问题标题】:MemoryCache object become nullMemoryCache 对象变为空
【发布时间】:2015-03-04 05:34:29
【问题描述】:

我有一个缓存对象的缓存管理器类,但我遇到了问题。有时,完全随机,MemoryCache.Default.Contains(keyName) 变为空,我的应用程序在没有任何缓存的情况下工作。我对这个问题感到非常沮丧。

我该怎么办?

它是在安装了 .NET 4.5 的 Windows Server 2008 R2 上运行的 ASP.NET WebForms 4.0。

当我在本地机器上运行这个应用程序时,我从来没有遇到过这个问题。

public class CacheManager
{
    private static object _lockObject = new object();
    public static object GetItem(string keyName, Func<object> action)
    {
        if (!MemoryCache.Default.Contains(keyName)) {
            System.Threading.Monitor.Enter(_lockObject);
            try {
                if (!MemoryCache.Default.Contains(keyName)) {
                    dynamic value = action.Invoke();
                    MemoryCache.Default.Set(keyName, value, System.DateTime.Now.AddDays(10));
                }
            }
            finally {
                System.Threading.Monitor.Exit(_lockObject);
            }
        }

        return MemoryCache.Default.GetCacheItem(keyName).Value;
    }

    public static void ResetCache(string keyName)
    {
        if (MemoryCache.Default.Contains(keyName)) {
            System.Threading.Monitor.Enter(_lockObject);
            try {
                if (MemoryCache.Default.Contains(keyName)) {
                    MemoryCache.Default.Remove(keyName);
                }
            }
            finally {
                System.Threading.Monitor.Exit(_lockObject);
            }
        }
    }
}

【问题讨论】:

    标签: c# asp.net caching memorycache


    【解决方案1】:

    我终于找到了问题所在。我检查了 IIS,我的应用程序池由于 UnexpectedException 而被重置。我的代码没有任何问题。

    【讨论】:

      猜你喜欢
      • 2011-12-12
      • 2019-05-31
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多