asp.net-mvc - 将数据存储在缓存中 30 分钟 MVC

标签 asp.net-mvc asp.net-mvc-3

我的 MVC Controller 中有一个操作,我想根据传递的参数作为键来缓存其返回结果,以便下次调用该操作时,它会首先在缓存中查找,如果没有找到,它会查看数据存储。

 public ActionResult GetSearchResult(string zipcode, int pageSize, int currentPage)
 {
     Cache[zipcode + page + currentpage] = somedata // but it should be cleared after 30 min
 }

我该怎么做?我可以将数据存储在缓存对象中,如上所述,但我想确保缓存对象在 30 分钟后被清除。我不知道如何在全局或每个缓存对象的基础上配置生命周期。

最佳答案

我强烈建议在您的操作中使用输出缓存过滤器,而不是自己手动执行

     [OutputCache(Duration=1800, VaryByParam="*")]
     public ActionResult GetSearchResult(string zipcode, int pageSize, int currentPage)
     {
  //       Cache[zipcode + page + currentpage] = somedata // but it should be cleared after 30 min
     }

关于asp.net-mvc - 将数据存储在缓存中 30 分钟 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10385794/

相关文章:

asp.net-mvc - 通过 HtmlHelper 创建动态 RadEditor

c# - 使用 ninject 在单元测试中注入(inject)属性

asp.net-mvc-3 - "using Mvc.Mailer;"错误

asp.net-mvc - HtmlHelper 扩展方法的 HTML 编码

c# - 有什么充分的理由不在核心 MVC 中使用 ViewComponent 而不是 Partial View 吗?

c# - ASP.NET HttpContext 缓存在插入后立即删除

c# - 使用 MVC3 IDependencyResolver 从 Unity 2.0 解析命名注册

c# - MaxLength 属性不生成客户端验证属性

javascript - 如何从 foreach 循环创建动态 ID

asp.net-mvc-3 - 存储必要的 OpenID 信息