Click to See Complete Forum and Search --> : How To Force Expire Cache


wackoyacky
10-11-2006, 10:55 PM
Hi!

I was wondering how in what way could I force the cache to expire? On my development machine what I would just do is just stop Cassini/ restart IIS and run the web app again. This works fine. But the problem is I cannot do this in our live server.

The cached objects were usually lookup tables that don't change very often.

Thanks,
Enzo

felgall
10-12-2006, 03:11 AM
You could add the no-cache header so it doesn't get cached in the first place and then comment that header out just before you run the final tests.

wackoyacky
10-12-2006, 03:46 AM
Hi! It's not that I want to do the forcing of cache to expire on dev systems but also on live systems.

Let's say I have this look up data(for ex "Status") that is very very rare that get's updated and doesn't have any UI for add/edit. I'll set it's sliding expiration to 1hr. Now there will come a time that I need to update this, let's say there's a new Status that they want to add, so what I would do is goto to the database then insert this new record. Now the thing is, this changes would only appear to the UI if the web application will be idle for an hour since that's what is set on the sliding expiration.

So basically what I need is a way to force expire this cache on this very rare occasion.

Thanks,
Enzo

sirpelidor
10-12-2006, 06:03 AM
sound like you want to cache and reuse data from SQL Server until that data has been updated.

From your pervious posts, i take that you are using 2.0, not 1.x, therefore you should be able to utilize SQL Cache invalidation support.

What it does, is to allow your application to display the most up to date information possible, while simultaneously making sure your application queries the database as infrequently as possible.

this article (http://quickstarts.asp.net/QuickStartv20/aspnet/doc/caching/SQLInvalidation.aspx#polling) has the detail.

p.s: if you use sql2k5, go ahead and skip the top section, and scroll all the way down to the 2005 section.

wackoyacky
10-12-2006, 10:59 AM
hi sirpelidor nice to see you again :)

I do know of the SQL dependency cache but I do also have applications that is using .NET 1.1 framework.

I was only wondering if there is any simple way of doing this manually. Actually the only way I can think of as the moment was to create a dependency file.

Thanks,
Enzo

sirpelidor
10-12-2006, 01:33 PM
I was only wondering if there is any simple way of doing this manually

I would use the Cache Class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebcachingcachememberstopic.asp) if I want to handle it manually.

Cache is a static object that use across the same application domain, you use the Insert method add a dataset to cache:

public DataSet getDataSet(){//ur sql code goes here}
public void CacheDataSet(){
Cache.Insert ("MyCachedDataSet", this.getDataSet(), null,
Cache.NoAbsoluteExpiration, TimeSpan.FromHours (1),
CacheItemPriority.Default,
new CacheItemRemovedCallback (refreshMyDataSet));
}//end CacheDataSet

private void refreshMyDataSet(String key, Object item,
CacheItemRemovedReason myReason){

cache.Insert ("MyCacheDataSet", this.getDataSet(), null,
Cache.NoAbsoluteExpiration, TimeSpan.FromHours (1),
CacheItemPriority.Default,
new CacheItemRemovedCallback (refreshMyDataSet));
}//end refreshMyDataSet


-the 4th parameter is telling cache when to expire....
-the 7th parameter is a delegate call back function...which allows u to call itself to refresh upon cache expire...

once we have the cache in place, we can now manually update the cache:

public void updateMyCache(){
Cache.Remove("MyCacheDataSet");
this.CacheDataSet();
}//end updateMyCache


detail can be found in this article (http://datagrid.codebetter.com/blogs/john.papa/archive/2005/04/08/61697.aspx) .

here's another article (http://www.developer.com/net/net/article.php/1477771) shows you how to apply at global.ascx if you want to do it when application_start() ....

hope that helps.