我在 aspnet 中实现 MemoryCache 对象时遇到了问题。 这是我的代码
ObjectCache cache = MemoryCache.Default;
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0);
cache.Add("keyName", resultQuery, policy);
对象正确地插入到缓存中,但是当我删除并重写它时, View 中的新对象仅在我的 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);
这是简单的 Get 实现。
cache.Get(cacheKey);
我不明白为什么有时在不同的浏览器 session 中工作,有时却不工作。 有人可以帮助我吗? 谢谢。
最佳答案
使用 System.Web.Caching.cache 使用此代码获取缓存
公共(public)字符串[] GetCache() {
string[] cache = Cache["names"] as string[];
if(names == null)
{
cache= DB.GetCache();
Cache["names"] = cache;
}
return cache;
关于c# - 内存缓存 Aspnet MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860945/