MemoryCache 在第一次过期后总是返回“null”

and*_*lzo 7 c# iis asp.net-mvc memorycache

我有以下问题,我有这种缓存管理:

public class CacheManager : ICacheManager {

    private readonly ObjectCache cache;

    public CacheManager() {
        this.cache = MemoryCache.Default;
    }

    public void AddItem(string key, object itemToCache, double duration) {
        var end = DateTime.Now.AddSeconds(duration);
        this.cache.Add(key, itemToCache, end);
    }

    public T Get<T>(string key) where T : class {
        try {
            return (T)this.cache[key];
        }
        catch (Exception ex) {
            return null;
        }
    }

    public void Remove(string key) {
        this.cache.Remove(key);
    }
}
Run Code Online (Sandbox Code Playgroud)

很简单。

编辑我(问题文本中的错误修复)

问题:当任何键的缓存过期时,获取缓存中任何对象的所有以下调用都会返回“null”。我检查了按键并且总是正确的。

该问题仅出现在我的 PC 中的客户端服务器中,并且我的服务器工作正常。

谢谢。

编辑二(前进)

当我们重新启动客户服务器中的应用程序池时,您的问题在几个小时内得到了解决。

我们的网站有一个特定的应用程序池,具有以下设置:

Pipeline Manager 模式:集成框架:v4.0 启用 32 位应用程序:True。

其他设置如默认。

在服务器上,我们有 2 个站点,其中一个启用了“启用 32 位应用程序”,如果两个站点都被禁用,则会发生错误,但不知道这是否是问题所在。

编辑III(前进)

由于我们无法解决问题,我们决定切换到Httpcontext.Current.Cache,我们可以解决问题。我们想要另一个解决方案并继续使用MemoryCache但没有结果。

Jam*_*ack 2

复活一个老问题,但我相信这个问题是由于一个仅在 ASP.NET 4.5 中修复的错误造成的,但对早期版本有一个修补程序。

请参阅此线程:MemoryCache Empty:设置后返回 null