SharedHybridCache class
Namespace: Tenantry.Caching · Package: Tenantry.Caching · API reference
The HybridCache for entries every tenant shares (exchange rates, reference data), where IsolateCaches() makes the injected HybridCache keep entries per tenant. Inject it where sharing is meant, so the constructor says so. Its keys and tags are its own: they never name a tenant's entry, and a tenant's never name one of these.
It is the HybridCache registered before IsolateCaches(), with its serializers and options, and works with or without a current tenant. A factory here runs with no current tenant, whoever calls, so what it loads cannot be one tenant's data: a query through a tenant's DbContext fails, as it does outside a tenant, rather than caching that tenant's rows for every tenant. It runs on the thread pool, without the caller's async context (its activity and log scopes too). A key or tag of * (every entry) means every shared entry. For a keyed HybridCache, inject [FromKeyedServices(key)] SharedHybridCache with the same key.
public sealed class ExchangeRates(SharedHybridCache cache)
{
public ValueTask<decimal> GetAsync(string currency, CancellationToken ct) =>
cache.GetOrCreateAsync($"fx:{currency}", async token => await LoadRateAsync(currency, token), cancellationToken: ct);
}public sealed class SharedHybridCache : HybridCache, IDisposableInherits HybridCache.
Implements IDisposable.
Methods
Dispose()
Disposes the cache it wraps, when that cache was created for it and is disposable.
public void Dispose()GetOrCreateAsync<TState, T>(string, TState, Func<TState, CancellationToken, ValueTask<T>>, HybridCacheEntryOptions?, IEnumerable<string>?, CancellationToken)
Asynchronously gets the value associated with the key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found.
public override ValueTask<T> GetOrCreateAsync<TState, T>(string key, TState state, Func<TState, CancellationToken, ValueTask<T>> factory, HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default)Type parameters:
TState: The type of additional state required byfactory.T: The type of the data being considered.
Parameters:
keystring: The key of the entry to look for or create.stateTState: The state required forfactory.factoryFunc<TState, CancellationToken, ValueTask<T>>: Provides the underlying data service if the data is not available in the cache.optionsHybridCacheEntryOptions: Additional options for this cache entry.tagsIEnumerable<string>: The tags to associate with this cache item.cancellationTokenCancellationToken: TheCancellationTokenused to propagate notifications that the operation should be canceled.
Returns: ValueTask<T>: The data, either from cache or the underlying data service.
RemoveAsync(IEnumerable<string>, CancellationToken)
Removes the shared entries keys; null removes none.
public override ValueTask RemoveAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)Parameters:
keysIEnumerable<string>: The entries' keys, as they were written here.cancellationTokenCancellationToken: Cancels the removal.
Returns: ValueTask: A task that completes when the entries are removed.
RemoveAsync(string, CancellationToken)
Removes the shared entry key.
public override ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default)Parameters:
keystring: The entry's key, as it was written here.cancellationTokenCancellationToken: Cancels the removal.
Returns: ValueTask: A task that completes when the entry is removed.
RemoveByTagAsync(IEnumerable<string>, CancellationToken)
Removes the shared entries tagged with any of tags; null removes none.
public override ValueTask RemoveByTagAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)Parameters:
tagsIEnumerable<string>: The tags, as entries were written with them here.cancellationTokenCancellationToken: Cancels the removal.
Returns: ValueTask: A task that completes when the entries are removed.
RemoveByTagAsync(string, CancellationToken)
Removes the shared entries tagged tag; * removes every shared entry.
public override ValueTask RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)Parameters:
tagstring: The tag, as entries were written with it here.cancellationTokenCancellationToken: Cancels the removal.
Returns: ValueTask: A task that completes when the entries are removed.
SetAsync<T>(string, T, HybridCacheEntryOptions?, IEnumerable<string>?, CancellationToken)
Asynchronously sets or overwrites the value associated with the key.
public override ValueTask SetAsync<T>(string key, T value, HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default)Type parameters:
T: The type of the data being considered.
Parameters:
keystring: The key of the entry to create.valueT: The value to assign for this cache entry.optionsHybridCacheEntryOptions: Additional options for this cache entry.tagsIEnumerable<string>: The tags to associate with this cache entry.cancellationTokenCancellationToken: TheCancellationTokenused to propagate notifications that the operation should be canceled.
Returns: ValueTask
ITenantDistributedCache interface
The registered IDistributedCache, with every key under the current tenant's prefix, for code that uses IDistributedCache directly and keeps data per tenant.
EfCoreIsolationOptions class
Options for EF Core tenant isolation: the application's, set with tenant.ConfigureEfCoreIsolationoptions => …, or one context's, set with options.UseTenantryo => ….