Question

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.

Was it helpful?

Solution 3

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.

OTHER TIPS

You could try this:

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

From: http://aspalliance.com/668

Thanks.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top