CacheManager Documentation

repository·dev·Indexed 25 days ago

https://github.com/michaco/cachemanager

An open source caching abstraction layer for .NET that unifies various caching technologies under a single interface. It supports multi-layered caching (e.g., in-process and distributed), automatic synchronization via Redis pub/sub, and multiple serialization formats including JSON, Bond, and Protocol Buffers. Compatible with .NET Standard 2.0, .NET 8.0, and .NET Framework 4.7.2.

Tokens
3.7K
Snippets
8
Records
16
Agent score
82%

What's inside CacheManager

  1. How layered caching and synchronization works

    dev

    When using multiple cache handles (layers), CacheManager ensures data consistency:

    • Put and Add operations: These are automatically executed on all registered cache handles in the manager.
    • Get operations: Behavior is controlled by CacheUpdateMode:
      • None: No update occurs across handles on a hit.
      • Up: Updates the handles "above" the current one in the hierarchy.
      • All: Updates/Adds the item to all handles in the manager.
    • Synchronization: For distributed environments, CacheManager can use Redis pub/sub to synchronize cache clients.
  2. Core Concepts of CacheManager

    dev

    CacheManager provides a unified abstraction layer for .NET caching. Key concepts include:

    • ICache<T>: The primary strongly-typed interface used to interact with the cache.
    • Layered Caching: You can implement multiple layers (e.g., an in-process memory cache in front of a distributed Redis cache). CacheManager manages synchronization between these layers.
    • Serialization: Required for distributed caches. If no specific serialization package is configured, it defaults to Binary serialization (if the full CLR is available).
    • Cache Regions: A mechanism to group cache elements, allowing for bulk operations like removing all elements in a specific region.
  3. Access CacheManager API Documentation

    dev
    The full API documentation for CacheManager, including detailed descriptions of classes, interfaces, and methods, is hosted as HTML documentation. You can access the core API reference to understand the available caching abstractions and provider implementations.
  4. Install CacheManager via NuGet

    dev

    CacheManager is distributed via NuGet packages. To use the core functionality, install CacheManager.Core. Depending on your requirements, you will also need provider-specific packages (like Redis or MemoryCache) and serialization packages for distributed caching.

    Supported frameworks include .NET Standard 2.0, .NET 8.0, and .NET Framework 4.7.2.

  5. Create a JSON cache configuration file

    dev

    To define your cache settings in a JSON file, use the cachemanager.json schema. This allows you to leverage IntelliSense and validation in your IDE. The configuration root should contain a cacheManagers array, where each entry defines a cache manager's name and the providers it handles (using knownType).

    {
      "$schema": "http://cachemanager.michaco.net/schemas/cachemanager.json",
      "cacheManagers": [
        {
            "name": "MyCache",
            "handles": [ { "knownType": "SystemRuntime" } ]
        }
      ]
    }
  6. Create a CacheManager instance using CacheFactory.Build

    dev

    You can use the CacheFactory.Build overload that accepts a name and an Action<ConfigurationBuilderCachePart> to configure and instantiate a new CacheManager. This allows for a fluent configuration of cache settings, such as update modes, dictionary handling, performance counters, and expiration policies, within a single lambda expression.

    var cache = cachefactory.build("mycachename", settings =>
    {
       settings
           .withupdatemode(cacheupdatemode.up)
           .withdictionaryhandle()
               .enableperformancecounters()
               .withexpiration(expirationmode.sliding, timespan.fromseconds(10));
    });
    
    cache.add("key", "value");
  7. Configure CacheManager to use JSON serialization

    dev

    By default, CacheManager uses a Binary serializer. You can switch to JSON serialization by calling .WithJsonSerializer() on the Core.ConfigurationBuilder.

    This allows you to use JSON for (de)serialization of cached items. You can optionally provide specific Newtonsoft.Json.JsonSerializerSettings for both serialization and deserialization to customize how objects are handled.

    var builder = new Core.ConfigurationBuilder();
    
    // Basic usage: replaces default Binary serializer with JSON
    builder.WithJsonSerializer();
    
    // Advanced usage: specify custom Newtonsoft.Json settings for serialization and deserialization
    builder.WithJsonSerializer(new JsonSerializerSettings(), new JsonSerializerSettings());
  8. Retrieve CacheManager configuration using Microsoft.Extensions.Configuration

    dev

    You can integrate CacheManager configuration with the standard .NET Microsoft.Extensions.Configuration ecosystem. To use configuration providers like JSON or XML, ensure you have installed the corresponding Microsoft.Extensions.Configuration.* NuGet packages.

    Use the GetCacheConfiguration() extension method on an IConfiguration object to retrieve the CacheManager-specific configuration settings.

    // setting up the configuration providers
    var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
        .AddJsonFile("cache.json");
    
    // build the configuration
    this.Configuration = builder.Build();
    
    // retrieve the CacheManager configuration
    var jsonConfiguration = 
        this.Configuration.GetCacheConfiguration();
  9. Configure Serialization options

    dev

    For distributed caching, you can opt-in to various serialization formats by installing the corresponding package. Supported options include:

    • Binary: Built-in (requires full CLR).
    • Json: Uses Newtonsoft.Json.
    • Json with Gzip: JSON serialization with compression.
    • Bond: Based on Microsoft.Bond.
    • DataContract: Based on System.Runtime.Serialization (supports Binary, Json, and Json with Gzip).
    • Protocol Buffer: Uses Google's protobuf via the protobuf-net implementation.
  10. Serializer performance comparison

    dev

    Benchmarks comparing various serializers using a complex object (containing Guids, lists, and nested objects).

    Key findings:

    • BondBinarySerializer and BondFastBinarySerializer are the fastest.
    • ProtoBufSerializer also performs very well.
    • JsonSerializer is a baseline.
    • JsonGzSerializer (compressed JSON) has significant compression overhead and is slower than standard JSON.
    | Method                   | Mean      | Error    | StdDev   | Ratio | RatioSD | Gen0    | Gen1    | Allocated | Alloc Ratio |
    |------------------------- |----------:|---------:|---------:|------:|--------:|-------:|--------:|----------:|------------:|
    | JsonSerializer           |  83.21 us | 1.163 us | 0.769 us |  1.00 |    0.01 | 14.8926 | 2.4414 | 191.16 KB |        1.00 |
    | JsonGzSerializer         | 346.37 us | 4.785 us | 3.165 us |  4.16 |    0.05 | 21.9727 | 2.9297 | 280.75 KB |        1.47 |
    | ProtoBufSerializer       |  39.18 us | 0.701 us | 0.463 us |  0.47 |    0.01 |  8.6060 | 1.0986 |  110.7 KB |        0.58 |
    | BondBinarySerializer     |  19.03 us | 0.398 us | 0.263 us |  0.23 |    0.00 |  4.7302 | 0.6714 |  60.53 KB |        0.32 |
    | BondFastBinarySerializer |  19.42 us | 0.431 us | 0.285 us |  0.23 |    0.00 |  4.7607 | 0.7324 |  60.84 KB |        0.32 |
    | BondSimpleJsonSerializer |  63.56 us | 1.132 us | 0.748 us |  0.76 |    0.01 | 11.9629 | 1.9531 | 153.35 KB |        0.80 |
  11. Performance characteristics of Put operations

    dev

    For Put operations (putting 1 item per run), Redis performs much closer to in-memory handles. This is because CacheManager uses a 'fire and forget' pattern for Put, as the operation does not need to return whether the item was added or updated.

    | Method     | Mean        | Error        | StdDev       | Ratio | RatioSD | Gen0    | Gen1    | Allocated | Alloc Ratio |
    |----------- |------------:|-------------:|-------------:|------:|--------:|-------:|--------:|----------:|------------:|
    | Dictionary |     95.01 ns |     1.888 ns |     1.249 ns |  1.00 |    0.02 | 0.0122 |      - |     160 B |        1.00 |
    | Runtime    |    887.35 ns |    19.929 ns |     1.381 ns |  9.34 |    0.18 | 0.4263 | 0.0095 |    5576 B |       34.85 |
    | MsMemory   |    172.40 ns |     5.030 ns |     3.327 ns |  1.81 |    0.04 | 0.0336 |      - |     440 B |        2.75 |
    | Redis      |  4,136.37 ns |    301.420 ns |   199.371 ns | 43.54 |    2.07 | 0.0839 | 0.0610 |    1095 B |        6.84 |