FluentCache is a simple, fluent library to help you write clean, legible caching code by reducing boilerplate.
double ezResult = cache.Method(r => r.DoSomeHardParameterizedWork(parameter))
.GetValue();
Features:
- Fluent API: clean, simple API to encapsulate caching logic
- Automatic Cache Key Generation: refactor at will and never worry about magic strings. FluentCache automatically analyzes the expression tree and generates caching keys based on the type, method, and parameters
- Caching policies: specify caching policies like expiration, validation, and error handling
- Cache Implementations: FluentCache supports common caching implementations and has a simple ICache interface to support other providers
Here's an example of some typical caching code that can be replaced by FluentCache:
//retrieve a value from the cache. If it's not there, load it from the repository
var repository = new Repository();
int parameter = 5;
string region = "FluentCacheExamples";
string cacheKey = "Samples.DoSomeHardParameterizedWork." + parameter;
CachedValue<double> cachedValue = cache.Get<double>(cacheKey, region);
if (cachedValue == null)
{
double val = repository.DoSomeHardParameterizedWork(parameter);
cachedValue = cache.Set<double>(cacheKey, region, val, new CacheExpiration());
}
double result = cachedValue.Value;
There are several issues with this code:
- boilerplate: duplicating this code is tedious
- magic strings: the cache key is based on magic strings that won't automatically refactor as methods and parameters change
- hard to read: the intent is overwhelmed by the mechanics
double ttlValue = cache.Method(r => r.DoSomeHardWork())
.ExpireAfter(TimeSpan.FromMinutes(5))
.GetValue();
double asyncValue = await cache.Method(r => r.DoSomeHardWorkAsync())
.GetValueAsync();
double onlyCachePositiveValues = cache.Method(r => r.DoSomeHardWork())
.InvalidateIf(cachedVal => cachedVal.Value <= 0d)
.GetValue();
cache.Method(r => r.DoSomeHardParameterizedWork(parameter))
.ClearValue();
To get started, we will use the FluentDictionaryCache
to illustrate the various Fluent extension methods provided by the API
//use the simplest cache, which wraps a dictionary
//other cache implementations are provided in additional nuget packages
ICache myCache = new FluentCache.Simple.FluentDictionaryCache();
//create a wrapper around our Repository
//wrapper will allow us to cache the results of various Repository methods
Repository repo = new Repository();
Cache<Repository> myRepositoryCache = myCache.WithSource(repo);
//create and execute a CacheStrategy using Fluent Extension methods
string resource = myRepositoryCache.Method(r => r.RetrieveResource())
.ExpireAfter(TimeSpan.FromMinutes(30))
.GetValue();
Cache Implementation | FluentCache Type | NuGet Package |
---|---|---|
ConcurrentDictionary |
FluentDictionaryCache |
FluentCache |
System.Runtime.Caching.MemoryCache |
FluentMemoryCache |
FluentCache.RuntimeCaching |
Microsoft.Extensions.Caching.Memory.MemoryCache |
FluentMemoryCache |
FluentCache.Microsoft.Extensions.Caching.Memory |
Microsoft.Extensions.Caching.Redis.RedisCache |
FluentRedisCache |
FluentCache.Microsoft.Extensions.Caching.Redis |
Microsoft.Extensions.Caching.Memory.IMemoryCache |
FluentIMemoryCache |
FluentCache.Microsoft.Extensions.Caching.Abstractions |
Microsoft.Extensions.Caching.Distributed.IDistributedCache |
FluentIDistributedCache |
FluentCache.Microsoft.Extensions.Caching.Abstractions |
Other caches can implement the FluentCache.ICache
interface
.ExpireAfter()
now supports a callback for determining sliding expiration based on cached valueFluentCache
now only requires .NET Standard 1.1- Assemblies are signed
- Support for .NET Standard 2.0
- New implementations for
Microsoft.Extensions.Caching
Microsoft.Extensions.Caching.Memory.IMemoryCache
added toFluentCache.Microsoft.Extensions.Caching.Abstractions
nuget packageMicrosoft.Extensions.Caching.Memory.MemoryCache
added toFluentCache.Microsoft.Extensions.Caching.Memory
nuget packageMicrosoft.Extensions.Caching.Distributed.IDistributedCache
added toFluentCache.Microsoft.Extensions.Caching.Abstractions
nuget packageMicrosoft.Extensions.Caching.Redis.RedisCache
added toFluentCache.Microsoft.Extensions.Caching.Redis
nuget package
- Support for caching methods on generic repositories
The previous FluentCache.Redis
package is deprecated in favor of FluentCache.Microsoft.Extensions.Caching.Redis