CacheManager

CacheManager is an open source caching framework for .NET written in C# and is available via NuGet. It supports various cache providers and implements many advanced features.

Simple to start with...

var manager = CacheFactory.Build<string>(
    p => p.WithSystemRuntimeCacheHandle());

Easy to use...

manager.AddOrUpdate("key", "value", _ => "updated value");

var val = manager.Get("key");
manager.Put("key", "another value");

Blog Posts

Documentation

Supported Caching Technologies

Dictionary Handle

A simple but very efficient implementation. This handle is faster than any other in most scenarios and works cross platform.

MemoryCache

Two implementations are available, based on System.Runtime.Caching and based on the new Microsoft.Extensions.Caching.Memory package.

Redis

Redis can be used as an extremely fast and reliable out of process (or distributed) solution.

The Redis implementation is based on the popular StackExchange.Redis package.

Memcached

Another great distributed solution is memcached, which can be used as a layer.

Couchbase

Couchbase is primarily a document database but can also be used for simple caching.

ASP.NET 4

ASP.NET comes with its own in-process memory cache which can be used as one layer.

Our Web NuGet package also comes with a custom OutputCacheProvider.


Features

Strongly Typed

Caching objects or primitives is easy, keep writing your code in a type safe way and you don't have to care about casts or serialization issues.

Configuration

There are diverse options to configure CacheManager:

  • By code with a fluent builder
  • Using System.Configuration (App- or Web.config)
  • Using ASP.NET Core Configuration.

Multiple Layers

The topology can be configured. To, for example, implement a local first level cache in front of a distributed second level, for extremely fast read access.

Safely Update

In distributed scenarios, updating a value must often be handled with care because of concurrency. CacheManager hides the complexity and lets you implement all that in one line of code.

Serialization

The serialization technology, used to serialize values in distributed scenarios, can be configured.

Choose either Binary-, JSON- or Protocol Buffer serialization for example.

Synchronization

When using multiple layers in conjunction with a distributed cache layer, and you have many instances of your app running, you might want the local instances synchronized. The backplane can automatically handle that.

Extensible Logging

Logging can be added to the configuration to log some or all the operations.

Install the Microsoft.Extensions.Logging package, to leverage all the additional features.

Cross Platform

Using the new .NET Core / ASP.NET Core project structure and libraries, CacheManager now has cross platform support and is tested on Windows, Linux and iOS.

Events

The CacheManager interface also provides events, you can subscribe to, for many operations.

Flexible Expiration

Through configuration, you can set a default expiration mode and time for each layer.

The expiration settings can also individually be changed for each item at any point.

Regions / Categories

Specify a region for an item to group keys together and, for example, remove all of them at once.

Statistics / Counters

Gather statistical information and track caching operations in Performance Monitor as needed.

Modular Design

CacheManager comes in many different packages, separating the features and dependencies, which gives you the option and flexibility to install only the features you need.

Scalable

CacheManager is easy to use and start with, but it is also just a few lines of configuration to use a more complex caching strategy without you having to change your code.

Start with a simple in-memory approach and scale out later to Redis or other distributed solutions!

Get Involved

File an issue, suggestion or feature request, or send a PR on GitHub!

You can post any technical questions on Stack Overflow.

Or just leave a message in the comments below!.


Code Samples

Different ways to initialize CacheManager:

The following three examples do actually create the same CacheManager instance. You can decide depending on the situation which method fits your needs.

The first example uses the CacheFactory.

var manager = CacheFactory.Build<string>(
    p => p.WithSystemRuntimeCacheHandle());

Instead you can also create an instance of ConfigurationBuilder...

var config = new ConfigurationBuilder()
    .WithSystemRuntimeCacheHandle()
    .Build();
var manager = new BaseCacheManager<string>(config);
// or
var manager = CacheFactory.FromConfiguration<string>(config);

Or you instantiate the CacheManagerConfiguration and call the Builder property to modify the configuration.

var manager = new BaseCacheManager<string>(
    new CacheManagerConfiguration()
        .Builder
        .WithSystemRuntimeCacheHandle()
        .Build());

Load the Configuration from JSON:

This example uses the Microsoft.Extensions.Configuration and the CacheManager extensions.

var jsonConfiguration =
    new Microsoft.Extensions.Configuration.ConfigurationBuilder()
        .AddJsonFile("cache.json")
        .Build();
var manager = new BaseCacheManager<string>(
    jsonConfiguration.GetCacheConfiguration());

Using In-Memory and Redis:

This is a more complex configuration using two layers of in-memory and distributed caching. It also uses a cache backplane.

var manager = CacheFactory.Build<int>(settings =>
{
    settings
        .WithSystemRuntimeCacheHandle()
        .And
        .WithRedisConfiguration("redis", config =>
        {
            config.WithAllowAdmin()
                .WithDatabase(0)
                .WithEndpoint("localhost", 6379);
        })
        .WithMaxRetries(100)
        .WithRetryTimeout(50)
        .WithRedisBackplane("redis")
        .WithRedisCacheHandle("redis", true);
});
manager.Add("test", 123456);

Configuring Microsoft.Extensions.Logging:

If the packages are installed, you can use the new Microsoft.Extensions.Logging framework to let CacheManager log common messages and errors to any kind of log targets.

var config = new ConfigurationBuilder()
    .WithMicrosoftLogging(l => l.AddConsole(LogLevel.Information))
    .WithSystemRuntimeCacheHandle()
    .Build();