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
<PackageReference Include="CleanArchitecture.Extensions.Caching" Version="0.2.9" />
<PackageVersion Include="CleanArchitecture.Extensions.Caching" Version="0.2.9" />
<PackageReference Include="CleanArchitecture.Extensions.Caching" />
paket add CleanArchitecture.Extensions.Caching --version 0.2.9
#r "nuget: CleanArchitecture.Extensions.Caching, 0.2.9"
#:package CleanArchitecture.Extensions.Caching@0.2.9
#addin nuget:?package=CleanArchitecture.Extensions.Caching&version=0.2.9
#tool nuget:?package=CleanArchitecture.Extensions.Caching&version=0.2.9
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
ICacheableQueryor[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.DefaultTtlorTtlByRequestType. - Debug logs show
Cache hitandCache missmessages when caching is active.
| Product | Versions 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. |
-
net10.0
- MediatR (>= 13.1.0)
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.0)
- Microsoft.Extensions.Logging (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
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 |