views:

1353

answers:

5

Hello all,

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.

+2  A: 

Edit: If you have enabled kernel caching on II6+ then you will need to go with Luke's advice and use a VaryByCustom header, as clearing ASP.NET cache will not affect kernel cache.

OutputCache is stored in the ASP.NET Cache, so you can just call Cache.Remove:

List<string> keys = new List<string>();

foreach(string key in HttpRuntime.Cache)
{
    keys.Add(key);
}

foreach(string key in keys)
{
    Cache.Remove(key);
}

This will, however, remove ALL cache entries, including custom entries added by your code.

Richard Szalay
Are you sure this is the same cache? I thought that the OutputCache was a IIS thing. I don't think this works.
Henk Holterman
Oh good point, I forgot about Kernel Cache
Richard Szalay
A: 

Luke.

Thanks for the reply. I have included GetVaryByCustomString method into Global.asax file but it doesnt let me compile the application neither it produces any error. Any suggestions???

Please add responses like this as a comment to the answer as opposed to adding another answer.
Rob
@Raheel, Apologies, there were a few typos in my code. I've fixed it now.
LukeH
+3  A: 

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"];
        return (o == null ? Guid.NewGuid() : 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.

LukeH
+2  A: 

You can use HttpResponse.RemoveOutputCacheItem or HttpResponse.AddCacheItemDependency to invalidate output cache entries.

Josef
I ended up doing something similar:http://stackoverflow.com/questions/11585/clearing-page-cache-in-asp-net/2876701#2876701
Kevin
A: 

you can add HttpResponse.RemoveOutputCacheItem("/YourCachePageName.aspx"); line of code in the page load event of the page, loading of which you expect the cached page's cache to go off.

suranga