任何方法来清除/平/删除的OutputCache? [英] Any way to clear/flush/remove OutputCache?

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

问题描述

我使用的OutputCache我WEBUSER控件(.ascx)

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

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

我想保留后面的1000秒,缓存,但当装上我的网站的特定网页,我想删除/平/刷新缓存。就像,我想被载入MyPage.aspx时清除缓存。我能刷新缓存programmetically?

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?

它只有一个页面存在缓存,因此没有paramatrized版本冲洗用缓存。

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

感谢您的帮助提前。

推荐答案

您可以使用<$c$c>VaryByCustom参数这一点。

You can use the VaryByCustom parameter for this.

在用户控件,你将有以下内容:

In your user control you would have the following:

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

然后,你会重写<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpapplication.getvarybycustomstring.aspx\"><$c$c>GetVaryByCustomString方法在Global.asax中像这样:

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

MyPage.aspx 最后你可以这样做:

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

这是如何工作的?

每当你的控制缓存,它与一个字符串(从 GetVaryByCustomString 方法返回的字符串相关的,当你控制的 VaryByCustom是键被传递到它)。

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

当控件随后使用 GetVaryByCustomString 再次调用。如果返回的字符串控制的缓存版本一致,则使用缓存的版本。

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.

在我们的例子中,的myKey传递到 GetVaryByCustomString 键,返回无论是存储在应用[MyGuid]

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

每当 MyPage.aspx 被调用时,它改变了应用[MyGuid] 来一个新的随机值。

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

当你的控制下一次使用了 GetVaryByCustomString 方法将返回新值,因为没有与该值相关的控制没有缓存的版本,控制会再生。 (然后控制将被缓存,并用新值相关联,坚持直到 MyPage.aspx 下一次调用等)

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天全站免登陆