Aqovia.MultiplexingDistributedCache
0.1.0-preview-49.1.5237228532
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
<PackageReference Include="Aqovia.MultiplexingDistributedCache" Version="0.1.0-preview-49.1.5237228532" />
paket add Aqovia.MultiplexingDistributedCache --version 0.1.0-preview-49.1.5237228532
#r "nuget: Aqovia.MultiplexingDistributedCache, 0.1.0-preview-49.1.5237228532"
// 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
- Provision your new Redis instance
- Change your application to use Aqovia.MultiplexingDistributedCache with the primary to be your current Redis instance, and secondary to be the new Redis instance
- 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.
- Change your application, so that the primary cache is your new Redis instance and secondary is the old one.
- 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.
- 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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Caching.Abstractions (>= 7.0.0)
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 |
---|---|---|
0.1.0-preview-49.1.5237228532 | 105 | 6/11/2023 |
0.1.0-preview-48.1.5237228328 | 88 | 6/11/2023 |
0.1.0-preview-47.1.5236987888 | 88 | 6/11/2023 |
0.1.0-preview-46.1.5236987746 | 87 | 6/11/2023 |
0.1.0-preview-45.1.5236960479 | 90 | 6/11/2023 |
0.1.0-preview-44.1.5236960401 | 95 | 6/11/2023 |
0.1.0-preview-43.1.5236706346 | 93 | 6/11/2023 |
0.1.0-preview-42.1.5236706189 | 92 | 6/11/2023 |
0.1.0-preview-39.1.5234277943 | 110 | 6/11/2023 |
0.1.0-preview-38.2.5234229427 | 89 | 6/11/2023 |
0.1.0-preview-38.1.5234229427 | 85 | 6/11/2023 |