Hyperbee.Pipeline.Caching 1.2.0

dotnet add package Hyperbee.Pipeline.Caching --version 1.2.0                
NuGet\Install-Package Hyperbee.Pipeline.Caching -Version 1.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="Hyperbee.Pipeline.Caching" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hyperbee.Pipeline.Caching --version 1.2.0                
#r "nuget: Hyperbee.Pipeline.Caching, 1.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 Hyperbee.Pipeline.Caching as a Cake Addin
#addin nuget:?package=Hyperbee.Pipeline.Caching&version=1.2.0

// Install Hyperbee.Pipeline.Caching as a Cake Tool
#tool nuget:?package=Hyperbee.Pipeline.Caching&version=1.2.0                

Hyperbee.Pipeline.Caching

The Hyperbee.Pipeline.Caching library is a set of extentsions to Hyperbee.Pipeline that adds support for caching within a pipeline using Microsoft.Extensions.Caching libraries.

Memory Cache Examples

For simple pipelines the previous step's return value can be used as the key:

// Takes a string and returns a number
var command = PipelineFactory
    .Start<string>()
    .PipeCacheAsync( CharacterCountAsync )
    .Build();

var result = await command( factory.Create( logger ), "test" );

Assert.AreEqual( 4, result );

Or for more complex the options callback can be used to customize how the results will be cached.

// Takes a string and returns a number
var command = PipelineFactory
    .Start<string>()
    .PipeCacheAsync( CharacterCountAsync,
        ( input, options ) =>
        {
            options.Key = $"custom/{input}";
            options.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours( 1 );
            return options;
        } )
    .Build();

var result = await command( factory.Create( logger ), "test" );

Assert.AreEqual( 4, result );

When a set of steps should cache the overload that takes a builder can be used. In this case the inital input will be used at the key and the final step of the nested pipeline will be cached

// Takes a string and returns a number
var command = PipelineFactory
    .Start<string>()
    .PipeCacheAsync( b => b
        .PipeAsync( CharacterCountAsync )
        .Pipe( (ctx, arg) => arg + 100 ))
    .Build();

var result = await command( factory.Create( logger ), "test" );

Assert.AreEqual( 104, result );

Distributed Cache

Distributed Caches work in the same way, but requires the registration of a IDistributedCache, an optional ICacheSerializer serializer (defaults to Json) and only supports Async methods

var command = PipelineFactory
    .Start<string>()
    .PipeDistributedCacheAsync( CharacterCountAsync )
    .Build();

Dependacy Injection

Because this uses the existing DI built into pipelines, caching can be configured with an existing cache.

Memory Cache

// Add Memory Cache
services.AddMemoryCache();

// Share with the pipelines
services.AddPipeline( includeAllServices: true );

Or defined seperately as part of the container use for the pipelines:

services.AddPipeline( (factoryServices, rootProvider) =>
{
    factoryServices.AddMemoryCache();
    factoryServices.AddPipelineDefaultCacheSettings( absoluteExpirationRelativeToNow: TimeSpan.FromHours( 1 ) )
} );

Distributed Cache

An Establish caching services such as SQL needs to be configured:

services.AddDistributedSqlServerCache(options =>
{
    options.ConnectionString = builder.Configuration.GetConnectionString(
        "DistCache_ConnectionString");
    options.SchemaName = "dbo";
    options.TableName = "TestCache";
});

An Optional ICacheSerializer can be registered. If one is missing the JsonCacheSerializer which uses System.Text.Json.JsonSerializer is used.

services.AddTransient<ICacheSerializer, JsonCacheSerializer>()
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 111 8/9/2024
1.1.6 84 5/3/2024
1.1.6-develop.240702125702 60 7/2/2024
1.1.6-develop.240506164152 66 5/6/2024