c# - 内存缓存 Aspnet MVC

标签 c# asp.net-mvc memorycache

我在 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/

相关文章:

c# - MemoryCache - 防止项目过期

c# - WebCache 和 MemoryCache 在缓存方面的区别

c# - 组合框的值应显示在文本框中

c# - 从 HttpRouteCollection 中删除路由

jquery - 在 Web Api 2 中发帖

c# - 如何将上传的文件从javascript发送到MVC中的 Controller ?

html - 如何将验证消息 span 包装在 div 中并根据验证渲染容器?

c# - 如何在 .Net Core ActionFilterAttribute 中使用依赖注入(inject)?

c# - Gremlin.NET : Execute queries using . Next() 引发 NullReferenceException

C#接口(interface)问题