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