DistributedCache 8.0.0

dotnet add package DistributedCache --version 8.0.0
NuGet\Install-Package DistributedCache -Version 8.0.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="DistributedCache" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DistributedCache --version 8.0.0
#r "nuget: DistributedCache, 8.0.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.
// Install DistributedCache as a Cake Addin
#addin nuget:?package=DistributedCache&version=8.0.0

// Install DistributedCache as a Cake Tool
#tool nuget:?package=DistributedCache&version=8.0.0

DistributedCache

DistributedCache is an open source caching abstraction layer for .NET which supports Redis, SQL Server, InMemory, with automatic failure recovery, heath checks, automatic table generation (MSSQL), logging and also allows to clear all keys.

It simplifies cache usage by allowing developers to inject it into the application and use it anywhere without having to worry about configuration for an specific infrastructure (InMemory, Redis, SQL server).

It is as fast as a cache interface can be and also includes the feature of CLEAR ALL keys and CLEAR ALL WITH PREFIX which were both missing from IDistributedCache.


Usage

Setting/Getting values from cache

Just inject and use the ICache interface to store or obtain values from the cache like the example below:


public class CountryManager
{
    private readonly ICache _cache;

    public CountryManager(ICache cache)
    {
        _cache = cache;
    }

    public async Task<List<CountryData>> GetAllCountriesAsync(CancellationToken cancellationToken)
    {
        var cacheKey = "some cache key";

        var cachedValue = await _cache.GetCacheValueAsync<List<CountryData>>(cacheKey, cancellationToken);
        if (cachedValue != null)
        {
            return cachedValue;
        }

        var dataValue = await GetAllCountriesFromRepositoryAsync();
        _ = _cache.SetCacheValueAsync(cacheKey, dataValue);

        return dataValue;
    }
}

Setting cache TTL

There are 2 ways to achieve that:

  1. Using a default TTL (see configuration section)
  2. Overriding the default TTL on a case-by-case basis like this:

var ttl = new DistributedCacheEntryOptions
{
    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60),
    SlidingExpiration = TimeSpan.FromSeconds(30)
};
_ = _cache.SetCacheValueAsync(cacheKey, dataValue, ttl, cancellationToken);

Configuration

Scenario 1: InMemory Cache

This is the most basic, yet common, scenario in which the application uses a cache in memory to increase the performance of a single instance of an application.

services.AddDistributedCache(options =>
{
    options.Configure(CacheType.InMemory);
});

Scenario 2: Redis with automatic failure recovery

This configuration use redis infrastructure and will check if it is healthy, in case of repeated failures (maxErrorsAllowed) it will automatically disable the cache and will check again in the interval given (resetIntervalMinutes).

var connectionString = "some cache connection string";
services.AddDistributedCache(options =>
{
    options.Configure(CacheType.Redis, connectionString, "myApplicationCacheInstance")
        .ConfigureHealthCheck(enabled:true, maxErrorsAllowed:5, resetIntervalMinutes:2);
});

Scenario 3: SQL Server with automatic failure recovery

This configuration use redis infrastructure and will check if it is healthy, in case of repeated failures (maxErrorsAllowed) it will automatically disable the cache and will check again in the interval given (resetIntervalMinutes).

var connectionString = "some cache connection string";
services.AddDistributedCache(options =>
{
    options.Configure(CacheType.SqlServer, connectionString, "myApplicationCacheInstance")
        .ConfigureHealthCheck(enabled:true, maxErrorsAllowed:5, resetIntervalMinutes:2)
        .AddDefaultTtl(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(30))
        .DisableCache(false);
});

Contributing

It is simple, as all things should be:

  1. Clone it
  2. Improve it
  3. Make pull request

Credits

  • Initial development by Slukad
Product Compatible and additional computed target framework versions.
.NET 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. 
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
8.0.0 352 11/17/2023
7.0.3 167 5/19/2023
7.0.2 130 5/18/2023
7.0.1 126 5/18/2023
1.3.5 129 5/11/2023
1.3.4 127 5/9/2023
1.3.3 120 5/9/2023
1.3.2 123 5/6/2023

Release