Simple cache interface and implementations

During a couple of my last .Net projects I used the same cache interface and implementations of it with MemoryCache, System.Web.Caching.Cache and CacheManager.

Visual Studio solution is available here https://github.com/mchudinov/CacheNet. Solution includes source code in both C# and Visual Basic.

1. Interfaces

Same interface in Visual Basic

2. Cache provider base abstract class

It defines to useful configuration parameters:

  • CacheDefaultDurationMinutes – default time to live for cache items
  • CacheKeyPrefix – key prefix for all items

3. Implementation

Implementations for MemoryCache and for System.Web.Caching.Cache are trivial. Source code is available here
https://github.com/mchudinov/CacheNet/blob/master/CacheCSharp/MemoryProvider.cs
https://github.com/mchudinov/CacheNet/blob/master/CacheCSharp/SystemWebProvider.cs

4. Implementation for CacheManager

The important moment with CacheManager is configuration. There are many ways to configure CacheManager, they are described in the documentation. I prefer to configure with a configuration file App|Web.config.

Config file can be transformed while building solution for target environment. With a config file transformations it is easy to implement a scenario with different cache backends in development and in production.

Useful link all valid configurations options in *.config file.

Simple configuration example:

Cache manager factory uses name “cache” to create an instance of a CacheManager with this configuration.

I use the same name in different configuration files (Web.Release.config and Web.Debug.config), for instance just “cache”. But the configuration can be different for development and production.

5. Serialization

Serialization is needed for non serializable objects for distributed (not in-process) cache systems like Redis and Memcached.
Useful link CacheManager serialization documentation.
To enable caching for non serializable objects we need to use CacheManager.Serialization.Json.
Install this package and add the following line to config for this for the cache manager:
serializerType="CacheManager.Serialization.Json.JsonCacheSerializer, CacheManager.Serialization.Json

6. Extension methods

Sync and async extension for cache. Method checks cache value, if value not present then run a method that is passed as parameter and put the returned result into cache with the given key.

Usage: