RedisMemoryCacheInvalidationCore 2.5.0

dotnet add package RedisMemoryCacheInvalidationCore --version 2.5.0
                    
NuGet\Install-Package RedisMemoryCacheInvalidationCore -Version 2.5.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="RedisMemoryCacheInvalidationCore" Version="2.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RedisMemoryCacheInvalidationCore" Version="2.5.0" />
                    
Directory.Packages.props
<PackageReference Include="RedisMemoryCacheInvalidationCore" />
                    
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 RedisMemoryCacheInvalidationCore --version 2.5.0
                    
#r "nuget: RedisMemoryCacheInvalidationCore, 2.5.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 RedisMemoryCacheInvalidationCore@2.5.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=RedisMemoryCacheInvalidationCore&version=2.5.0
                    
Install as a Cake Addin
#tool nuget:?package=RedisMemoryCacheInvalidationCore&version=2.5.0
                    
Install as a Cake Tool

RedisMemoryCacheInvalidation - Core

A migration and changes to RedisMemoryCacheInvalidation as PRs were going stale and the package appears abandoned.

Nuget Nuget

System.Runtime.MemoryCache invalidation using Redis PubSub feature.

Installing via NuGet

Install-Package RedisMemoryCacheInvalidationCore

How to use it ?

quick start

First, you have to configure the library, mainly to set up a persistent redis connection and various stuff

  // somewhere in your global.asax/startup.cs
  InvalidationManager.Configure("localhost:6379", new InvalidationSettings());

Redis connection string follow StackExchange.Redis Configuration model

There are at least 3 ways to send invalidation messages :

  • send an invalidation message via any redis client following the command PUBLISH invalidate onemessagekey
  • use InvalidationManager.InvalidateAsync (same as the previous one)
  • use keyspace notification (yes, RedisMemoryCacheInvalidation supports it)

Once an invalidation message is intercepted by the library, you can invalidate one or more items at the same time by using InvalidationSettings.InvalidationStrategy

  • InvalidationStrategyType.ChangeMonitor ⇒ a custom change monitor InvalidationManager.CreateChangeMonitor
  • InvalidationStrategyType.AutoCacheRemoval ⇒ use the automatic MemoryCache removal configured at InvalidationSettings.ConfigureAsync
  • InvalidationStrategyType.External ⇒ use the callback configured at InvalidationSettings.InvalidationCallback

How it works ?

A single connection is opened to redis via the servicestack thread-safe multiplexer.

The library uses a topic-based observer pattern to manage notifications within the application; allowing for a large number of possible monitors.

Invalidation messages are sent in the format channel=invalidate and message=key-to-invalidate.

As the name suggests, this implementation relies on Redis, especially on PubSub feature. In the current version of this implementation, nothing is stored on the Redis server.

                       +------------------------------------------+
                       |  System.Runtime.Caching.MemoryCache      |
                       |  clients (Frontend/Backend Servers)      |
                       |------------------------------------------|
                       |   [Server1]   [Server2]   [Server3]      |
                       +------------------------------------------+
                             ^    ^    ^              |
                             |    |    |              |
                             |    |    |              v
                             |    |    |   5 - Remove items from local cache
                             |    |    +---------------------------+
                             |    |                                |
                             |    +--------------------------------+
                             |                                     |
                             |                                     |
                             | 2 - OnDemand: Load data             |
                             |     from Data Tier                  |
                             |                                     |
                       +-------------------+                       |
                       |     Data Tier     |<----------------------+
                       +-------------------+

                             ^
                             | 4 - Send notification
                             |
                       +-------------------+
                       |      Redis        |
                       |  (Invalidations)  |
                       +-------------------+
                          ^             |
        1 - Subscribe     |             | 3 - Publish Invalidation
        on startup        |             |   (when something changed)
                          |             v
                          |       +-------------------+
                          |       |    Publisher      |
                          |       +-------------------+
                          |
                    3' - Publish Invalidation
                        from any redis client
 

Configuration

Settings

To configure RedisMemoryCacheInvalidation, you should use one of the InvalidationManager.ConfigureAsync methods. Three parameters are available to configure it :

  • redisConfig:string : Redis connection string. Check StackExchange.Redis Configuration model for more details. A basic example is localhost:6379.
  • mux:ConnectionMultiplexer : an existing StackExchange.Redis.ConnectionMultiplexer that you want to reuse.
  • settings:InvalidationSettings : see below for more details.

InvalidationSettings is the main configuration object

  • InvalidationStrategy:InvalidationStrategyType : How to handle invalidation notifications : notify ChangeMonitor, execute callback or automatically remove an item from the cache.
  • TargetCache:MemoryCache : the target MemoryCache instance when InvalidationStrategy is set to AutoCacheRemoval.
  • EnableKeySpaceNotifications:bool : allow subscribe to keyevents notification __keyevent*__:.
  • InvalidationCallback:Action : a callback that is invoked when InvalidationStrategy is set to External.

When to configure ?

Thanks to StackExchange.Redis a persistent connection is established between your application and the redis server. That's why it's important to configure it very early at startup : Global.asax, Owin or Application Initialisation.

Examples

Once RedisMemoryCacheInvalidation is configured, local cache invalidation is a two-steps process : capturing invalidation messages and handling those notification messages.

Sending invalidation messages

You can use one of the folowing methods.

  • Send a pubsub message from any redis client PUBLISH invalidate onemessagekey.
  • Send an invalidation message from InvalidationManager.InvalidateAsync("onemessagekey")
  • Capture keyspace events for one particular key. Note : the redis server should be configured to support keyspace events. (off by default)

Handling notification messages

This behavior is entirely configured via InvalidationSettings.InvalidationStrategyType. As it's marked with a FlagsAttribute, you can use one or more strategies.

  • Automatically removed a cache item from the cache

The easiest way to invalidate local cache items. If the The core will try to remove cache items For example, if you add a cache item like this :

CacheItem cacheItem = new CacheItem("mycacheKey", "cachevalue");
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.UtcNow.AddDays(1);
MyCache.Add(cacheItem, policy);

Calling PUBLISH invalidate mycacheKey or InvalidationManager.InvalidateAsync("mycacheKey") will remove that item from the cache.

  • Notify ChangeMonitors

ChangeMonitor is defined as "Provides a base class for a derived custom type that monitors changes in the state of the data which a cache item depends on.""

You can create a custom monitor (watching for myinvalidationKey) like this :

CacheItem cacheItem = new CacheItem("cacheKey", "cachevalue");
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.UtcNow.AddDays(1);
policy.ChangeMonitors.Add(InvalidationManager.CreateChangeMonitor("myinvalidationKey"));
MyCache.Add(cacheItem, policy);

When raised the corresponding cache item will be automatically removed. One interesting feature is that you can create several change monitors watching for the same key.

  • invoke a callback

Suppose you're using another caching implementation there is another way to be notified with InvalidationStrategyType.External. Each time a notification message is intercepted, the callback defined in InvalidationSettings.InvalidationCallback is invoked. It's up to you to remove/flush/reload the cache item.

  • Enabling resilience (retrys)
var settings = new InvalidationSettings
{
    EnableResilience = true,
    HealthCheckIntervalMs = 15000, // 15 seconds
    MaxRetryAttempts = 5,
    RetryDelayMs = 500
};

InvalidationManager.Configure("localhost:6379", settings);

License

Licensed under the terms of the MIT License

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 was computed.  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 was computed.  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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  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
2.5.0 41 9/16/2025
2.4.0 4,102 7/16/2024
2.3.0 124 7/16/2024
2.2.0 2,584 5/10/2023
2.0.2 598 7/14/2020