Aqovia.MultiplexingDistributedCache 0.1.0-preview-49.1.5237228532

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

// Install Aqovia.MultiplexingDistributedCache as a Cake Tool
#tool nuget:?package=Aqovia.MultiplexingDistributedCache&version=0.1.0-preview-49.1.5237228532&prerelease                

Aqovia Multiplexing Distributed Cache

Aqovia.MultiplexingDistributedCache is a library that implements a IDistributedCache that multiplexes between two other implementations of IDistributedCache, primary and secondary. While the secondary is optional that makes this library act as only a primary cache.

How to use

You need to install the nuget package.

You will inject an object of MultiplexingDistributedCache in Startup. For that you need to pass a primary IDistributedCache and an optional secondary IDistributedCache objects.

In the following example we use StackExchange.Redis implementation of IDistributedCache as the primary cache and an In Memory cache for the secondary.

In Startup.cs:

using Aqovia.Cache;
using StackExchange.Redis;
using Microsoft.Extensions.Caching.Memory;

then in its ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    ...
    var primaryRedisConnectionString = Configuration.GetConnectionString("PrimaryRedisCacheConnectionString");
    var primaryRedisConfig = ConfigurationOptions.Parse(primaryRedisConnectionString);
    var primaryRedisConnectionMultiplexer = ConnectionMultiplexer.Connect(primaryRedisConfig);
    var primaryRedisCache = new RedisCache(new RedisCacheOptions() { ConfigurationOptions = primaryRedisConfig });

    var secondaryRedisCache = new MemoryCache(new MemoryCacheOptions() { });

    services.AddSingleton<IDistributedCache>(
        new MultiplexingDistributedCache(primaryRedisCache, secondaryRedisCache));
    services.AddSession();
    ...
}

You can also have only a Primary cache:

public void ConfigureServices(IServiceCollection services)
{
    ...
    var primaryRedisConnectionString = Configuration.GetConnectionString("PrimaryRedisCacheConnectionString");
    var primaryRedisConfig = ConfigurationOptions.Parse(primaryRedisConnectionString);
    var primaryRedisConnectionMultiplexer = ConnectionMultiplexer.Connect(primaryRedisConfig);
    var primaryRedisCache = new RedisCache(new RedisCacheOptions() { ConfigurationOptions = primaryRedisConfig });

    services.AddSingleton<IDistributedCache>(new MultiplexingDistributedCache(primaryRedisCache));
    services.AddSession();
    ...
}

This is useful when you want to have optional secondary based on your config settings:

public void ConfigureServices(IServiceCollection services)
{
    ...
    var primaryRedisConnectionString = Configuration.GetConnectionString("PrimaryRedisCacheConnectionString");
    var primaryRedisConfig = ConfigurationOptions.Parse(primaryRedisConnectionString);
    var primaryRedisConnectionMultiplexer = ConnectionMultiplexer.Connect(primaryRedisConfig);
    var primaryRedisCache = new RedisCache(new RedisCacheOptions() { ConfigurationOptions = primaryRedisConfig });

    IDistributedCache secondaryRedisCache = null;
    var secondaryRedisConnectionString = Configuration.GetConnectionString("SecondaryRedisCacheConnectionString");
    if (!string.IsNullOrEmpty(secondaryRedisConnectionString) &&
        !secondaryRedisConnectionString.Equals(primaryRedisConnectionString))
    {
        var secondaryRedisConfig = ConfigurationOptions.Parse(secondaryRedisConnectionString);
        var secondaryRedisConnectionMultiplexer = ConnectionMultiplexer.Connect(secondaryRedisConfig);
        secondaryRedisCache = new RedisCache(new RedisCacheOptions() { ConfigurationOptions = secondaryRedisConfig });
    }

    services.AddSingleton<IDistributedCache>(
        new MultiplexingDistributedCache(primaryRedisCache, secondaryRedisCache));
    services.AddSession();
    ...
}

Example Usage Scenario

You are using one Redis instance and you have to migrate to a new Redis instance, for reasons like:

  • You want to use different tier of Redis and you can't upgrade it.
  • Your Redis instance is in one Cloud Infrastructure or one tenant, and you want to migrate to a Redis in another.

Or any similar reasons. And you want to do this without losing your users' sessions.

Your Migration Plan

  1. Provision your new Redis instance
  2. Change your application to use Aqovia.MultiplexingDistributedCache with the primary to be your current Redis instance, and secondary to be the new Redis instance
  3. Wait for a while until the first session after you have done the action 2, is expired. This means both Redis instances should have the same data.
  4. Change your application, so that the primary cache is your new Redis instance and secondary is the old one.
  5. Change your application, so that the primary cache is your new Redis instance and you don't have a secondary cache. At this stage your app will work like before but with the new Redis instance.
  6. Now you can remove your old Redis instance.

How it works

Multiplexing Distributed Cache is an implementation of IDistributedCache that is multiplexing to two other implementation of IDistributedCache. Read will be always from the Primary cache, but other implemented methods of IDistributedCache will be done on both caches.

Feedback

feedbacks are always welcomed, please open an issue for any problem or bug found, and the suggestions are also welcomed.

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 was computed.  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. 
.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.