简体   繁体   English

在UserControl上以编程方式使OutputCache无效

[英]Invalidating OutputCache programmatically on UserControl

Is there any way of invalidating OutputCache on UserControl? 有什么方法可以使UserControl上的OutputCache无效吗?
I've setup a partial caching on my site using UserControls and it's working fine. 我已经使用UserControls在我的网站上设置了部分缓存,并且工作正常。
I've set output cache like this in my user control: 我已经在用户控件中设置了输出缓存:

<%@ OutputCache Duration="3600" VaryByParam="None" %>

My user contrlol is located in /UserControls/SomeAction.ascx 我的用户控制位于/UserControls/SomeAction.ascx
So I've tried to invalidate it using this and it didn't work.: 所以我试图使用它使它无效,但是它不起作用。

HttpResponse.RemoveOutputCacheItem("/UserControls/SomeAction.ascx");

I also tried this approach: 我也尝试过这种方法:
I've set HttpContext.Current.Cache.Insert("MyCache",DateTime.Now); 我已经设置了HttpContext.Current.Cache.Insert("MyCache",DateTime.Now); inside Global.asax's Application_Start function, and Response.AddCacheItemDependency("MyCache"); 在Global.asax的Application_Start函数中,以及Response.AddCacheItemDependency("MyCache"); inside my user control's Page_Load function. 在我的用户控件的Page_Load函数中。
I've then tried to invalidate it by calling another function: 然后,我试图通过调用另一个函数来使其无效:

    private void InvalidateCache()
    {
        HttpContext.Current.Cache.Insert("MyCache", DateTime.Now);
    }

It still didn't work. 仍然没有用。

Is there any way to invalidate UserControl's cache programmatically? 有什么方法可以通过编程使UserControl的缓存无效吗?

Use usercontrol's CachePolicy property to create a dependency on another cache key. 使用usercontrol的CachePolicy属性创建对另一个缓存键的依赖关系。 For example, in user control code 例如,在用户控制代码中

protected void Page_Load(object sender, EventArgs e)
{
    this.CachePolicy.Dependency = new System.Web.Caching.CacheDependency(null, 
        new string[] { "KeyForThisUserControl"  });
    ...
}

And else-where to invalidate user-control's cache, use 在其他地方使用户控件的缓存无效,请使用

Cache["KeyForThisUserControl"] = DateTime.Now;

where Cache refers to current web application cache. 其中,缓存是指当前的Web应用程序缓存。

This is possibly your problem: 这可能是您的问题:

Make sure you set the cache Location = "Server" otherwise it will cache on the Clients machine and then your code 确保将缓存Location = "Server"设置为Location = "Server"否则它将缓存在客户端计算机上,然后缓存您的代码

HttpResponse.RemoveOutputCacheItem("/UserControls/SomeAction.ascx");

Should work? 应该管用?

I think the problem is that you are calling RemoveOutputCacheItem from the control itself, which would mean that the control is basically trying to remove itself from the cache... 我认为问题在于您正在从控件本身调用RemoveOutputCacheItem,这意味着控件基本上是在尝试从缓存中删除自身...

Try calling HttpResponse.RemoveOutputCacheItem("/UserControls/SomeAction.ascx"); 尝试调用HttpResponse.RemoveOutputCacheItem("/UserControls/SomeAction.ascx"); from another page. 从另一个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM