FloxDc.CacheFlow 1.11.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FloxDc.CacheFlow --version 1.11.0                
NuGet\Install-Package FloxDc.CacheFlow -Version 1.11.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="FloxDc.CacheFlow" Version="1.11.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FloxDc.CacheFlow --version 1.11.0                
#r "nuget: FloxDc.CacheFlow, 1.11.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 FloxDc.CacheFlow as a Cake Addin
#addin nuget:?package=FloxDc.CacheFlow&version=1.11.0

// Install FloxDc.CacheFlow as a Cake Tool
#tool nuget:?package=FloxDc.CacheFlow&version=1.11.0                

CacheFlow

CacheFlow is a cache management system for .Net Core. It ables you not only use cache but handles various complex use cases i.e. value serialization, default values, the Get or Set pattern.

Table of Content

The library removes boilerplate code and builds on top of standard caching interfaces of .Net, so most of its methods have same names as regular caching services and use same option types. In addition to standard features it adds a little bit of comfort. For instance one may use the GetOrSet method instead of Get and Set separately. Just use this:

public T CacheFlowWay<T>(string key, TimeSpan expirationTime)
    => GetOrSet(key, CalculateResult, timeout)

instead of this:

public T UsualWay<T>(string key, TimeSpan expirationTime)
{
    if (Cache.TryGetValue(key, out T result))
        return result;

    result = CalculateResult();

    Cache.Set(key, result, expirationTime);
    return result;
}

And that's the simpliest example. In addition the library contains safity checks, serialization, logging, and other handy features.

Quick Start

Install a package via NuGet

PM> Install-Package FloxDc.CacheFlow -Version 1.10.0

And add following lines to your Startup.cs file:

services.AddMemoryCache()
    .AddStackExchangeRedisCache(options => 
    { 
        options.Configuration = Configuration["Redis:Endpoint"]; 
    })
    .AddDoubleFlow();

Caching strategies

In-Memory

MemoryFlow
Method Description
GetOrSet Tries to get a value from a cache, and sets it if no entries were found
GetOrSetAsync Tries to get a value from a cache, and sets it if no entries were found
Remove Removes a specified cache entry
Set Sets a cache entry with a provided value
TryGetValue Tries to get a value from a cache

Distributed

DistributedFlow
Method Description
GetAsync Gets a value from a cache
GetOrSet Tries to get a value from a cache, and sets it if no entries were found
GetOrSetAsync Tries to get a value from a cache, and sets it if no entries were found
Refresh Refreshes a specified cache entry
RefreshAsync Refreshes a specified cache entry
Remove Removes a specified cache entry
RemoveAsync Removes a specified cache entry
Set Sets a cache entry with a provided value
SetAsync Sets a cache entry with a provided value
TryGetValue Tries to get a value from a cache

Both In-Memory And Disitibuted

When you work with immutable data you may want to cache it both distributed and in-memory. There is a DoubleFlow approach for that case. Note some methods of the DoubleFlow may return a ValueTask where the DistributedFlow returns a Task.

DoubleFlow
Method Description
GetAsync Gets a value from a cache
GetOrSet Tries to get a value from a cache, and sets it if no entries were found
GetOrSetAsync Tries to get a value from a cache, and sets it if no entries were found
Refresh Refreshes a specified cache entry
RefreshAsync Refreshes a specified cache entry
Remove Removes a specified cache entry
RemoveAsync Removes a specified cache entry
Set Sets a cache entry with a provided value
SetAsync Sets a cache entry with a provided value
TryGetValue Tries to get a value from a cache

Options

There is a set of options you can use to configure CacheFlow:

Parameter Default Meaning
CacheKeyDelimiter :: Sets a delimiter which uses for key construction
CacheKeyPrefix Sets a prefix to all cache keys within an app
DataLoggingLevel Normal Sets a logging level of cache values and execution points, like hit, miss, data calculation etc.
SuppressCacheExceptions true Enables exception throwing suppression, for error caused by caching service itself. Suitable when your app tolerate for invalid cache requests.
Data Logging Levels

The library can produce monitoring events of different types:

Level Behavior
Disabled Emits only tracing events
Normal Traces events, logs operations, their result states, and cache keys
Sensitive Traces events, logs operations, their result states, cache keys, and cached values
Exception Suppression

Warning!

By default exception supression is on and it may slow down your application. Turn off the option if you confident in your caching system.

Named instances

You could use typed service insances to autoprefix cache keys with the class name:

public MyClass(IMemoryFlow<MyClass> cache)

Useful if one want to find a key in a database.

Time Spans

If you want to avoid overlaps in caching, you may use following TimeSpan extensions:

Method Name Time Frame
BeforeMinuteEnds up to 1 minute
BeforeTwoMinutesEnd up to 2 minutes
BeforeFiveMinutesEnd up to 5 minutes
BeforeTenMinutesEnd up to 10 minutes
BeforeQuarterHourEnds up to 15 minutes
BeforeHalfHourEnds up to 30 minutes
BeforeHourEnds up to 60 minutes
BeforeDayEnds up to 24 hours

Extensions

Serialization

By default CacheFlow uses the System.Text.Json serializer. Add no extension metoods on a configuration step to use that method. There are two more pre-built options, and also you could implement your own serializer as well.

Json

A Newtonsoft.Json serializer.

Install a package via NuGet

PM> Install-Package FloxDc.CacheFlow.Json -Version 1.10.0

And add following lines to your configuration:

services.AddMemoryFlow()
    .AddCacheFlowJsonSerialization();
MessagePack

A neuecc's MessagePack serializer.

Install a package via NuGet

PM> Install-Package FloxDc.CacheFlow.MessagePack -Version 1.10.0

And add following lines to Startup.cs:

var messagePackOptions = MessagePackSerializerOptions.Standard;

services.AddMemoryFlow()
    .AddCacheFlowMessagePackSerialization(messagePackOptions, StandardResolver.Instance);

Keep in mind MessagePack requires to specify a structured map of serialized data.

Telemetry

The package supports standard .Net activity-based telemetry. Register a source from a namespace FloxDc.CacheFlow.Telemetry.ActivitySourceHelper to enable it. Here's an example of OpenTelemetry configuration with the source:

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    // other configs
    .AddSource(ActivitySourceHelper.CacheFlowActivitySourceName)
    // other configs
    .Build();    
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. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on FloxDc.CacheFlow:

Package Downloads
FloxDc.CacheFlow.Json

Newtonsoft Json serialization for FloxDc's Cache Flow

FloxDc.CacheFlow.MessagePack

Message Pack serialization for FloxDc's Cache Flow

FloxDc.CacheFlow.OpenTelemetry

An OpenTelemetry instrumentation for CacheFlow

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.13.0 469 10/25/2024
1.12.0 28,049 11/22/2022
1.11.0 11,346 9/30/2022
1.10.0 22,539 12/24/2021
1.10.0-beta3 764 11/21/2021
1.9.1 7,704 2/20/2021
1.8.0 2,747 11/12/2020
1.7.0 4,469 5/30/2020
1.6.4 1,529 3/18/2020
1.6.3 999 2/23/2020
1.6.2 788 1/15/2020
1.6.1 726 1/12/2020
1.6.0 843 11/1/2019
1.5.2 1,216 6/11/2019
1.5.2-beta1 507 6/7/2019
1.5.1 1,403 3/20/2019
1.5.0 599 3/20/2019
1.4.0 663 12/23/2018
1.3.1 884 9/20/2018
1.3.0 795 9/20/2018
1.3.0-beta2 645 9/18/2018
1.3.0-beta1 792 7/10/2018
1.3.0-alpha 727 9/18/2018
1.2.0 1,036 7/6/2018

- JsonOptions are configurable from a startup now