Janus Pienaar

Made in South Africa

Implementing caching in ASP.NET 2.0

I'm in the process of adding some caching functionality to my ASP.NET 2.0 project, but can't decide which route to take when it comes to the caching mechanisms to use.

The 2 mechanisms that stand out at the moment are:

1. System.Web.Caching - Seems straight forward and easy to implement.
2. Microsoft.Practices.EnterpriseLibrary.Caching - Looks a bit more complex but with added functionality. I like the idea of a CacheManager.

And then there is the issue to sync across several web farms.

What would be the best way to implement concurrency between server?

These are the 2 possibles i'm playing with at the moment:

1. When something changes on an object, you could set a flag in the Database and check this flag everytime the object is requested from cache.
2. Send a HTTPWebRequest to all servers in a web farm requesting them to update their cache.

Any ideas? Any recommendations? Best practices?

If anybody has some experienced with working on one or perhaps both mechanisms, I would love to hear from you.

Posted: Nov 01 2006, 04:37 PM by janus | with 19 comment(s)
Filed under:

Comments

markn said:

Hi,

I have done some extensive work on caching for a very large enterprise call centre application. Here are my recommendations:

1. Use the System.Web.Caching.Cache class where-ever possible. In fact Microsoft recommends this too. The only time Microsoft suggests that you use the Enterprise Library cache instead is when you need to persist the cached information to a backing store, e.g. in an offline scenario (true smart client that must be able to operate with offline data - NOT click once "smart client").

2. The Enterprise Library caching block has some interesting limitations. One I can think of off the top of my head is the default scavenging algorithm. Scavenging is the process of removing cache items early in order to free memory, when needed, before the expiry time of certain cache items. What the default scavenging algorithm does in Enuterprise Library is free by number of items not size. Thus if you have 2 1GB items they will not be scavenged, but 20000 1kilo-byte items will be scavenged - not very intelligent.

3. The System.Web.Caching.Cache is not without it's own problems, but at least you have options now on .NET 2.0. Before the memory usage was fixed at 90% of available RAM. Now you can configure all these things via the xml config files.

4. The System.Web.Caching.Cache class has nice functionality to support SQL 2005 notification services via SqlDependency. This way the cache is automatically refreshed whenever the underlying data is changed. You do this via two mechanisms, the SqlCacheDependency class and a callback to refill the cache when items are removed due to change.

5. The System.Web.Caching.Cache can safely be used outside of IIS and ASP.NET. There is a KB article on this from Microsoft; which means you can use the same caching mechanism in Windows Forms clients.

6. One gotcha is ... don't "cache" the Cache object. e.g. HttpRuntime.Cache property return value. Always access the cache directly by HttpRuntime.Cache. This is because the internal reference can change.

7. You talk about syncing cache across server farms. This makes alarm bells go off in my head. You need to be very clear on the distinction between CACHE and STATE or SESSION data. STATE or SESSION data cannot be lost and therefore must be synced across a farm. Cache data must always be thought of as throw-away. The cache must be able to be recycled at anytime for any reason. Thuse there is never a need to sync cache across server farms, since each server in the farm will build up and manage it's own cache. State in an application SHOULD NEVER be treated as cache data. This must be stored either in the Session object or the page ViewState.

Hope this little bit of info helps... there is just so much more, but those are the most important. If I can summarize with the most important point is the distinction between what is cache and what is session data and how the two operate on completely different conceptual models. Cache is always throw-away and never synced.

# November 1, 2006 8:09 PM

janus said:

Hey Mark

Thank you for your reply, I really appreciates it.

I did some more reading, and agree that I should go the System.Web.Caching route.

Please picture my next scenario:

I have a object called "Schedule", now this have some properties like name, date etc.

But it also hold a collection of "Employees" and "Duties". "Duties" hold a collection of "Tasks".

This takes a while to load up for the first time, so I would like to cache the whole object model.

It would be read from very often, but could be updated from time to time.

Now the problem offcourse is, that the changes to the object in cache will not be synced across web farms.

Would you say that I should rather store this in Session than Cache? Will the Session be synced across web farms and does this happen automaticly?

# November 1, 2006 11:36 PM

janus said:

To answer my own question, Session is stored on a per user basis, so this won't work for us.

I can't use Application state as this is basicly a global vairable for a ASP.Net application.

I will have to add the "Schedule" object to cache to enable this to be used for all users across all servers.

# November 2, 2006 11:38 AM

pctfbk said:

ucDDBf  <a href="hefchbevacbb.com/.../a>, [url=http://feyeeczbjrdz.com/]feyeeczbjrdz[/url], [link=http://uvpuhqwvzsfn.com/]uvpuhqwvzsfn[/link], http://kjzehtcttmaz.com/

# April 15, 2008 1:06 AM

bactroban said:

# July 6, 2008 2:32 PM

Aiakos said:

Nice site!

<a href="psychology-study.bestuseful.com/map.html"> psychology study </a>

<a href="motorcycle-repair.bestuseful.com/map.html"> motorcycle repair </a>

<a href="orange-county-credit-union.bestuseful.com/map.html"> orange county credit union </a>

<a href="cheap-hotel-in-rome.bestuseful.com/map.html"> cheap hotel in rome </a>

<a href="data-recovery.bestuseful.com/map.html"> data recovery </a>

Enjoy!

# July 30, 2008 3:16 PM

abdxyd said:

w9UVd0  <a href="qnugtwmfsity.com/.../a>, [url=http://habltsgjrzpz.com/]habltsgjrzpz[/url], [link=http://iogolgwcblqe.com/]iogolgwcblqe[/link], http://xrurgjqmpsrg.com/

# August 3, 2008 12:53 PM

ruitauxaiq said:

MV2eQs  <a href="igtcaiycptwc.com/.../a>, [url=http://lkjasdnpxenm.com/]lkjasdnpxenm[/url], [link=http://gjocbwqtdfhw.com/]gjocbwqtdfhw[/link], http://ksgxtmbtkqpt.com/

# August 24, 2008 3:51 PM

adalat side effects said:

ZZns5r adalat side effects

# August 25, 2008 12:21 PM

adalat side effects said:

Focg4R accutane

# August 25, 2008 1:22 PM

rowjjcalv said:

ipVyS8  <a href="njtnpfyjyrjc.com/.../a>, [url=http://zugfepswmgmp.com/]zugfepswmgmp[/url], [link=http://olnsnfvvwkal.com/]olnsnfvvwkal[/link], http://dbtelfqprxxu.com/

# August 25, 2008 3:16 PM

avazwvviv said:

qoIZNC  <a href="yxgcirphmisk.com/.../a>, [url=http://vtwzepnqfqxo.com/]vtwzepnqfqxo[/url], [link=http://lfhmsrwizqei.com/]lfhmsrwizqei[/link], http://figmdzkrjiln.com/

# August 25, 2008 4:21 PM

aquarium eclipse lighting said:

comment6, felzjpvxj09.wikispaces.com/anson+money+clips anson money clips,  =OOO, sbsxlurok68.wikispaces.com/acoustic+guitar+clip+art acoustic guitar clip art,  >:-[[, hfvyeabpy78.wikispaces.com/animal+planet+video+clips animal planet video clips,  tmsaq, xapegkphk52.wikispaces.com/50s+clipart 50s clipart,  2052, ghrpagphc23.wikispaces.com/98+mitsubishi+eclipse+pictures 98 mitsubishi eclipse pictures,  wjnypp, ghrpagphc23.wikispaces.com/alan+partridge+clips alan partridge clips,  bcxyn,

# August 26, 2008 10:23 AM

art body building clip said:

comment6, felzjpvxj09.wikispaces.com/ant+clip+art ant clip art,  876189, mixvriuts87.wikispaces.com/1998+mitsubishi+eclipse+floor+mats 1998 mitsubishi eclipse floor mats,  =[, jjvglqkvi20.wikispaces.com/39+pounds+of+love+clip+with+doctor 39 pounds of love clip with doctor,  fuogd, cqqmwsfxp70.wikispaces.com/animated+clip+art+weapon+and+ammunition animated clip art weapon and ammunition,  44373, wvzsgavks34.wikispaces.com/2002+mitsubishi+eclipse+gts 2002 mitsubishi eclipse gts,  umwpa, ihrlsvuxw72.wikispaces.com/02+mitsubishi+eclipses 02 mitsubishi eclipses,  9523,

# August 26, 2008 8:16 PM

clip art painting said:

comment3, rswvuscgyh66.wikispaces.com/clip+hunter.com clip hunter.com,  yfxrnw, teyocllzew21.wikispaces.com/celine+dion+rare+video+clips celine dion rare video clips,  883679, amawkpfugw73.wikispaces.com/clip+art+explosion clip art explosion,  :-[[, zumzgfnkdo95.wikispaces.com/car+crash+clips car crash clips,  yje, dlndkuxuqb26.wikispaces.com/clip+art+of+curious+george clip art of curious george,  keabo, khowvxoadd79.wikispaces.com/chinese+clip+art chinese clip art,  8[,

# August 28, 2008 12:36 AM

clips brianna banks said:

comment2, iwmdnefhwn24.wikispaces.com/cliptrakker cliptrakker,  099, ucbwgfxhzj76.wikispaces.com/clippered+napes clippered napes,  76051, lphprpkjco96.wikispaces.com/computer+clipart+pictures computer clipart pictures,  19225, vxwjcgjcww98.wikispaces.com/clip2.mpg clip2.mpg,  8-], lcijqyavzp82.wikispaces.com/colin+firth+video+clips colin firth video clips,  8), xdvuxckset63.wikispaces.com/connecting+plastic+straws+metal+paper+with+clips connecting plastic straws metal paper with clips,  pcgumg,

# August 28, 2008 2:32 AM

disney kid clip said:

comment5, wvzsgavksj42.wikispaces.com/earth+quake+video+clip earth quake video clip,  mxkb, pebhhvmgez79.wikispaces.com/dsm+eclipse dsm eclipse,  kotbsb, owgfqtyxsp58.wikispaces.com/eastenders+video+clip eastenders video clip,  hkzib, cldgnzdthh43.wikispaces.com/eclipse+gun+company eclipse gun company,  1152, wvzsgavksj42.wikispaces.com/eclipse+ego+06 eclipse ego 06,  902446, ldgnzdthhl37.wikispaces.com/elmer+fudd+sound+clips elmer fudd sound clips,  jllthv,

# August 28, 2008 9:55 AM

Dighenis said:

Hi, <a href="destor.nookiehost.com/.../california-health-insurance-leads.html"">destor.nookiehost.com/.../california-health-insurance-leads.html">california">destor.nookiehost.com/.../california-health-insurance-leads.html">california health insurance leads</a>, [url="destor.nookiehost.com/.../california-health-insurance-leads.html"">destor.nookiehost.com/.../california-health-insurance-leads.html"]california health insurance leads[/url], destor.nookiehost.com/.../california-health-insurance-leads.html california health insurance leads,  tpxc,

# August 31, 2008 8:15 AM

Dimitrios said:

hqV52l Nice article…, <a href="great-vegan-main-dishes.supertodayusa.info/index.html"">great-vegan-main-dishes.supertodayusa.info/index.html">great">great-vegan-main-dishes.supertodayusa.info/index.html">great vegan main dishes</a>, [url="great-vegan-main-dishes.supertodayusa.info/index.html"">great-vegan-main-dishes.supertodayusa.info/index.html"]great vegan main dishes[/url], great-vegan-main-dishes.supertodayusa.info/index.html great vegan main dishes,  =-),

# August 31, 2008 4:08 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: