4

I am using the Asp.net OutputCache on a page containing a usercontrol that in certain circumstances when the usercontrol is edited i want to be able to expire the page cache and reload the page with fresh data.

Is there any way i can do this from within the usercontrol?

If not, what are some other methods of caching the page that will allow me to edit in this way.

----------- EDIT -----------

After some more research I found a method that seems to work well.

Dim cachekey As String = String.Format("Calendar-{0}", calendarID)
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
Response.AddCacheItemDependency(cachekey)

that will add a dependency to the page cache object, then to expire I do this:

Dim cachekey as string = String.Format("Calendar-{0}", CalendarID)
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)

Now as long as the dependency cachekey is known a page can be expired.

Andrei
  • 55,890
  • 9
  • 87
  • 108
John Boker
  • 82,559
  • 17
  • 97
  • 130

3 Answers3

1

You could try this:

private void RemoveButton_Click(object sender, System.EventArgs e)
{
    HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
}

From: http://aspalliance.com/668

Thanks.

Andre Gallo
  • 2,261
  • 5
  • 23
  • 46
  • yeah that seems to be the only example out there, but it doesnt take into account the VaryByParams, so if i expire that page i think there'd still be data in the cache for the same page with different query strings. – John Boker Jan 26 '09 at 23:42
1

Your solution didn't work for me. However ... after some testing I got this to work fine. This code will be inside your UserControl Page_Load that needs to be cached.

string key_refresh = "refresh_id_" + YourID;
Cache[key_refresh] = DateTime.Now.ToString();

CacheDependency dep = new CacheDependency(null, new string[] { key_refresh });
this.CachePolicy.Dependency = dep;

For some reason, using Response.AddCacheItemDependency didn't have any effect when I was updating my data from Cache[key_refresh].

In my case I cache my control by user ID, so each user will have a different version of this control with different data, I use VaryByCustom to cache it individually.

some_guy
  • 97
  • 1
  • 8
0

After some more research I found a method that seems to work well.

Dim cachekey As String = String.Format("Calendar-{0}", calendarID)
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
Response.AddCacheItemDependency(cachekey)

that will add a dependency to the page cache object, then to expire I do this:

Dim cachekey as string = String.Format("Calendar-{0}", CalendarID)
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)

Now as long as the dependency cachekey is known a page can be expired.

John Boker
  • 82,559
  • 17
  • 97
  • 130