CacheManager - An Open-Source Caching Abstraction Layer for .NET Written in C#

CacheManager - An Open-Source Caching Abstraction Layer for .NET Written in C#

CacheManager is an open-source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.

Last updated 7/8/2022 7:03 AM
黑哥聊dotNet
3 min read
Category
.NET
Tags
.NET C# Open Source

Introduction

CacheManager is an open-source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.

The main goal of the CacheManager package is to make developers' lives easier when dealing with even very complex caching scenarios. With CacheManager, you can implement multi-layer caching, such as in-process cache in front of a distributed cache, with just a few lines of code.

CacheManager is not just an interface that unifies the programming model of various cache providers, which will make it very easy to change caching strategies in the project later. It also provides additional features such as cache synchronization, concurrent updates, serialization, events, performance counters... Developers can choose to opt into these features only when needed.

Feature List

  • A common interface for handling different caching technologies: ICache
  • Configurable
  • Supports different cache providers
  • Serialization can now be configured. Serialization is only required in distributed caches. If no additional serialization package is installed and configured, binary serialization will be used.
  • Atomic value updates using locks or transactions of the distributed cache.
  • Logging: CacheManager comes with an extensible logging API
  • Typed cache interface.
  • Multi-layer: By managing multiple cache handles through CacheManager, you can easily implement hierarchical caching
  • Cache regions: Even if some cache systems do not support or implement cache regions, CacheManager implements that mechanism. This can be used, for example, to group items and remove all of them at once.
  • Statistics: Counters for various cache operations.
  • Performance Counters: In order to be able to examine certain numbers via perfmon, CacheManager supports performance counters per manager instance and per cache handle.
  • Event system: CacheManager fires events for common cache operations: OnGet, OnAdd, OnPut, OnRemove, OnClear, OnClearRegion
  • System.Web.OutputCache implementation: Using CacheManager as the OutputCache provider makes OutputCache very flexible, for example by using a distributed cache like Redis across many web servers.
  • Cache client synchronization implemented using Redis publish/subscribe

Examples

private static void MostSimpleCacheManager()
{
    var config = new ConfigurationBuilder()
        .WithSystemRuntimeCacheHandle()
        .Build();

    var cache = new BaseCacheManager<string>(config);
    // or
    var cache2 = CacheFactory.FromConfiguration<string>(config);
}

private static void EventsExample()
{
    var cache = CacheFactory.Build<string>(s => s.WithDictionaryHandle());
    cache.OnAdd += (sender, args) => Console.WriteLine("Added " + args.Key);
    cache.OnGet += (sender, args) => Console.WriteLine("Got " + args.Key);
    cache.OnRemove += (sender, args) => Console.WriteLine("Removed " + args.Key);

    cache.Add("key", "value");
    var val = cache.Get("key");
    cache.Remove("key");
}

Finally, if you like my articles, please follow me and give a like. I hope the .NET ecosystem gets better and better!

Keep Exploring

Related Reading

More Articles