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
2009-09-29 11:21:06
Looks promising - thanks :o)
2009-09-29 21:02:56
"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
2009-11-24 19:11:33