【问题标题】:MemoryCache Aspnet MVC内存缓存 Aspnet MVC
【发布时间】:2015-09-30 08:04:13
【问题描述】:

我在 aspnet 中实现 MemoryCache 对象时遇到问题。 这是我的代码

        ObjectCache cache = MemoryCache.Default;
        CacheItemPolicy policy = new CacheItemPolicy();
        policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0);
        cache.Add("keyName", resultQuery, policy);

对象在缓存中正确插入,但是当我删除并重写它时,视图中的新对象仅在我的会话中可见。

        var q = queryObject;
        var cacheKey = "keyName";
        MemoryCache.Default.Remove(cacheKey);
        ObjectCache cache = MemoryCache.Default;
        CacheItemPolicy policy = new CacheItemPolicy();
        policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0);
        cache.Add(cacheKey, q, policy);

这就是简单的 Get 实现。

        cache.Get(cacheKey);

我不明白为什么有时可以在不同的浏览器会话中工作,有时却不能工作。 有人可以帮助我吗? 谢谢。

【问题讨论】:

  • 什么意思只在我的会话中可见?是什么让你这么认为?

标签: c# asp.net-mvc memorycache


【解决方案1】:

使用 System.Web.Caching.cache 使用此代码获取缓存

公共字符串[] GetCache() {

string[] cache = Cache["names"] as string[];
if(names == null)
{
cache= DB.GetCache();
Cache["names"] = cache;
}
return cache;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2021-11-13
    • 2019-05-16
    • 2014-01-29
    • 2010-12-13
    • 1970-01-01
    相关资源
    最近更新 更多