EasyCache.NET is basically as very thin wrapper over the System.Web.Caching.Cache object with the modest purpose of simplifying the number of lines of code required for the most common use cases when caching objects.

EasyCache implements a static Cache class which detects whether the application is within ASP.NET context or not and internally loads the System.Web.Caching.Cache from the appropriate source. It also allows for a configurable cache duration that frees the developer from having to set the duration on each add/insert to the cache (or accept that NoAbsoluteExpiration default).

The most significant method is the Cache.Get. The developer no longer needs to pull an item from the cache, cast it, check for null, perform some object loading logic, and place the object back in the cache. This is very repetitive boilerplate code. Instead the developer can simply specify the key, and a Delegate pointing to the method that should be called in the case that the item is not found in the cache, and it returns a strongly typed value that doesn't need to be cast.

The Cache.Get method handles the boilerplate code and conditionally executes the delegate if necessary to populate/re-populate the cache.

There are several unit tests included in the source detailing the syntax. The following is one such sample:

public void CanGetFromCacheWithDelegatePassingParameter()
const string CACHE_KEY = "CanGetFromCacheWithDelegatePassingParameter";
const int TEST_VAL = 5;
var obj = Cache.Get(CACHE_KEY, () => ObjectToCache.Retrieve(TEST_VAL));
Assert.AreEqual(TEST_VAL, obj.Id);

The latest version also includes:
A simple provider model. By implementing the ICacheProvider interface you'll be able to swap Cache implementations via configuration. I've provided two implementations; the MemoryCache which uses the standard .NET memory cache, and the SerializedMemoryCache which also uses that standard .NET memory cache but serializes objects in and out of the cache to simulate a distributed cache.

Last edited Jun 4, 2011 at 11:27 AM by jnappi, version 2


No comments yet.