Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
346 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...