ZiggyCreatures.FusionCache 0.10.0 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET Standard 2.0
Install-Package ZiggyCreatures.FusionCache -Version 0.10.0
dotnet add package ZiggyCreatures.FusionCache --version 0.10.0
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.10.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ZiggyCreatures.FusionCache --version 0.10.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZiggyCreatures.FusionCache, 0.10.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 ZiggyCreatures.FusionCache as a Cake Addin
#addin nuget:?package=ZiggyCreatures.FusionCache&version=0.10.0

// Install ZiggyCreatures.FusionCache as a Cake Tool
#tool nuget:?package=ZiggyCreatures.FusionCache&version=0.10.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

FusionCache

FusionCache logo

FusionCache is an easy to use, high performance and robust cache with an optional distributed 2nd layer and some advanced features.

It was born after years of dealing with all sorts of different types of caches: memory caching, distributed caching, http caching, CDNs, browser cache, offline cache, you name it. So I've tried to put together these experiences and came up with FusionCache.

It uses a memory cache (any impl of the standard IMemoryCache interface) as the primary backing store and optionally a distributed, 2nd level cache (any impl of the standard IDistributedCache interface) as a secondary backing store for better resilience and higher performance, for example in a multi-node scenario or to avoid the typical effects of a cold start (initial empty cache, maybe after a restart).

Optionally, it can also use a backplane: in a multi-node scenario this will send notifications to the other nodes to keep each node's memory cache perfectly synchronized, without any additional work.

FusionCache also includes some advanced features like a fail-safe mechanism, cache stampede prevention, fine grained soft/hard timeouts with background factory completion, customizable extensive logging and more (see below).

FusionCache diagram

🏆 Award

On August 2021, FusionCache received the Google Open Source Peer Bonus Award. Here is the official blogpost.

✔ Features

These are the key features of FusionCache:

  • 🚀 Cache Stampede prevention: using the optimized GetOrSet[Async] method prevents multiple concurrent factory calls per key, with a guarantee that only 1 factory will be called at the same time for the same key (this avoids overloading the data source when no data is in the cache or when a cache entry expires)
  • 🔀 Optional 2nd level: FusionCache can transparently handle an optional 2nd level cache: anything that implements the standard IDistributedCache interface is supported (eg: Redis, MongoDB, SqlServer, etc)
  • 📢 Backplane: when using a distributed cache as a 2nd layer in a multi-node scenario, you can also enable a backplane to immediately notify the other nodes about changes in the cache, to keep everything synchronized without having to do anything
  • 💣 Fail-Safe: enabling the fail-safe mechanism prevents throwing an exception when a factory or a distributed cache call would fail, by reusing an expired entry as a temporary fallback, all transparently and with no additional code required
  • ⏱ Soft/Hard timeouts: advanced timeouts management prevents waiting for too long when calling a factory or the distributed cache, to avoid hanging your application. It is possible to specify both soft and hard timeouts, and thanks to automatic background completion no data will be wasted
  • 🧙‍♂️ Adaptive Caching: there are times when you don't know upfront what the cache duration for a piece of data should be, maybe because it depends on the object being cached itself. Adaptive caching solves this elegantly
  • ⚡ High performance: FusionCache is optimized to minimize CPU usage and memory allocations to get better performance and lower the cost of your infrastructure all while obtaining a more stable, error resilient application
  • 💫 Natively sync/async: full native support for both the synchronous and asynchronous programming model, with sync/async methods working together harmoniously
  • 📞 Events: there's a comprehensive set of events to subscribe to regarding core events inside of a FusioCache instance, both at a high level and at lower levels (memory/distributed layers)
  • 🧩 Plugins: thanks to a plugin subsystem it is possible to extend FusionCache with additional behaviour, like adding support for metrics, statistics, etc...
  • 📃 Extensive logging: comprehensive, structured, detailed and customizable logging via the standard ILogger interface (you can use Serilog, NLog, etc)

⭐ Quick Start

FusionCache can be installed via the nuget UI (search for the ZiggyCreatures.FusionCache package) or via the nuget package manager console:

PM> Install-Package ZiggyCreatures.FusionCache

As an example, imagine having a method that retrieves a product from your database:

Product GetProductFromDb(int id) {
	// YOUR DATABASE CALL HERE
}

💡 This is using the sync programming model, but it would be equally valid with the newer async one for even better performance.

To start using FusionCache the first thing is create a cache instance:

var cache = new FusionCache(new FusionCacheOptions());

If instead you are using DI (Dependency Injection) use this:

services.AddFusionCache();

We can also specify some global options, like a default FusionCacheEntryOptions object to serve as a default for each call we'll make, with a duration of 2 minutes and a Low priority:

var cache = new FusionCache(new FusionCacheOptions() {
	DefaultEntryOptions = new FusionCacheEntryOptions {
		Duration = TimeSpan.FromMinutes(2),
		Priority = CacheItemPriority.Low
	}
});

Or, using DI, like this:

services.AddFusionCache(options => {
	options.DefaultEntryOptions = new FusionCacheEntryOptions {
		Duration = TimeSpan.FromMinutes(2),
		Priority = CacheItemPriority.Low
	}
});

Now, to get the product from the cache and, if not there, get it from the database in an optimized way and cache it for 30 sec simply do this:

var id = 42;

cache.GetOrSet<Product>(
	$"product:{id}",
	_ => GetProductFromDb(id),
	TimeSpan.FromSeconds(30)
);

That's it 🎉

📖 Documentation

A complete documentation, including examples and common use cases, is available at the official repo page on GitHub.

🧰 Supported Platforms

FusionCache targets .NET Standard 2.0 so any compatible .NET implementation is fine: this means .NET Framework (the old one), .NET Core 2+ and .NET 5/6+ (the new ones), Mono 5.4+ and more (see here for a complete rundown).

NOTE: if you are running on .NET Framework 4.6.1 and want to use .NET Standard packages Microsoft suggests to upgrade to .NET Framework 4.7.2 or higher (see the .NET Standard Documentation) to avoid some known dependency issues.

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
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on ZiggyCreatures.FusionCache:

Package Downloads
ZiggyCreatures.FusionCache.Serialization.SystemTextJson The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

FusionCache serializer based on System.Text.Json

ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

FusionCache serializer based on Newtonsoft Json.NET

ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

FusionCache backplane for Redis based on the StackExchange.Redis library

IdempotentAPI.Cache.FusionCache

The FusionCache implementation for the definition of the IdempotentAPI.Cache.

JoeShook.ZiggyCreatures.FusionCache.Metrics.EventCounters

EventSource metrics provider implementation.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ZiggyCreatures.FusionCache:

Repository Stars
TurnerSoftware/CacheTower
An efficient multi-layered caching system for .NET
Version Downloads Last updated
0.10.0 15,067 5/1/2022
0.10.0-preview1 149 4/23/2022
0.9.0 13,960 2/17/2022
0.1.10-beta3 1,024 2/14/2022
0.1.10-beta2 56 2/11/2022
0.1.10-beta1 78 2/9/2022
0.1.10-alpha2 156 1/30/2022
0.1.10-alpha1 69 1/27/2022
0.1.9 29,611 11/23/2021
0.1.8 6,303 11/23/2021
0.1.7 4,367 10/16/2021
0.1.6 33,719 8/1/2021
0.1.5 2,345 6/29/2021
0.1.4 1,763 6/5/2021
0.1.3 2,225 4/3/2021
0.1.2 8,049 2/26/2021
0.1.1 1,008 1/19/2021
0.1.0 409 12/31/2020

- added support for adaptive caching
- added an optimization to use the FactorySoftTimeout as a LockTimeout in case a custom one has not been specified + fail-safe is enabled + there's a fallback entry, so that in certain situations there can be a perf boost
- added IFusionCache.HasDistributedCache prop to know if there is a distributed cache configured
- added a warning log in case there's a backplane configured without a distributed cache and DefaultEntryOptions.EnableBackplaneNotifications == true , since this can be dangerous
- more correct cache hit events in case of stale data
- better nullability decoration