2

Is it possible to clear one action's cache from another action?

Let's say my Index action lists all my Widgets. There are lots of Widgets but new ones are not created very often. So I want to cache my Index action indefinitely but force it to render after a successful Create.

public class WidgetController : Controller
{
    [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
    public ActionResult Index()
    {
        return View(Widget.AllWidgets);
    }

    public ActionResult Create()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(string name)
    {
        Widget widget = new Widget(name);

        // Can I clear Index's cache at this point?
        // ClearCache("Index");

        return View(widget);
    }
}
Lobstrosity
  • 3,928
  • 29
  • 23
  • possible duplicate of [How to "invalidate" portions of ASP.NET MVC output cache?](http://stackoverflow.com/questions/1288463/how-to-invalidate-portions-of-asp-net-mvc-output-cache) – bzlm Oct 25 '10 at 18:21

3 Answers3

4

HttpResponse.RemoveOutputCacheItem?

takepara
  • 10,413
  • 3
  • 34
  • 31
  • Thanks, led me to the following question, which answered a bit more thoroughly: http://stackoverflow.com/questions/1288463/how-to-invalidate-portions-of-asp-net-mvc-output-cache – Lobstrosity Nov 02 '09 at 15:25
2

Use a VaryByCustom property to expire the cache whenever a new Widget is added.

Chris Shaffer
  • 32,199
  • 5
  • 49
  • 61
0

IMHO if you call the Create action you won't hit the cache because you are just rendering a view and not redirecting to the Index action whose output has been cached.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Right. I'm not concerned about caching Create. But when the POST Create action occurs, I want to programmatically clear Index's cache so that the next hit on Index will reflect the new Widget. – Lobstrosity Oct 29 '09 at 13:34