Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Alan Northam



Cold, Hard, Cache!

Don't get me wrong, I really like the Asp.Net Cache (or HttpRuntime.Cache or Uncle Daddy if you want to call it that... you're a little odd aren't you?), but sometimes it just can't be trusted.  I mean, I just gave you (the cache) my precious object a second ago and now you claim you don't have it?  Did you lose it?  Did you sell it on eBay?  Did you pawn it to support your habit?  Please at least tell me you got a decent price for it. 

One of the great things about the cache is also what makes it so untrustworthy.   Objects stored there are referenced with WeakReference so if your sever starts getting low on memory GC will collect objects from the cache to free some up.  Also take into account that if you put an object in Cache and set it to expire after X minutes, when you go back to retreive it, do you really know how long it's been?

So what we want is a way to tell the cache "I want this specific object (a yellow Tonka dumptruck, metal, not plastic) and if you don't have it, here are instructions on how to get it".

Something like this:

Usage would look like this:

I call it the code equivalent of "Trust but verify".



Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# March 7, 2008 8:06 AM

Will Sullivan said:

Heheh.  I beat you to it.  I've used this pattern for a long time.

statestreetgang.net/post.aspx

Its a great little pattern.  You can also apply it to the session state, view state, or any other "bag" type storage.  One of my fav uses is for session state-scoped singletons...

# March 7, 2008 11:20 AM

anortham said:

Will,

If I'd known we were in a race, I'd have kneecapped you with a pipe :)

# March 7, 2008 3:25 PM

Sam said:

I like your sense of humor. It's funnier than your code, but less useful.

# March 7, 2008 8:57 PM

Cold, Hard, Cache! - Alan Northam « Noocyte’s Weblog said:

Pingback from  Cold, Hard, Cache! - Alan Northam « Noocyte’s Weblog

# March 11, 2008 1:45 PM

Alan Northam said:

Take a look at my previous post for the details on a cache helper method that I like to use. In this

# March 17, 2008 10:54 PM

Luka said:

Hi Alan,

I'm trying to store data table into cache, without much success.

I have a static method that suppose to return datatable in case that it is not already in cache (retrieve method). I admit, I don't know much with anonymous methods, but wander if this pattern should work for datatable types?

# May 23, 2008 3:06 PM

Community Blogs said:

Karl Seguin has an interesting post about using System.Func to fight repetitive code blocks , which actually

# July 7, 2008 9:52 AM

Readed By Wrocław NUG members said:

Karl Seguin has an interesting post about using System.Func to fight repetitive code blocks , which actually

# July 7, 2008 5:25 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Proudly Partnered With