【问题标题】:ASP.NET MVC: Clear an action's cache from another actionASP.NET MVC:从另一个动作中清除一个动作的缓存
【发布时间】:2009-10-28 14:57:14
【问题描述】:

是否可以从另一个动作中清除一个动作的缓存?

假设我的索引操作列出了我所有的小部件。有很多小部件,但不会经常创建新的小部件。所以我想无限期地缓存我的索引操作,但在成功创建后强制它渲染。

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

【问题讨论】:

标签: asp.net-mvc caching


【解决方案1】:

HttpResponse.RemoveOutputCacheItem?

【讨论】:

【解决方案2】:

每当添加新的 Widget 时,使用 VaryByCustom 属性使缓存过期。

【讨论】:

    【解决方案3】:

    恕我直言,如果您调用 Create 操作,您将不会命中缓存,因为您只是在渲染视图,而不是重定向到其输出已被缓存的 Index 操作。

    【讨论】:

    • 对。我不关心缓存 Create。但是当 POST Create 操作发生时,我想以编程方式清除 Index 的缓存,以便下一次点击 Index 将反映新的 Widget。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2010-11-23
    • 1970-01-01
    • 2018-02-01
    • 2018-04-18
    • 1970-01-01
    相关资源
    最近更新 更多