3
votes

I am trying to implement caching in an MVC 4.0 ASP.net application. I can cache using outputcache

[OutputCache (Duration=60)]
public ActionResult myaction(string parm1)
{
     --logic to construct the model object
     -- followed by this return statement

     return PartialView(model);
}

But I need to clear the cache after editing data which is stored in an xml file.

So I tried to add

HttpResponse.RemoveOutputCacheItem(Url.Action("myaction", "myController"));

in another action of the same controller before calling return RedirectToAction(myaction);

But the cache is not getting reset.

Is this the method to refresh outputcache using actions? I call these actions from jquery using ajax.

3

3 Answers

0
votes

Use ICacheProvider instead. OutputCache is very limited on what you can do.

Once you need to change the data that is cached, you can invalidate that particular data using ICacheProvider while using OutputCache, you either cache the entire ActionResult or none.

Also OutputCache does neither have the flexibility that ICacheProvider has, nor the beauty of working with it.

0
votes

You may have cached the output on the user's machine instead of the server. Try specifying the Location:

[OutputCache(Location= System.Web.UI.OutputCacheLocation.Server, Duration=60]

Otherwise the cache deletion won't work because you've cached the HTML output on the user's machine

0
votes

ASP.NET MVC supports donut-hole caching but not donut caching. You can cache partial views and exclude parent view BUT you cannot cache parent view and exclude partial views. Write a custom filter for caching or use https://www.nuget.org/packages/MvcDonutCaching