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?
Caches are used to store temporary files, using hardware and software components. An example of a hardware cache is a CPU cache. This is a small chunk of memory on the computer's processor used to store basic computer instructions that were recently used or are frequently used.
In computing, a cache is a high-speed data storage layer which stores a subset of data, typically transient in nature, so that future requests for that data are served up faster than is possible by accessing the data's primary storage location.
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.
Have you looked at: Domain Objects Caching Pattern for .NET
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With