有什么方法可以清除/刷新/删除OutputCache? [英] Any way to clear/flush/remove OutputCache?

查看:17
本文介绍了有什么方法可以清除/刷新/删除OutputCache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网络用户控件 (.ascx) 上使用 OutputCache

<%@ OutputCache Duration="1000" VaryByParam="none" %>

我想在接下来的 1000 秒内保留缓存,但是当加载我网站上的特定页面时,我想删除/刷新/刷新缓存.就像,我想在加载 MyPage.aspx 时清除缓存.我可以以编程方式刷新缓存吗?

只有一页被缓存,所以没有参数化版本来刷新缓存.

提前感谢您的帮助.

解决方案

您可以使用 VaryByCustom 参数.

在您的用户控件中,您将拥有以下内容:

<%@ OutputCache Duration="1000" VaryByParam="None" VaryByCustom="MyKey" %>

然后您将覆盖 GetVaryByCustomString 方法在您的 Global.asax 中,如下所示:

public override string GetVaryByCustomString(HttpContext context, string arg){if (arg == "MyKey"){object o = context.Current.Application["MyGuid"];如果(o == 空){o = Guid.NewGuid();context.Current.Application["MyGuid"] = o;}返回o.ToString();}返回 base.GetVaryByCustomString(context, arg);}

最后在 MyPage.aspx 中你会这样做:

Application["MyGuid"] = Guid.NewGuid();

这是如何工作的?

每当您的控件被缓存时,它都与一个字符串相关联(当您的控件的 VaryByCustom 键被传递给它时,从 GetVaryByCustomString 方法返回的字符串).

无论何时使用该控件,都会再次调用 GetVaryByCustomString.如果返回的字符串与控件的缓存版本匹配,则使用缓存版本.

在我们的例子中,MyKey"被传递到 GetVaryByCustomString 并返回存储在 Application["MyGuid"] 中的任何内容.

每当 MyPage.aspx 被调用时,它都会将 Application["MyGuid"] 更改为一个新的随机值.

下次使用您的控件时,GetVaryByCustomString 方法将返回新值,并且由于没有与该值关联的控件的缓存版本,因此将重新生成控件.(然后控件将被缓存并与新值相关联,直到下一次调用 MyPage.aspx 等)

这里有一个概述.

I'm using the OutputCache on my webuser control (.ascx)

<%@ OutputCache Duration="1000" VaryByParam="none" %>

I would like to retain the cache for next 1000 seconds, but when a specific page on my site is loaded, I would like to remove/flush/refresh the cache. Like, I want to clear the cache when MyPage.aspx is loaded. Can i flush the cache programmetically?

Its only one page being cache so there are no paramatrized versions to flush cache with.

Thanks for your help in advance.

解决方案

You can use the VaryByCustom parameter for this.

In your user control you would have the following:

<%@ OutputCache Duration="1000" VaryByParam="None" VaryByCustom="MyKey" %>

Then you would override the GetVaryByCustomString method in your Global.asax like so:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "MyKey")
    {
        object o = context.Current.Application["MyGuid"];
        if (o == null)
        {
            o = Guid.NewGuid();
            context.Current.Application["MyGuid"] = o;
        }
        return o.ToString();
    }
    return base.GetVaryByCustomString(context, arg);
}

Finally in MyPage.aspx you would do this:

Application["MyGuid"] = Guid.NewGuid();

How does this work?

Whenever your control is cached, it is associated with a string (the string returned from the GetVaryByCustomString method when your control's VaryByCustom key is passed into it).

Whenever the control is subsequently used, GetVaryByCustomString is called again. If the returned string matches a cached version of the control, then the cached version is used.

In our case, "MyKey" is passed into GetVaryByCustomString and it returns whatever is stored in Application["MyGuid"].

Whenever MyPage.aspx is called, it changes Application["MyGuid"] to a new random value.

When your control is next used the GetVaryByCustomString method will return the new value, and because there is no cached version of the control associated with that value, the control will be regenerated. (The control will then be cached and associated with the new value, to persist until the next call to MyPage.aspx etc)

There's an overview here.

这篇关于有什么方法可以清除/刷新/删除OutputCache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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