CasCap.Common.Caching 4.9.0

Prefix Reserved
dotnet add package CasCap.Common.Caching --version 4.9.0
                    
NuGet\Install-Package CasCap.Common.Caching -Version 4.9.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CasCap.Common.Caching" Version="4.9.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CasCap.Common.Caching" Version="4.9.0" />
                    
Directory.Packages.props
<PackageReference Include="CasCap.Common.Caching" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CasCap.Common.Caching --version 4.9.0
                    
#r "nuget: CasCap.Common.Caching, 4.9.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CasCap.Common.Caching@4.9.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CasCap.Common.Caching&version=4.9.0
                    
Install as a Cake Addin
#tool nuget:?package=CasCap.Common.Caching&version=4.9.0
                    
Install as a Cake Tool

CasCap.Common.Caching

Multi-tier distributed caching library implementing the cache-aside pattern with support for Memory, Disk, and Redis cache layers.

Purpose

Provides a complete caching infrastructure with local (in-process) and remote (Redis) cache tiers, synchronised expiration via Redis Pub/Sub, and configurable serialization (JSON or MessagePack). Includes background services for cache expiry management, an async duplicate-lock mechanism to prevent thundering-herd scenarios, and optional Redis-based distributed locking via RedLock.net.

Target frameworks: netstandard2.0, net8.0, net9.0, net10.0

Services

Service Description
DistributedCacheService Multi-tier cache orchestrator — reads from local first, falls back to Redis, and populates both tiers on miss. Supports opt-in Redis-based distributed locking (Redlock) to prevent thundering herd across instances
RedisCacheService IRemoteCache implementation wrapping StackExchange.Redis with optional Lua script support
MemoryCacheService ILocalCache implementation backed by IMemoryCache
DiskCacheService ILocalCache implementation persisting cache entries to disk
CacheExpiryBgService Background service coordinating expiry synchronisation between local and remote caches
LocalCacheExpiryService Handles local cache entry expiration
RemoteCacheExpiryService Handles remote cache entry expiration

Extensions

Extension Description
ServiceCollectionExtensions.AddCasCapCaching() Registers all caching services into the DI container
CachingExtensions Helper methods for cache key formatting and serialization
AsyncDuplicateLock Prevents duplicate concurrent cache population for the same key

Configuration

Type Description
CachingConfig Main configuration record — RemoteCacheConnectionString, PubSubPrefix, MemoryCacheSizeLimit, UseBuiltInLuaScripts, DiskCacheFolder, ExpirationSyncMode, DistributedLockingEnabled, CacheKeyFormat, Redlock
RedlockConfig Timing parameters for Redis distributed locks with named profile support — root defaults (ExpiryMs 5s, WaitMs 5s, RetryMs 250ms) tuned for cache-miss protection. RedisKeyFormat for lock key prefixing. Built-in LeaderElection profile (30s/60s/5s) for long-lived locks. Custom profiles via Profiles dictionary
RedlockProfiles Well-known profile name constants — CacheMiss, LeaderElection
RedlockTimingProfile Timing values for a single named lock profile — ExpiryMs, WaitMs, RetryMs
CacheParameters Per-layer cache configuration (TTL, size limits)

Enums

Enum Values
CacheType None, Memory, Disk, Redis
SerializationType None, Json, MessagePack
ExpirationSyncType None, ExpireViaPubSub, ExtendRemoteExpiry

Service Architecture

Multi-tier caching infrastructure with cache-aside pattern:

flowchart TD
    CLIENT["Application Code"]

    subgraph CachingServices["Caching Services"]
        DIST["DistributedCacheService<br/>(Multi-tier orchestrator)"]

        subgraph LocalCaches["Local Cache Implementations"]
            MEM["MemoryCacheService<br/>(IMemoryCache)"]
            DISK["DiskCacheService<br/>(File system)"]
        end

        subgraph RemoteCaches["Remote Cache"]
            REDIS["RedisCacheService<br/>(StackExchange.Redis)"]
        end

        subgraph BackgroundServices["Background Services"]
            EXPIRY_BG["CacheExpiryBgService"]
            LOCAL_EXP["LocalCacheExpiryService"]
            REMOTE_EXP["RemoteCacheExpiryService"]
        end
    end

    REDIS_SERVER[("Redis Server<br/>(Pub/Sub)")]
    LOCK["AsyncDuplicateLock<br/>(Prevent thundering herd)"]
    REDLOCK["IDistributedLockFactory<br/>(RedLock.net — optional)"]

    CLIENT --> DIST
    DIST --> MEM
    DIST --> DISK
    DIST --> REDIS
    DIST -.uses.-> LOCK
    DIST -."opt-in distributed lock".-> REDLOCK

    REDIS <--> REDIS_SERVER
    REDLOCK -."Redlock algorithm".-> REDIS_SERVER

    EXPIRY_BG --> LOCAL_EXP
    EXPIRY_BG --> REMOTE_EXP

    REDIS_SERVER -."Pub/Sub expiry sync".-> LOCAL_EXP
    REDIS_SERVER -."Pub/Sub expiry sync".-> REMOTE_EXP

    LOCAL_EXP -.expires.-> MEM
    LOCAL_EXP -.expires.-> DISK
    REMOTE_EXP -.expires.-> REDIS

Configuration Examples

All configuration lives under the CasCap:CachingConfig section in appsettings.json. Properties omitted from the JSON use their record defaults.

Minimal — local memory cache only

{
  "CasCap": {
    "CachingConfig": {
      "RemoteCacheConnectionString": "localhost:6379,abortConnect=false"
    }
  }
}

Redis remote cache with JSON serialization

{
  "CasCap": {
    "CachingConfig": {
      "RemoteCacheConnectionString": "localhost:6379,abortConnect=false",
      "RemoteCache": {
        "SerializationType": "Json"
      }
    }
  }
}

Distributed locking enabled (cache-miss protection)

{
  "CasCap": {
    "CachingConfig": {
      "RemoteCacheConnectionString": "localhost:6379,abortConnect=false",
      "DistributedLockingEnabled": true,
      "RemoteCache": {
        "SerializationType": "Json"
      }
    }
  }
}

Distributed locking with custom Redlock profiles

{
  "CasCap": {
    "CachingConfig": {
      "RemoteCacheConnectionString": "localhost:6379,abortConnect=false",
      "DistributedLockingEnabled": true,
      "RemoteCache": {
        "SerializationType": "Json"
      },
      "Redlock": {
        "ExpiryMs": 5000,
        "WaitMs": 5000,
        "RetryMs": 250,
        "Profiles": {
          "LeaderElection": {
            "ExpiryMs": 30000,
            "WaitMs": 60000,
            "RetryMs": 5000
          }
        }
      }
    }
  }
}

Fully configured — production-style deployment

{
  "CasCap": {
    "CachingConfig": {
      "RemoteCacheConnectionString": "redis-sentinel:26379,serviceName=mymaster,abortConnect=false,connectTimeout=1000",
      "DistributedLockingEnabled": true,
      "CacheKeyFormat": "Production:{0}",
      "MemoryCacheSizeLimit": 500,
      "UseBuiltInLuaScripts": true,
      "DiskCacheFolder": "/var/cache/cascap",
      "ExpirationSyncMode": "ExpireViaPubSub",
      "MemoryCache": {
        "IsEnabled": true,
        "ClearOnStartup": false,
        "SerializationType": "None"
      },
      "DiskCache": {
        "IsEnabled": false,
        "SerializationType": "Json"
      },
      "RemoteCache": {
        "IsEnabled": true,
        "DatabaseId": 1,
        "ClearOnStartup": false,
        "SerializationType": "MessagePack"
        },
        "Redlock": {
          "RedisKeyFormat": "Production:RedLock:{0}",
          "ExpiryMs": 5000,
        "WaitMs": 5000,
        "RetryMs": 250,
        "Profiles": {
          "LeaderElection": {
            "ExpiryMs": 30000,
            "WaitMs": 60000,
            "RetryMs": 5000
          },
          "LongRunningJob": {
            "ExpiryMs": 120000,
            "WaitMs": 30000,
            "RetryMs": 10000
          }
        }
      }
    }
  }
}

Dependencies

NuGet Packages

Package
Microsoft.Extensions.Caching.Memory
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Options.ConfigurationExtensions
Microsoft.Extensions.Options.DataAnnotations
RedLock.net
StackExchange.Redis

Project References

Project Purpose
CasCap.Common.Abstractions ILocalCache, IAppConfig contracts
CasCap.Common.Extensions General-purpose helper utilities
CasCap.Common.Serialization.Json JSON serialization for cache values
CasCap.Common.Serialization.MessagePack MessagePack serialization for cache values
CasCap.Common.Logging ApplicationLogging static logger factory
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.9.0 28 4/15/2026
4.8.1 160 4/2/2026
4.8.0 155 4/1/2026
4.7.1 227 3/13/2026
4.7.0 102 3/11/2026
4.6.0 291 2/12/2026
4.5.5 116 2/2/2026
4.5.4 180 1/25/2026
4.5.3 488 12/11/2025
4.5.1 391 11/12/2025
4.5.0 362 11/11/2025
4.4.0 241 11/1/2025
4.3.12 288 10/19/2025
4.3.11 358 10/10/2025
4.3.10 317 10/6/2025
4.3.9 342 9/30/2025
4.3.8 256 9/14/2025
4.3.7 293 8/13/2025
4.3.6 437 8/7/2025
4.3.5 393 7/9/2025
Loading failed