tags:

views:

427

answers:

2

I want to be able to cache a few objects without referencing System.Web. I want sliding expiration and little more... Is there really no where to go but to build my own using a Dictionary and some selfmade expiration of objects - or are there something in the .NET framework I've totally missed out on?

+4  A: 

You could try the Microsoft Enterprise Library Caching Application Block.

Example utility function for caching on demand with a sliding timeout:

static T GetCached<T>(string key, TimeSpan timeout, Func<T> getDirect) {
    var cache = CacheFactory.GetCacheManager();
    object valueCached = cache[key];
    if(valueCached != null) {
        return (T) valueCached;
    } else {
        T valueDirect = getDirect();
        cache.Add(key, valueDirect, CacheItemPriority.Normal, null, new SlidingTime(timeout));
        return valueDirect;
    }
}

You can specify multiple expiration policies, including SlidingTime, FileDependency, ExtendedFormatTime, or you can write your own.

Christian Hayter
Looks promising - thanks :o)
"In my opinion that block has way too many moving parts. Using the ASP.NET Cache is fairly lightweight, and includes the very useful cache dependancies stuff."- Scott Hanselmanhttp://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx
Merritt
+2  A: 

Have you looked at: Domain Objects Caching Pattern for .NET

Mitch Wheat