The ASPSmith's Blog

Some rants about ASP.NET by Steven Smith

The Caching Pattern

Here's a little something that I term "the caching pattern" for using the ASP.NET cache object:

Object cacheItem = Cache[key]

as DataTable;

if(cacheItem == null)

{

  cacheItem = GetData();

  Cache.Insert(key, cacheItem, null, DateTime.Now.AddHours(1), TimeSpan.Zero);

}

return (DataTable)cacheItem;

 

The as keyword will try to cast Cache[key] to DataTable and if unsuccessful it will return null.  If it is null, it will return null.  The rest of it is pretty straightforward.  This is the best practice way to get something from the cache, but look at it!  It's huge!  I'm trying to come up with a helper class that will encapsulate this logic in a reusable fashion so that reading something from cache would take only a line or two of code.  I'll post more when I'm done - I think I'm close to having it.

[Listening to: Queens of the Stone Age - No One Knows]

 

Comments

TrackBack said:

HumanCompiler
# June 20, 2003 11:44 PM

Jason Gaylord said:

Great example Steve! I noticed the "as" keyword before but never used it. Let me know when the helper class is done. I have a specific use for it.

Thanks,
Jason
# June 26, 2003 9:54 AM

TrackBack said:

# April 2, 2004 12:36 PM

TrackBack said:

# April 11, 2004 2:56 PM

TrackBack said:

# April 11, 2004 2:57 PM

Gavin Joyce's Blog said:

IntroductionCaching is one of the greatest strategies for improving the performance of our applications....
# May 23, 2006 6:42 PM

Gavin Joyce's Blog said:

IntroductionCaching is one of the greatest strategies for improving the performance of our applications....
# May 23, 2006 6:53 PM

Michael Freidgeim said:

I have posted my "Generic LoadFromCache method to reliably read data from cache"

geekswithblogs.net/.../Generic-LoadFromCache-method.aspx

# October 8, 2007 7:21 AM

Andrew said:

This is great as long as your cache didn't go kafluff on you which is what many developers are falling into. The real pattern that is needed is which caching model is a guaranteed success. Do we have to go with yet another Microsoft Building Block Design Pattern Magical Mystery Tour OR do we expect ASP.Net caching to work out of the box.

# March 11, 2008 4:19 PM

Alan Northam said:

devlicio.us/.../cold-hard-cache.aspx

Trust but verify pattern.

# April 3, 2008 1:08 PM

Sarah on ASP.NET Cache said:

This indeed is the correct way of retrieving a cached object. We have been doing it for a long time and it works like a charm. Good to hear that you are trying to create a helper class. Please do post more when you are finished.

On a side note ;-)

What'll you do when your application needs to scale though since ASP.NET isn't scalable.

All the best.

# October 29, 2008 1:24 PM

Jason said:

You are the biggest idiot on the internet.

How about this. It’s a little something I call “The Looping Pattern”:

int limit = 100; // or however much you want

for (int i = 0; i < limit; i++)

{

     // do something here

}

# July 15, 2009 12:44 PM

Brijesh said:

I do agree with Jason.

# July 31, 2009 12:22 AM

neon tabela said:

Topic Thanks for sharing a very good explanation

# August 29, 2009 2:29 PM

evden eve nakliyat said:

I do agree with Jason.

# March 3, 2010 11:14 AM

medyum said:

Trust but verify pattern.

# May 24, 2010 9:46 AM

guzelsoz said:

Trust but verify pattern

# July 31, 2010 8:33 AM

medyum cenap said:

I am to a great extent impressed with the article I have just read interesting very good…

# August 4, 2010 1:30 PM

gka said:

I also agree with Jason

# November 23, 2010 12:04 PM

ipad accessories prices said:

Nothing is impossible!

-----------------------------------

# December 20, 2010 6:11 AM

buy 26 inch LCD TV said:

Just want to say what a great blog you got here!

I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

Thumbs up, and keep it going!

Cheers

# January 1, 2011 9:26 PM

ipad application said:

-----------------------------------------------------------

"Please, can you PM me and tell me few much more thinks about this, I am seriously lover of the web site.

# January 2, 2011 4:45 PM

Home Security Monitoring said:

Thanks  for another informative web site. Where else could I get that kind of info written in such an ideal way' I've a project that I'm just now working on, and I've been on the look out for such information.

<b><a href="satays.net/information-on-home-security

">Home Security Monitoring

<a/><b/>

# March 30, 2011 1:26 PM

promosyon said:

Your site is very good. There are useful information and most importantly, for sharing great. Thank you.

# April 12, 2011 5:16 AM

promosyon said:

Your site is very good. There are useful information and most importantly, for sharing great. Thank you.

# May 6, 2011 11:27 AM

Medyum said:

Very useful for beginners.I'm writing a new article about the caching pattern..I wil share with you.

Best Regards

# August 8, 2011 9:44 PM

eticaret said:

An interesting page, thanks for the information

# August 19, 2011 9:44 PM

ipad reviews said:

I am to a great extent impressed with the article I have just read interesting very good…

# January 19, 2012 6:00 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)