CleanArchitecture.Extensions.Caching 0.2.9

dotnet add package CleanArchitecture.Extensions.Caching --version 0.2.9
                    
NuGet\Install-Package CleanArchitecture.Extensions.Caching -Version 0.2.9
                    
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="CleanArchitecture.Extensions.Caching" Version="0.2.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanArchitecture.Extensions.Caching" Version="0.2.9" />
                    
Directory.Packages.props
<PackageReference Include="CleanArchitecture.Extensions.Caching" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CleanArchitecture.Extensions.Caching --version 0.2.9
                    
#r "nuget: CleanArchitecture.Extensions.Caching, 0.2.9"
                    
#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.
#:package CleanArchitecture.Extensions.Caching@0.2.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CleanArchitecture.Extensions.Caching&version=0.2.9
                    
Install as a Cake Addin
#tool nuget:?package=CleanArchitecture.Extensions.Caching&version=0.2.9
                    
Install as a Cake Tool

CleanArchitecture.Extensions.Caching

Simple setup for the JaysonTaylorCleanArchitectureBlank template.

Step 1 - Install the package

Install in both Application and Infrastructure projects:

dotnet add src/Application/Application.csproj package CleanArchitecture.Extensions.Caching
dotnet add src/Infrastructure/Infrastructure.csproj package CleanArchitecture.Extensions.Caching

Step 2 - Register caching services (Infrastructure layer)

File: src/Infrastructure/DependencyInjection.cs

Add the package and register caching:

using CleanArchitecture.Extensions.Caching;
using CleanArchitecture.Extensions.Caching.Options;

public static void AddInfrastructureServices(this IHostApplicationBuilder builder)
{
    // existing registrations...

    builder.Services.AddCleanArchitectureCaching(options =>
    {
        options.DefaultNamespace = "MyApp";
        options.MaxEntrySizeBytes = 256 * 1024;
        // Set Backend = CacheBackend.Distributed to force shared cache when IDistributedCache is configured.
    }, queryOptions =>
    {
        queryOptions.DefaultTtl = TimeSpan.FromMinutes(5);
    });
}

Step 3 - Register the caching behavior (Application layer)

File: src/Application/DependencyInjection.cs

Insert the caching behavior after Validation and before Performance:

using CleanArchitecture.Extensions.Caching.Behaviors;

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
    cfg.AddOpenRequestPreProcessor(typeof(LoggingBehaviour<>));
    cfg.AddOpenBehavior(typeof(UnhandledExceptionBehaviour<,>));
    cfg.AddOpenBehavior(typeof(AuthorizationBehaviour<,>));
    cfg.AddOpenBehavior(typeof(ValidationBehaviour<,>));
    // Add caching behavior below this line ----
    cfg.AddOpenBehavior(typeof(QueryCachingBehavior<,>));
    // Add caching behavior above this line ----
    cfg.AddOpenBehavior(typeof(PerformanceBehaviour<,>));
});

Step 4 - Opt-in queries

Only queries that opt in are cached by default. Use the marker interface or attribute:

using CleanArchitecture.Extensions.Caching;
using CleanArchitecture.Extensions.Caching.Abstractions;

[CacheableQuery]
public record GetTodosQuery : IRequest<TodosVm>;

// or
public record GetUserQuery(int Id) : IRequest<UserDto>, ICacheableQuery;

// If your query cannot be serialized for hashing (e.g., contains delegates or HttpContext),
// implement ICacheKeyProvider to supply a deterministic hash:
public record GetReportQuery(Func<int> Factory) : IRequest<ReportDto>, ICacheableQuery, ICacheKeyProvider
{
    public string GetCacheHash(ICacheKeyFactory keyFactory) => keyFactory.CreateHash(new { Version = 1 });
}

Step 5 - What to expect

  • Only queries marked with ICacheableQuery or [CacheableQuery] are cached by default.
  • First request is a cache miss; the second identical request is a cache hit.
  • Default TTL is 5 minutes; override with QueryCachingBehaviorOptions.DefaultTtl or TtlByRequestType.
  • Debug logs show Cache hit and Cache miss messages when caching is active.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CleanArchitecture.Extensions.Caching:

Package Downloads
CleanArchitecture.Extensions.Multitenancy.Caching

Tenant-aware caching integration for Clean Architecture multitenancy, including cache scope binding and cache-scope warnings.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.9 100 1/13/2026
0.2.8 97 1/13/2026
0.2.7 99 1/13/2026
0.2.6 96 1/12/2026
0.2.5 107 1/8/2026
0.2.4 102 1/3/2026
0.2.3 119 1/1/2026
0.2.2 116 1/1/2026
0.2.1 107 12/29/2025
0.2.0 100 12/29/2025
0.1.8-preview.3 53 12/28/2025
0.1.7 122 12/26/2025
0.1.6 425 12/10/2025
0.1.6-preview.13 370 12/10/2025