ZiggyCreatures.FusionCache 0.22.0

FusionCache

FusionCache logo

🙋‍♂️ Updating from before v0.20.0 ? please read here.

FusionCache is an easy to use, fast and robust cache with advanced resiliency features and an optional distributed 2nd layer.

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.

FusionCache diagram

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 resiliency 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).

🏆 Award

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

📕 Getting Started

With 🦄 A Gentle Introduction you'll get yourself comfortable with the overall concepts.

Want to start using it immediately? There's a ⭐ Quick Start for you.

Curious about what you can achieve from start to finish? There's a 👩‍🏫 Step By Step guide.

More into videos? The great Anna Hoffman has been so nice to listen to me mumble random stuff on Data Exposed.

Data Exposed Talk

✔ Features

These are the key features of FusionCache:

  • 🚀 Cache Stampede prevention: automatic protection from the Cache Stampede problem
  • 🔀 Optional 2nd level: an optional 2nd level handled transparently, with any implementation of IDistributedCache
  • 💣 Fail-Safe: a mechanism to avoids transient failures, by reusing an expired entry as a temporary fallback
  • ⏱ Soft/Hard timeouts: a slow factory (or distributed cache) will not slow down your application, and no data will be wasted
  • 🧙‍♂️ Adaptive Caching: for when you don't know upfront the cache duration, as it depends on the value being cached itself
  • 🔂 Conditional Refresh: like HTTP Conditional Requests, but for caching
  • 🦅 Eager Refresh: start a non-blocking background refresh before the expiration occurs
  • 📢 Backplane: in a multi-node scenario, it can notify the other nodes about changes in the cache, so all will be in-sync
  • 🔃 Dependency Injection: native support for Dependency Injection, with a nice fluent interface including a Builder support
  • 📛 Named Caches: easily work with multiple named caches, even if differently configured
  • 💫 Natively sync/async: native support for both the synchronous and asynchronous programming model
  • 📞 Events: a comprehensive set of events, both at a high level and at lower levels (memory/distributed)
  • 🧩 Plugins: extend FusionCache with additional behavior like adding support for metrics, statistics, etc...
  • 📜 Logging: comprehensive, structured and customizable, via the standard ILogger interface

⭐ 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()
	.WithDefaultEntryOptions(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.

Showing the top 20 packages that depend on ZiggyCreatures.FusionCache.

Packages Downloads
ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis
FusionCache backplane for Redis based on the StackExchange.Redis library
4
ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis
FusionCache backplane for Redis based on the StackExchange.Redis library
5
ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis
FusionCache backplane for Redis based on the StackExchange.Redis library
41
ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson
FusionCache serializer based on Newtonsoft Json.NET
4
ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson
FusionCache serializer based on Newtonsoft Json.NET
5
ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson
FusionCache serializer based on Newtonsoft Json.NET
6
ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson
FusionCache serializer based on Newtonsoft Json.NET
44

- Added: Expire()/ExpireAsync() method - Added: SkipMemoryCache option - Added: NullFusionCache (null object pattern) - Added: better timestamping - Added: special handling of zero duration - Change: better log messages with CacheName and InstanceId - Change: Adaptive Caching now supports more use cases - Change: Eager Refresh now also checks distributed cache - Tests: added automated tests for serializers' backward compatibility towards old versions' binary payloads - Perf: better (lazy) handling of named caches instantiation - Perf: better handling of backplane messages - Perf: more robus edge-case handling for locks release - Perf: better handling of distributed cache and backplane while disposing FusionCache - Perf: various performance optimizations - Obsolete: some old obsolete members have been marked with the error flag (next step: remove them)

Version Downloads Last updated
2.3.0 4 06/10/2025
2.2.0 4 05/23/2025
2.2.0-preview-1 4 04/14/2025
2.1.0 35 03/16/2025
2.0.2 3 06/08/2025
2.0.1 4 04/01/2025
2.0.0 4 04/01/2025
2.0.0-preview-4 4 04/01/2025
2.0.0-preview-3 4 04/01/2025
2.0.0-preview-2 3 04/01/2025
2.0.0-preview-1 3 04/01/2025
1.4.1 4 04/01/2025
1.4.0 4 04/01/2025
1.3.0 4 04/01/2025
1.2.0 4 04/01/2025
1.2.0-preview1 4 04/01/2025
1.1.0 4 04/01/2025
1.0.0 5 04/01/2025
1.0.0-preview2 4 04/01/2025
1.0.0-preview1 4 04/01/2025
0.26.0 4 04/01/2025
0.25.0 4 04/01/2025
0.25.0-preview1 4 04/01/2025
0.24.0 6 04/01/2025
0.24.0-preview1 4 04/01/2025
0.23.0 4 04/01/2025
0.22.0 3 04/01/2025
0.21.0 4 04/01/2025
0.21.0-preview2 4 04/01/2025
0.21.0-preview1 4 04/01/2025
0.20.0 5 04/01/2025
0.20.0-preview2 3 04/01/2025
0.20.0-preview1 4 04/01/2025
0.19.0 5 04/01/2025
0.18.0 4 04/01/2025
0.17.0 4 04/01/2025
0.16.0 4 04/01/2025
0.15.0 3 04/01/2025
0.14.0 4 04/01/2025
0.13.0 4 04/01/2025
0.12.0 4 04/01/2025
0.11.1 4 04/01/2025
0.11.0 4 04/01/2025
0.10.0 4 04/01/2025
0.10.0-preview1 6 04/01/2025
0.9.0 3 04/01/2025
0.1.10-beta3 4 04/01/2025
0.1.10-beta2 4 04/01/2025
0.1.10-beta1 4 04/01/2025
0.1.10-alpha2 6 04/01/2025
0.1.10-alpha1 4 04/01/2025
0.1.9 4 04/01/2025
0.1.8 4 04/01/2025
0.1.7 4 04/01/2025
0.1.6 4 04/01/2025
0.1.5 3 04/01/2025
0.1.4 4 04/01/2025
0.1.3 4 04/01/2025
0.1.2 4 04/01/2025
0.1.1 4 04/01/2025
0.1.0 4 04/01/2025