IntelligentHack.IntelligentCache 3.2.0

There is a newer version of this package available.
See the version list below for details.
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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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. 
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 (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