如何从Controller中使缓存数据[OutputCache]无效?

Gib*_*boK 38 c# asp.net-mvc caching asp.net-mvc-3

使用ASP.Net MVC 3我有一个控制器,其输出正在使用属性进行缓存 [OutputCache]

[OutputCache]
public controllerA(){}
Run Code Online (Sandbox Code Playgroud)

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

public controllerB(){} // Calling this invalidates the cache
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 57

你可以使用这个RemoveOutputCacheItem方法.

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

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

索引操作响应在服务器上缓存1分钟.如果您点击该InvalidateCacheForIndexAction操作,它将使Index操作的缓存失效.目前无法使整个缓存无效,您应该根据缓存操作(而非控制器)执行此操作,因为该RemoveOutputCacheItem方法需要缓存的服务器端脚本的URL.

  • e10,您无法从服务器上删除客户端浏览器上缓存的数据.这没有意义. (3认同)
  • @DarinDimitrov我可以在客户端清除缓存吗?我发现很难找到任何带有该信息的文章. (3认同)
  • 你是如何为 - > Location = OutputCacheLocation.Client实现这个,还有其他特定的参数/方法吗? (2认同)