Caching in asp.net

Caching in asp.net

Old forum URL: forums.lhotka.net/forums/t/3376.aspx


ajj3085 posted on Tuesday, August 14, 2007

Hi all,

I started as a web developer, but haven't done asp.net development since about 03.  I'm currently working on a new asp.net site, with .Net 2.0.   

I have some NVLs that I want to cache site wide.  The BL should make this transparent (so that if an admin changes the list it can be invalidated).  In my WinForms app, I'm using the EntLib 2.0 Cache manager. 

Should I continue to use this?  Or should I use the Cache from the System.Web assembly?  A third option that would be better for use in asp.net?  One final thing; I only use the null backing source for the EntLib caching... any other configuration ideas for this would be welcome.

Thanks
Andy

skagen00 replied on Tuesday, August 14, 2007

One thing we did was implement a CacheManager, which gets its CacheProvider based on a configuration setting. We wrote a WebCacheProvider for use in Web applications, and you could equally write some WinCacheProvider or something more specific (EntLibCacheProvider, etc). Our WebCacheProvider uses the ASP.Net caching, of course. (CacheManager and CacheProvider are just classes we created and named, of course).

Just a suggestion as you don't have to use the same caching scheme if you don't want to.

Chris

 

ajj3085 replied on Tuesday, August 14, 2007

I wrote a similar wrapper for the EntLib caching I'm using.  the nice thing about entlib is that you can change it via configuration settings.

It sounds like you use the asp.net caching.  Is there any reason you didn't use the caching provided by EntLib?  Just curious.

JoeFallon1 replied on Tuesday, August 14, 2007

Andy,

A couple of things to consider.

1. Use the ASP.Net cache.

2. Only cache Read Only objects that are not changed by users. I cache NVLs all the time.
I tried caching a read/write object and served up a clone from the cache but it is not a deep clone and many related BOs inside of it were "changed" and then put back in the cache so users were seeing data input by other users. Overall - a bad idea.

3. Depending on your technology stack you may be able to use SQL Server 2005 database cache invalidation through publish/subscribe.

4. If not, it is fairly straighforward to write your own database cache invalidation mechanism. There are many articles on the topic. I went with a polling solution instead of the "touch a file" solution. The trick is to set up a cache dependency object and then make your NVL in the cache related to it. When the DB is "changed" the dependency object is flushed from the cache (on the next poll interval) and it takes your NVL with it.

Any DB table that you want to track needs a small trigger attached to it that can call a SP to insert a row in a tracking table. The tracking table has 2 columns: tablename and changeid. The SP simply increments changeid for the tablename that was modified. The polling code just selects this tracking table and compares the stored changeid value to the current changeid value. If they are the same, the table has not been modified. If they are different, the table has been modified and the cache dependency object is removed from the cache taking your NVL with it.

Joe

 

 

ajj3085 replied on Tuesday, August 14, 2007

JoeFallon1:
1. Use the ASP.Net cache.


Sounds like two recommendations for asp.net cache.

JoeFallon1:
2. Only cache Read Only objects that are not changed by users. I cache NVLs all the time.
I tried caching a read/write object and served up a clone from the cache but it is not a deep clone and many related BOs inside of it were "changed" and then put back in the cache so users were seeing data input by other users. Overall - a bad idea.

No, wasn't planning on caching any editable objects.

JoeFallon1:
3. Depending on your technology stack you may be able to use SQL Server 2005 database cache invalidation through publish/subscribe.

Thats one I had forgotten about.  How have you integrated this with Csla (if you have)?  I have a DAL which hides the db server specifics, so this might be a bit more challenging, but I'm sure I could figure something out.

JoeFallon1:
4. If not, it is fairly straighforward to write your own database cache invalidation mechanism. There are many articles on the topic. I went with a polling solution instead of the "touch a file" solution. The trick is to set up a cache dependency object and then make your NVL in the cache related to it. When the DB is "changed" the dependency object is flushed from the cache (on the next poll interval) and it takes your NVL with it.

Any DB table that you want to track needs a small trigger attached to it that can call a SP to insert a row in a tracking table. The tracking table has 2 columns: tablename and changeid. The SP simply increments changeid for the tablename that was modified. The polling code just selects this tracking table and compares the stored changeid value to the current changeid value. If they are the same, the table has not been modified. If they are different, the table has been modified and the cache dependency object is removed from the cache taking your NVL with it.


I'd rather not roll my own, and instead stick with proven solutions (System.Web.Caching or the EntLib Caching AB).

Thanks!
Andy

Copyright (c) Marimer LLC