Class CacheService
- Namespace
- adas_core.Application.Services.Caching
- Assembly
- adas-core.Application.dll
Implementación de caché en memoria con soporte de locking seguro mediante LockManagerService + InMemoryLockProvider. Compatible con la interfaz ICacheService incluyendo GetOrSet.
public class CacheService : ICacheService
- Inheritance
-
CacheService
- Implements
- Inherited Members
- Extension Methods
Constructors
CacheService(LockManagerService)
Implementación de caché en memoria con soporte de locking seguro mediante LockManagerService + InMemoryLockProvider. Compatible con la interfaz ICacheService incluyendo GetOrSet.
public CacheService(LockManagerService lockManager)
Parameters
lockManagerLockManagerService
Methods
CleanCache()
Clears all entries from the in-memory cache, removing any previously stored data.
public void CleanCache()
DeleteByPatternAsync(string)
Asynchronously deletes cache entries whose keys contain the specified pattern, where asterisk (*) characters in the pattern are treated as wildcards (stripped and matched as substrings). Returns the number of entries successfully removed.
public Task<long> DeleteByPatternAsync(string pattern)
Parameters
patternstringThe pattern to match against cache keys. Asterisk (*) characters are removed and the remaining text is used as a substring match.
Returns
- Task<long>
A task that represents the asynchronous operation, containing the count of entries that were removed.
DeleteObjectAsync(string)
Asynchronously removes the object associated with the specified key from the in-memory store. The operation succeeds silently whether or not the key exists.
public Task DeleteObjectAsync(string key)
Parameters
keystringThe identifier of the object to delete.
Returns
GetObjectAsync<T>(string, bool)
Asynchronously retrieves an object of type T from the in-memory cache using the specified key.
Returns the stored value cast to T if the key exists, or default (null for reference types) if the key is not found.
public Task<T?> GetObjectAsync<T>(string key, bool updateExpiration = true)
Parameters
keystringThe cache key used to look up the stored object.
updateExpirationboolIndicates whether the entry's expiration should be refreshed on access. Not currently used by this implementation.
Returns
- Task<T>
A Task<TResult> containing the cached value cast to
T, ornullif no entry exists for the given key.
Type Parameters
T
GetObjectAsync<T>(string, TimeSpan?, bool)
Retrieves an object of type T associated with the specified key by delegating to an overload that supports an update flag. The ttlOverride parameter is accepted by this overload but is not forwarded to the underlying call.
public Task<T?> GetObjectAsync<T>(string key, TimeSpan? ttlOverride, bool upd)
Parameters
keystringThe identifier of the object to retrieve.
ttlOverrideTimeSpan?An optional time-to-live override accepted by this overload but ignored when delegating to the underlying retrieval call.
updboolA flag indicating whether the retrieval should trigger an update on the stored object.
Returns
- Task<T>
A task that represents the asynchronous operation, containing the retrieved object of type
Tornullif no object is found for the given key.
Type Parameters
T
GetOrSetObjectAsync<T>(string, Func<Task<T>>, TimeSpan?, CancellationToken)
Retrieves an object from the in-memory cache by key, or creates and stores it using the provided factory if absent. Uses a fast path for cache hits and a lock-based path with a double-check to prevent duplicate creation across concurrent callers.
public Task<T> GetOrSetObjectAsync<T>(string key, Func<Task<T>> factory, TimeSpan? ttl = null, CancellationToken cancellationToken = default)
Parameters
keystringThe cache key used to look up and store the object.
factoryFunc<Task<T>>The asynchronous factory function invoked to produce the object when it is not found in the cache.
ttlTimeSpan?Optional time-to-live associated with the cached object.
cancellationTokenCancellationTokenA token to observe for cancellation requests.
Returns
- Task<T>
The cached or newly created object of type
T.
Type Parameters
T
GetOrSetObjectAsync<T>(GroupedField, ObjectId, Func<Task<T>>, TimeSpan?, CancellationToken)
Asynchronously retrieves a cached object associated with the given grouped field and patient, or creates and caches a new one using the supplied factory when no cached value exists. The factory is only invoked when the cache does not contain a value for the key, and the produced value is stored in the cache only when it is not null.
public Task<T> GetOrSetObjectAsync<T>(GroupedField groupedField, ObjectId patientId, Func<Task<T>> factory, TimeSpan? ttl = null, CancellationToken cancellationToken = default)
Parameters
groupedFieldGroupedFieldThe grouped field that contributes to the cache key.
patientIdObjectIdThe patient identifier that contributes to the cache key.
factoryFunc<Task<T>>The asynchronous factory used to build the object when no cached value is available.
ttlTimeSpan?Optional time-to-live for the cached entry.
cancellationTokenCancellationTokenToken used to cancel the asynchronous operation.
Returns
- Task<T>
A task containing the cached or newly created object.
Type Parameters
T
GetOrSetValueAsync(string, Func<Task<string>>, TimeSpan?)
Asynchronously retrieves the cached string value associated with the specified key, or loads and stores it using the provided loader function if absent. Delegates to GetOrSetObjectAsync<T>(string, Func<Task<T>>, TimeSpan?, CancellationToken) to handle caching, honoring the optional TTL override for the cache entry.
public Task<string?> GetOrSetValueAsync(string key, Func<Task<string>> loader, TimeSpan? ttlOverride = null)
Parameters
keystringThe cache key used to identify the stored string value.
loaderFunc<Task<string>>The asynchronous function invoked to load the value when no cached entry exists for the key.
ttlOverrideTimeSpan?An optional time span that overrides the default time-to-live for the cached value.
Returns
- Task<string>
The cached or newly loaded string value, or
nullwhen the underlying cache entry is absent or cannot be cast to a string.
GetValue(string)
Retrieves the string representation of the value associated with the specified key from the in-memory store.
Returns the value converted via ToString() when the key is found, or null when the key is not present.
public string? GetValue(string key)
Parameters
keystringThe key used to look up the value in the underlying store.
Returns
- string
The string representation of the stored value if the key exists; otherwise,
null.
SetObjectAsync<T>(string, T, bool)
Asynchronously stores the specified object in the in-memory cache using the given key.
public Task SetObjectAsync<T>(string key, T obj, bool updateExpiration = true)
Parameters
keystringThe cache key under which the object will be stored.
objTThe object to store in the cache.
updateExpirationboolIndicates whether the cache entry's expiration should be refreshed.
Returns
Type Parameters
T
SetObjectAsync<T>(string, T, TimeSpan?, bool)
Asynchronously stores an object associated with the specified key, with an option to override its time-to-live. The ttlOverride parameter is accepted but is not forwarded to the underlying storage call, so the effective time-to-live is determined elsewhere.
public Task SetObjectAsync<T>(string key, T obj, TimeSpan? ttlOverride, bool upd)
Parameters
keystringThe identifier used to store and later retrieve the object.
objTThe object to store in the underlying store.
ttlOverrideTimeSpan?An optional time-to-live override for the stored entry; not applied by this overload.
updboolA flag indicating whether the operation should update an existing entry.
Returns
- Task
A task that represents the asynchronous set operation.
Type Parameters
T
SetValue(string, string)
Stores the specified value in the in-memory collection under the given key, overwriting any existing entry.
public void SetValue(string key, string value)