Clever caching using generics?

Are there any clever layers out there to sit on top of the System.Web.Caching.Cache class to handle simple scenarios to fetch items using generics.

I'd like to maybe provide a delegate function to retrieve an item, and be able to fetch an item with a strongly typed GetItem and a delegate function to retrieve an item if it isnt found.

For instance

 CleverCache<K, T> 
 T GetItem(K)


I'd also like a nice way to handle a cache that only ever handles a single object.

The ASP.NET Cache object just seems really primitive. It just seems like a hashtable with a timeout. They may as well just call it HashtableWithTimeout for all I care. I want some better astraction. Is this the kind of thing that the caching application block will do for me?

Yes I know this is fairly simple code, i just dont have time for any wheel reinventing right now.

13.10.2009 23:36:22

You may want to look into SharedCache and Microsoft Velocity as improvements to the basic caching available in ASP.NET. I don't know how extensively strong typing is designed into either - but from the samples and documentation I've seen it's better than the ASP.NET Cache.

14.10.2009 00:10:37
Velocity and SharedCache are great products, but they solve a very specific class of problems, of which this question is not a part.
Rex M 14.10.2009 01:11:37
A better way to put it might be that you're recommending OP goes from a bicycle to a city bus system when he really just needs a small car.
Rex M 14.10.2009 01:18:23

You may want to look into some more sophisticated cache providers that can wrap the ASP.NET cache, such as the Enterprise Library's caching block. It provides a much more robust and rich caching story for object lifecycle, invalidation, retrieval, etc.

The ASP.NET cache deserves more credit than you give it - it's actually a very powerful base. Alone, it doesn't do anything complex as you have pointed out, but it is well-designed and robust enough to support some pretty cool stuff.

14.10.2009 01:13:57
I wasn't aware that the EL caching block had any support for type safe caching or generics.
LBushkin 14.10.2009 02:28:12
@Lbushkin it doesn't, but it does provide a framework that more easily allows item fetch delegation, which is at the core of what OP is describing. Adding generics is as simple as a cast.
Rex M 14.10.2009 02:55:07