IntelligentHack.IntelligentCache 3.2.0

.NET 5.0
dotnet add package IntelligentHack.IntelligentCache --version 3.2.0
NuGet\Install-Package IntelligentHack.IntelligentCache -Version 3.2.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="IntelligentHack.IntelligentCache" Version="3.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IntelligentHack.IntelligentCache --version 3.2.0
#r "nuget: IntelligentHack.IntelligentCache, 3.2.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install IntelligentHack.IntelligentCache as a Cake Addin
#addin nuget:?package=IntelligentHack.IntelligentCache&version=3.2.0

// Install IntelligentHack.IntelligentCache as a Cake Tool
#tool nuget:?package=IntelligentHack.IntelligentCache&version=3.2.0

<img src="doc/logo.png?raw=true" width="200">

Intelligent Cache

This package implements a distributed cache monad ("pattern") and currently supports single and multiple layers of caching, in memory and via Redis.

To use the pattern, you will interact with an object of type ICache, where you can use the following operations:

// Get something from the cache, on cache fail the Func is called to refresh the value and stored
var foo = myCache.GetSet("foo-cache-key", ()=>{ return foo-from-db(); }, Timespan.FromHours(1) );

// alternatively

// Invalidate the cache, in case we've modified foo in the db so the cache is stale
myCache.Invalidate("foo-cache-key");

The ICache object can be of different kinds -- we currently offer a memory cache for local caching and a Redis cache for distributed caching. What makes the pattern a monad is that different caches can be composed and this allows seamless multilayer caching.

For example, to implement a multilayer cache with a local layer and a Redis layer:

var memoryCache = new MemoryCache(/* params */);
var redisCache = new RedisCache(/* params */);
var cache = new CompositeCache(memoryCache, redisCache);

Note that this cache does not invalidate correctly in a web farm environment: Invalidations will work on the local server and Redis but not the other web farm webservers. In order to propagate invalidation, we introduced two new composable ICache objects: RedisInvalidationSender and RedisInvalidationReceiver.

In order to create a local cache that invalidates when the remote cache is nuked, you can follow this composition pattern:

ISubscriber subscriber = GetRedisSubscriber();
var invalidationChannel = "cache-invalidations";
var cache = new CompositeCache(
    new RedisInvalidationReceiver(
        new MemoryCache(/* arguments */),
        subscriber,
        invalidationChannel
    ),
    new CompositeCache(
        new RedisCache(/* arguments */),
        new RedisInvalidationSender(subscriber, invalidationChannel)
    )
);

Take in account that when a calculateValue function returns a null value nothing is cached and a null value is returned back to the caller.

Details

Upgrading from a previous version

This package follows semantic versioning, which means that upgrading to a higher MINOR or PATCH version should always work. Upgrading to a higher MAJOR version will require code changes. Make sure to read the release notes before upgrading.

Contributing

Please read CONTRIBUTING.md for guidelines.

License

This code is released under the MIT license. Please refer to LICENSE.md for details.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on IntelligentHack.IntelligentCache:

Repository Stars
TurnerSoftware/CacheTower
An efficient multi-layered caching system for .NET
Version Downloads Last updated
3.2.0 84 11/2/2022
3.0.2 8,397 5/6/2021
3.0.0 4,689 11/25/2020
1.0.1 269 5/6/2021