绕过ASP.NET MVC中的OutputCache

时间:2021-06-30 03:51:12

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

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

[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.

谢谢。

5 个解决方案

#1


10  

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

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

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.

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

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.

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

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

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

#2


4  

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:

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

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.

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

#3


2  

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

#4


1  

See the following article for an AOP approach to customising the OutputCache

有关自定义OutputCache的AOP方法,请参阅以下文章

http://www.avantprime.com/blog/21/how-to-handle-output-caching-in-the-development-environment-for-asp-net-mvc-applications

http://www.avantprime.com/blog/21/how-to-handle-output-caching-in-the-development-environment-for-asp-net-mvc-applications

#5


1  

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":

这并不完全回答你的问题但回答你的标题(这不是“如何清除缓存中的项目”):你可以添加一个“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

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

#1


10  

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

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

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.

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

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.

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

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

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

#2


4  

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:

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

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.

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

#3


2  

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

#4


1  

See the following article for an AOP approach to customising the OutputCache

有关自定义OutputCache的AOP方法,请参阅以下文章

http://www.avantprime.com/blog/21/how-to-handle-output-caching-in-the-development-environment-for-asp-net-mvc-applications

http://www.avantprime.com/blog/21/how-to-handle-output-caching-in-the-development-environment-for-asp-net-mvc-applications

#5


1  

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":

这并不完全回答你的问题但回答你的标题(这不是“如何清除缓存中的项目”):你可以添加一个“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

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