绕道的OutputCache的ASP.NET MVC [英] Bypass OutputCache in ASP.NET MVC

查看:100
本文介绍了绕道的OutputCache的ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

感谢。

推荐答案

所有我需要到是使用缓存依赖性。我需要这种变化在运行时修改,所以配置文件是不是一种选择。

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

这篇关于绕道的OutputCache的ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆