Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching in C# without System.Web

Tags:

c#

caching

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?

like image 553
kerbou Avatar asked Sep 29 '09 11:09

kerbou


People also ask

What is caching with example?

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.

What is caching in programming?

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.


2 Answers

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.

like image 114
Christian Hayter Avatar answered Sep 29 '22 02:09

Christian Hayter


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

like image 34
Mitch Wheat Avatar answered Sep 29 '22 00:09

Mitch Wheat