Class DefaultCacheKeyBuilder
Default implementation of cache key builder that creates consistent cache keys for dispatch actions. Uses JSON serialization for generating stable keys based on action content.
public sealed class DefaultCacheKeyBuilder : ICacheKeyBuilder
- Inheritance
-
DefaultCacheKeyBuilder
- Implements
- Inherited Members
- Extension Methods
Constructors
DefaultCacheKeyBuilder(DispatchJsonSerializer, ILogger<DefaultCacheKeyBuilder>?)
Default implementation of cache key builder that creates consistent cache keys for dispatch actions. Uses JSON serialization for generating stable keys based on action content.
public DefaultCacheKeyBuilder(DispatchJsonSerializer serializer, ILogger<DefaultCacheKeyBuilder>? logger = null)
Parameters
serializerDispatchJsonSerializerThe JSON serializer for creating consistent keys from action data.
loggerILogger<DefaultCacheKeyBuilder>Optional logger for reporting cache key generation issues.
Methods
CreateKey(IDispatchAction, IMessageContext)
Builds a stable, hash-safe cache key for the given message and context, or null when no cache identity can be derived for the action.
[RequiresUnreferencedCode("Cache key generation may use JSON serialization which requires unreferenced code")]
[RequiresDynamicCode("Cache key generation may use JSON serialization which requires dynamic code generation")]
public string? CreateKey(IDispatchAction action, IMessageContext context)
Parameters
actionIDispatchActionThe dispatch action to create a key for.
contextIMessageContextThe message context.
Returns
- string
A stable cache key string, or null when no cache identity can be derived for
action— for example the action declaresICacheable<T>but its key cannot be resolved, or the action cannot be serialized. A null result means "do not cache": the caller MUST bypass the cache and invoke the underlying operation. Implementations MUST be infallible for a "cannot derive a key" condition — return null rather than throwing.
CreateKey(string, string?, string?)
Builds the stored cache key for a logical key under a tenant/user identity, applying the same identity folding and hashing transform as CreateKey(IDispatchAction, IMessageContext) — without a live IMessageContext.
public string CreateKey(string logicalKey, string? tenantId, string? userId)
Parameters
logicalKeystringThe logical cache identity (e.g. the value a cacheable query exposes via
GetCacheKey()).tenantIdstringThe tenant identity the entry was stored under, or null for the global tenant.
userIdstringThe user identity the entry was stored under, or null for an anonymous user.
Returns
- string
The storage key that a cacheable query would have stored for
logicalKeyunder the given identity. Use this to turn a direct invalidation key into the exact key to remove — a raw logical key can never equal the stored hashed key.
Remarks
This overload performs no serialization and is AOT- and trimming-safe. It is intended for direct-key cache invalidation, where the tenant/user are derived from the invalidating dispatch's context rather than the original cached request.