绕过 ASP.NET MVC 中的 OutputCache

发布于 2024-10-11 05:09:15 字数 288 浏览 2 评论 0原文

我在 MVC 网站中使用 OutputCache 属性,如下所示:

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

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

在这种情况下我是否可以绕过缓存?

谢谢。

I am using the OutputCache attribute in my MVC website as follows:

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

However sometimes I'd like to bypass the output cache entirely and force a fetch from the database. This is especially true for my test environment where I am continuously loading new data in to the database for testing.

Is there anyway I could bypass the cache in this case?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箹锭⒈辈孓 2024-10-18 05:09:15

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

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

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

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

Instead of specifying all of your output cache parameters inline, in the attribute, you could use an OutputCache profile.

Output Cache profiles allow you to put all of your output cache settings in your web.config, give the profile a name, and then point to that profile from your attribute.

Once you have that set up, you could alter the settings in the web.config you use to debug with so that the caching duration is only 1 second. Obviously you would leave your production web.config file with a much larger duration.

For more information about profiles, check out http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx

痴梦一场 2024-10-18 05:09:15

如果您想完全关闭它,您可以

<caching>
  <outputCache enableOutputCache="false" />
</caching>

在 .system.web 下的 web.config 中使用。如果您想通过代码(使用按钮或其他东西)来完成此操作,您也可以这样做:

System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
System.Web.Configuration.OutputCacheSection cacheSection = (System.Web.Configuration.OutputCacheSection)config.GetSection("system.web/caching/outputCache");
cacheSection.EnableOutputCache = true/false;
config.Save();

可能仅适用于您的开发计算机。大多数服务器都设置为不允许编辑 machine.config 中的该部分。

If you wanted to turn it off completely you could use

<caching>
  <outputCache enableOutputCache="false" />
</caching>

in your web.config under.system.web. If you wanted to do it from code (using a button or something else) you could also do:

System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
System.Web.Configuration.OutputCacheSection cacheSection = (System.Web.Configuration.OutputCacheSection)config.GetSection("system.web/caching/outputCache");
cacheSection.EnableOutputCache = true/false;
config.Save();

This will probably only work on your dev machine. Most servers are set up to not allow editing that section in the machine.config.

暖心男生 2024-10-18 05:09:15

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

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

Response.AddCacheItemDependency("Pages");

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

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

All I needed to to was use a cache dependency. I needed this change to be modified at runtime, so config files were not an option.

Added the following to the action i wanted to have a "no-cache" option on.

Response.AddCacheItemDependency("Pages");

And created the following action that i can call to refresh cache.

public ActionResult RefreshCache()
    {
        HttpContext.Cache.Insert("Pages", DateTime.Now, null,
                                 DateTime.MaxValue, TimeSpan.Zero,
                                 CacheItemPriority.NotRemovable,
                                 null);
        Logger.Info("Cleansed cache");
        return RedirectToAction("HubContent");
    }
你对谁都笑 2024-10-18 05:09:15

这并不能完全回答您的问题,而是回答您的标题(这不是“如何从缓存中清除项目”):
您可以添加“VaryByParam”:

[OutputCache(Duration=5000,VaryByParam="YourExistingParams;CacheBypass")]

然后当您想要绕过缓存时,只需向 CacheBypass 参数传递 DateTime.UtcNow.Ticks 的值即可> (或任何随机的东西)=>例如:http://localhost/?CacheBypass=1234567890

This doesn't exactly answer your question but answers your title (which is not "how to clear an item from the cache"):
You could add a "VaryByParam":

[OutputCache(Duration=5000,VaryByParam="YourExistingParams;CacheBypass")]

then when you want to bypass the cache you simply pass the CacheBypass parameter the value of DateTime.UtcNow.Ticks (or any random thing) => for example: http://localhost/?CacheBypass=1234567890

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文