Table of Contents

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

lockManager LockManagerService

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

pattern string

The 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

key string

The identifier of the object to delete.

Returns

Task

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

key string

The cache key used to look up the stored object.

updateExpiration bool

Indicates 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, or null if 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

key string

The identifier of the object to retrieve.

ttlOverride TimeSpan?

An optional time-to-live override accepted by this overload but ignored when delegating to the underlying retrieval call.

upd bool

A 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 T or null if 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

key string

The cache key used to look up and store the object.

factory Func<Task<T>>

The asynchronous factory function invoked to produce the object when it is not found in the cache.

ttl TimeSpan?

Optional time-to-live associated with the cached object.

cancellationToken CancellationToken

A 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

groupedField GroupedField

The grouped field that contributes to the cache key.

patientId ObjectId

The patient identifier that contributes to the cache key.

factory Func<Task<T>>

The asynchronous factory used to build the object when no cached value is available.

ttl TimeSpan?

Optional time-to-live for the cached entry.

cancellationToken CancellationToken

Token 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

key string

The cache key used to identify the stored string value.

loader Func<Task<string>>

The asynchronous function invoked to load the value when no cached entry exists for the key.

ttlOverride TimeSpan?

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 null when 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

key string

The 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

key string

The cache key under which the object will be stored.

obj T

The object to store in the cache.

updateExpiration bool

Indicates whether the cache entry's expiration should be refreshed.

Returns

Task

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

key string

The identifier used to store and later retrieve the object.

obj T

The object to store in the underlying store.

ttlOverride TimeSpan?

An optional time-to-live override for the stored entry; not applied by this overload.

upd bool

A 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)

Parameters

key string

The key that identifies where the value will be stored.

value string

The value to associate with the specified key.