简体   繁体   English

MemoryCache Aspnet MVC

[英]MemoryCache Aspnet MVC

i've a problem for implement a MemoryCache object in aspnet. 我在ASPNET中实现MemoryCache对象时遇到问题。 This is my code 这是我的代码

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

The object is correct insert in the cache but when i delete and rewrite it the new object in the view is visible only in my session. 该对象正确插入缓存中,但是当我删除并重写该对象时,视图中的新对象仅在我的会话中可见。

        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);

And this is the simple Get Implementation. 这就是简单的Get Implementation。

        cache.Get(cacheKey);

I don't understand why sometimes works in different browser session and sometimes doesn't work. 我不明白为什么有时有时在不同的浏览器会话中起作用而有时不起作用。 Someone can help me? 有人可以帮我吗? Thanks. 谢谢。

Use the System.Web.Caching.cache Use this code to get Cache 使用System.Web.Caching.cache使用此代码获取Cache

public string[] GetCache() { 公共字符串[] GetCache(){

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM