Class CacheDispatcher
- Namespace
- adas_core.Application.Services.Caching
- Assembly
- adas-core.Application.dll
Orquestador de caché. Selecciona el backend (Redis, InMemory, None) según CacheSettings y la entidad del key. Implementa ICacheService y delega en el backend elegido.
public class CacheDispatcher : ICacheService
- Inheritance
-
CacheDispatcher
- Implements
- Inherited Members
- Extension Methods
Constructors
CacheDispatcher(RedisService, CacheService, NoCacheService, CacheSettings)
Orquestador de caché. Selecciona el backend (Redis, InMemory, None) según CacheSettings y la entidad del key. Implementa ICacheService y delega en el backend elegido.
public CacheDispatcher(RedisService redis, CacheService memory, NoCacheService noop, CacheSettings cacheSettings)
Parameters
redisRedisServicememoryCacheServicenoopNoCacheServicecacheSettingsCacheSettings
Methods
CleanCache()
Clears all cached data from both the in-memory cache and the Redis cache, ensuring that stale entries are removed across all configured cache providers.
public void CleanCache()
DeleteByPatternAsync(string)
Deletes entries matching the specified pattern from both Redis and in-memory storage, returning the total count of deleted entries.
public Task<long> DeleteByPatternAsync(string pattern)
Parameters
patternstringThe pattern used to match entries for deletion in both storage backends.
Returns
DeleteObjectAsync(string)
Asynchronously deletes the object identified by the specified key by delegating the operation to the backend selected for that key.
public Task DeleteObjectAsync(string key)
Parameters
keystringThe identifier of the object to delete, also used to resolve the responsible backend.
Returns
- Task
A task that represents the asynchronous delete operation.
GetObjectAsync<T>(string, bool)
Retrieves an object of type T from the backend selected by the given key, with an option to trigger an update.
public Task<T?> GetObjectAsync<T>(string key, bool upd = true)
Parameters
keystringThe identifier used to select the appropriate backend and to look up the object.
updboolIndicates whether the underlying backend should perform an update during retrieval. Defaults to
true.
Returns
- Task<T>
A task that represents the asynchronous retrieval operation, containing the object of type
Tornullif not found.
Type Parameters
T
GetObjectAsync<T>(string, TimeSpan?, bool)
Retrieves an object of the specified type from the backend selected by the given key, optionally applying a time-to-live and update behavior.
public Task<T?> GetObjectAsync<T>(string key, TimeSpan? ttl, bool upd)
Parameters
keystringThe identifier used to select the backend and locate the stored object.
ttlTimeSpan?An optional time-to-live applied to the object; if null, the backend's default is used.
updboolA flag indicating whether the retrieval should update the object's state (e.g., refresh expiration).
Returns
- Task<T>
A task containing the deserialized object, or null if the object is not found in the selected backend.
Type Parameters
T
GetOrSetObjectAsync<T>(string, Func<Task<T>>, TimeSpan?, CancellationToken)
Asynchronously retrieves the object associated with the specified key from the selected backend, or sets it using the provided factory if it is not already cached.
public Task<T> GetOrSetObjectAsync<T>(string key, Func<Task<T>> factory, TimeSpan? ttl = null, CancellationToken cancellationToken = default)
Parameters
keystringThe cache key used to identify the object and to select the appropriate backend.
factoryFunc<Task<T>>A delegate that asynchronously produces the value to store when the key is not present in the selected backend.
ttlTimeSpan?An optional time-to-live duration for the cached object. If null, the backend's default expiration is applied.
cancellationTokenCancellationTokenA token to observe while waiting for the operation to complete.
Returns
- Task<T>
A task that represents the asynchronous operation, containing the retrieved or newly created object of type
T.
Type Parameters
T
GetOrSetObjectAsync<T>(GroupedField, ObjectId, Func<Task<T>>, TimeSpan?, CancellationToken)
Asynchronously retrieves the object associated with the specified grouped field and patient, or creates and stores it using the provided factory if it does not exist. The appropriate backend is selected based on the grouped field and patient identifier before the underlying get-or-set operation is performed.
public Task<T> GetOrSetObjectAsync<T>(GroupedField groupedField, ObjectId patientId, Func<Task<T>> factory, TimeSpan? ttl = null, CancellationToken cancellationToken = default)
Parameters
groupedFieldGroupedFieldThe grouped field that determines the target backend and identifies the cached object.
patientIdObjectIdThe identifier of the patient whose object is being retrieved or created.
factoryFunc<Task<T>>The asynchronous factory used to create the object when no cached value is available.
ttlTimeSpan?An optional time-to-live applied to the cached object. When
null, the backend's default expiration is used.cancellationTokenCancellationTokenThe token to observe for canceling the asynchronous operation.
Returns
- Task<T>
A task that represents the asynchronous get-or-set operation, containing the retrieved or newly created object.
Type Parameters
TThe type of the object to retrieve or create.
GetOrSetValueAsync(string, Func<Task<string>>, TimeSpan?)
Asynchronously retrieves the value associated with the specified key from the backend selected for that key, or loads and stores it using the provided loader function if it is not already present. Supports an optional time-to-live (TTL) for the cached entry, and the returned value may be null.
public Task<string?> GetOrSetValueAsync(string key, Func<Task<string>> loader, TimeSpan? ttl = null)
Parameters
keystringThe key used to identify the cached value and to select the appropriate backend.
loaderFunc<Task<string>>An asynchronous function that produces the value to cache when no existing entry is found.
ttlTimeSpan?An optional time-to-live duration after which the cached entry expires. If null, the backend's default TTL is used.
Returns
- Task<string>
A task that represents the asynchronous operation, containing the cached or loaded string value, or null if no value could be obtained.
GetValue(string)
Retrieves the value associated with the specified key by delegating the lookup to a backend selected for that key.
public string? GetValue(string key)
Parameters
keystringThe key used to select the backend and retrieve the associated value.
Returns
- string
The value associated with the key, or
nullif the selected backend returns no value.
SetObjectAsync<T>(string, T, bool)
Asynchronously stores an object in the backend selected by the specified key.
public Task SetObjectAsync<T>(string key, T obj, bool upd = true)
Parameters
keystringThe key used to select the backend and identify the stored object.
objTThe object to store in the selected backend.
updboolIndicates whether an update operation should be performed. Defaults to
true.
Returns
- Task
A task that represents the asynchronous store operation.
Type Parameters
TThe type of the object to store.
SetObjectAsync<T>(string, T, TimeSpan?, bool)
Asynchronously stores an object in the backend selected for the specified key, with an optional time-to-live and update flag.
public Task SetObjectAsync<T>(string key, T obj, TimeSpan? ttl, bool upd)
Parameters
keystringThe key used to select the target backend and identify the object.
objTThe object to store.
ttlTimeSpan?The optional time-to-live duration for the stored object.
updboolIndicates whether to update an existing entry or create a new one.
Returns
- Task
A task that represents the asynchronous set operation.
Type Parameters
T
SetValue(string, string)
Sets the value associated with the specified key by selecting the appropriate backend for that key and delegating the assignment to it.
public void SetValue(string key, string value)