Skip to content

TailoredApps.Shared.Mediator.Caching

NuGet License: MIT


Description

A lightweight package defining ICachableRequest<TResponse> — a contract for mediator requests that name their own cache key. A request implementing this interface provides a GetCacheKey() method that generates a unique cache key for that particular query instance.

The package contains the contract only: it has no services and nothing to register. CachingBehavior from TailoredApps.Shared.Mediator does not read ICachableRequest<TResponse> — it caches a request only when an ICachePolicy<TRequest, TResponse> is registered for it. Use the interface to keep the key logic in the request itself, and add a cache policy that returns that key (shown below) when the response is to be cached by that behavior.


Installation

dotnet add package TailoredApps.Shared.Mediator.Caching

Usage example

using TailoredApps.Shared.Mediator.Caching;
using ForkForward.Mediator;

// Request with built-in cache key logic
public class GetUserProfileQuery : ICachableRequest<UserProfileDto>
{
    public int UserId { get; set; }
    public string Language { get; set; } = "pl";

    // Unique key that takes the query parameters into account
    public string GetCacheKey() => $"user-profile:{UserId}:{Language}";
}

// Handler — a standard request handler
public class GetUserProfileQueryHandler : IRequestHandler<GetUserProfileQuery, UserProfileDto>
{
    private readonly IUserRepository _repo;

    public GetUserProfileQueryHandler(IUserRepository repo) => _repo = repo;

    public async Task<UserProfileDto> Handle(GetUserProfileQuery request, CancellationToken ct)
    {
        var user = await _repo.GetByIdAsync(request.UserId, ct);
        return user.ToProfileDto(request.Language);
    }
}

Caching the request with CachingBehavior

using TailoredApps.Shared.Mediator.Interfaces.Caching;

// The policy is what CachingBehavior looks for; it hands back the key the request defines
public class GetUserProfileQueryCachePolicy : ICachePolicy<GetUserProfileQuery, UserProfileDto>
{
    public string GetCacheKey(GetUserProfileQuery request) => request.GetCacheKey();
}

Integration with CachingBehavior

ICachableRequest is a marker interface — on its own it does not trigger caching, and CachingBehavior from the TailoredApps.Shared.Mediator package does not look at it. A request is cached only when an ICachePolicy<TRequest, TResponse> is registered for it (PipelineRegistration.RegisterPipelineBehaviors(assembly) scans for policies). Without a policy the request above runs its handler every time.

Trimming and NativeAOT

The package holds a contract only and is safe for trimming and NativeAOT. In such an application a policy that returns request.GetCacheKey() is also the simplest way to a cache key: the default key of ICachePolicy needs reflection. The policy is registered by name (AddCachePolicy<,,>()) - see Mediator: Trimming and NativeAOT.


API Reference

Type Kind Description
ICachableRequest<TResponse> Interface Extends IRequest<TResponse>; requires GetCacheKey()
ICachableRequest.GetCacheKey() Method Returns a unique cache key for this request instance

🤖 AI Agent Prompt

## TailoredApps.Shared.Mediator.Caching - instructions for an AI agent

You are using ICachableRequest to give mediator requests their own cache key.

### Registration
Nothing to register: the package contains one interface, no services and no configuration.

### Usage
```csharp
public class GetProductQuery : ICachableRequest<ProductDto>
{
    public int Id { get; set; }
    public string GetCacheKey() => $"product:{Id}";
}

// Needed for CachingBehavior (TailoredApps.Shared.Mediator) to cache the request:
public class GetProductQueryCachePolicy : ICachePolicy<GetProductQuery, ProductDto>
{
    public string GetCacheKey(GetProductQuery request) => request.GetCacheKey();
}
```

### Rules
- ICachableRequest<TResponse> extends IRequest<TResponse>
- GetCacheKey() must return a unique key for this combination of parameters
- The interface alone does not cache, and CachingBehavior from TailoredApps.Shared.Mediator does not read it
- CachingBehavior caches only requests that have a registered ICachePolicy<TRequest, TResponse> — add a policy that returns request.GetCacheKey()
- An application with PublishAot: register the policy by name - new PipelineRegistration(services).AddCachePolicy<TRequest, TResponse, TPolicy>() - scanning is not available there