绕过ASP.NET MVC中的OutputCache

Cra*_*rge 9 model-view-controller asp.net-mvc outputcache

我在我的MVC网站中使用OutputCache属性如下:

[OutputCache(Duration = 5000,
        VaryByParam = "name;region;model;id;op;content;featured;isStarred;page;size;")]
Run Code Online (Sandbox Code Playgroud)

但有时我想完全绕过输出缓存并强制从数据库中获取.对于我不断将新数据加载到数据库进行测试的测试环境,尤其如此.

无论如何我在这种情况下可以绕过缓存吗?

谢谢.

nik*_*d23 10

您可以使用OutputCache配置文件,而不是在属性中指定内联的所有输出缓存参数.

输出缓存配置文件允许您将所有输出缓存设置放在web.config中,为配置文件指定名称,然后从属性中指向该配置文件.

完成设置后,您可以更改用于调试的web.config中的设置,以便缓存持续时间仅为1秒.显然,您会将生产web.config文件保留更长的持续时间.

有关配置文件的更多信息,请查看http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx


Cra*_*rge 2

我所需要的只是使用缓存依赖项。我需要在运行时修改此更改,因此配置文件不是一个选项。

将以下内容添加到我想要启用“无缓存”选项的操作中。

Response.AddCacheItemDependency("Pages");
Run Code Online (Sandbox Code Playgroud)

并创建了以下操作,我可以调用该操作来刷新缓存。

public ActionResult RefreshCache()
    {
        HttpContext.Cache.Insert("Pages", DateTime.Now, null,
                                 DateTime.MaxValue, TimeSpan.Zero,
                                 CacheItemPriority.NotRemovable,
                                 null);
        Logger.Info("Cleansed cache");
        return RedirectToAction("HubContent");
    }
Run Code Online (Sandbox Code Playgroud)