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

3 Answers 3

4

HttpResponse.RemoveOutputCacheItem?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, led me to the following question, which answered a bit more thoroughly: stackoverflow.com/questions/1288463/…
2

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

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.