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

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

问题描述

使用ASP.Net MVC 3

我有输出被使用属性缓存在控制器 [的OutputCache]

  [的OutputCache]
公共controllerA(){}

我想知道是否有可能对特定的控制器或一般都缓存数据调用诺特尔控制器

 公共controllerB(){} //调用此缓存失效


解决方案

您可以使用 RemoveOutputCacheItem 方法。

下面是你如何可以使用它的一个例子:

 公共类HomeController的:控制器
{
    [的OutputCache(持续时间= 60,位置= OutputCacheLocation.Server)
    公众的ActionResult指数()
    {
        返回含量(DateTime.Now.ToLongTimeString());
    }    公众的ActionResult InvalidateCacheForIndexAction()
    {
        字符串路径= Url.Action(指数);
        Response.RemoveOutputCacheItem(路径);
        返回的内容(缓存失效,你现在可以回去的索引操作);
    }
}

索引操作响应被缓存在服务器上为1分钟。如果你打的 InvalidateCacheForIndexAction 操作将到期的指数操作的缓存。目前还没有办法无效整个缓存,你应该做的每个缓存操作(不控制器),因为 RemoveOutputCacheItem 方法需要在服务器端脚本的git缓存的URL。

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

[OutputCache]
public controllerA(){}

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

public controllerB(){} // Calling this Invalidate the Cache

解决方案

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");
    }
}

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 git cached.

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

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