如何使控制器中的缓存数据 [OutputCache] 无效? [英] How to invalidate cache data [OutputCache] from a Controller?

查看:26
本文介绍了如何使控制器中的缓存数据 [OutputCache] 无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ASP.Net MVC 3 我有一个使用属性缓存输出的控制器 [OutputCache]

Using ASP.Net MVC 3 I have a Controller which output is being cached using attributes [OutputCache]

[OutputCache]
public controllerA(){}

我想知道是否可以通过调用另一个控制器来使特定控制器的缓存数据 (SERVER CACHE) 或通常所有缓存数据无效

I would like to know if it is possible to invalidate the Cache Data (SERVER CACHE) for a Specific Controller or generally all the Cache data by calling another controller

public controllerB(){} // Calling this invalidates the cache

推荐答案

您可以使用 RemoveOutputCacheItem 方法.

You could use the RemoveOutputCacheItem method.

以下是您如何使用它的示例:

Here's an example of how you could use it:

public class HomeController : Controller
{
    [OutputCache(Duration = 60, Location = OutputCacheLocation.Server)]
    public ActionResult Index()
    {
        return Content(DateTime.Now.ToLongTimeString());
    }

    public ActionResult InvalidateCacheForIndexAction()
    {
        string path = Url.Action("index");
        Response.RemoveOutputCacheItem(path);
        return Content("cache invalidated, you could now go back to the index action");
    }
}

索引操作响应在服务器上缓存了 1 分钟.如果您点击 InvalidateCacheForIndexAction 操作,它将使索引操作的缓存过期.目前无法使整个缓存失效,您应该针对每个缓存操作(而不是控制器)执行此操作,因为 RemoveOutputCacheItem 方法需要它缓存的服务器端脚本的 url.

The Index action response is cached on the server for 1 minute. If you hit the InvalidateCacheForIndexAction action it will expire the cache for the Index action. Currently there's no way to invalidate the entire cache, you should do it per cached action (not controller) because the RemoveOutputCacheItem method expects the url of the server side script that it cached.

这篇关于如何使控制器中的缓存数据 [OutputCache] 无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆