# 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
  object
  CacheDispatcher

Implements
  ICacheService

Inherited Members
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.MemberwiseClone()
  object.ReferenceEquals(object, object)
  object.ToString()

Extension Methods
  NumberUtils.IsNumber(object)
  NumberUtils.ToDouble(object)
  ObjectUtils.ToStr(object?)

## 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

`redis` RedisService
`memory` CacheService
`noop` NoCacheService
`cacheSettings` CacheSettings

## 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

`pattern` string

  The pattern used to match entries for deletion in both storage backends.

#### Returns

Task<long>

  The combined total number of entries deleted from Redis and in-memory storage.

### 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

`key` string

  The 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

`key` string

  The identifier used to select the appropriate backend and to look up the object.

`upd` bool

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

`key` string

  The identifier used to select the backend and locate the stored object.

`ttl` TimeSpan?

  An optional time-to-live applied to the object; if null, the backend's default is used.

`upd` bool

  A 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

`key` string

  The cache key used to identify the object and to select the appropriate backend.

`factory` Func<Task<T>>

  A delegate that asynchronously produces the value to store when the key is not present in the selected backend.

`ttl` TimeSpan?

  An optional time-to-live duration for the cached object. If null, the backend's default expiration is applied.

`cancellationToken` CancellationToken

  A 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

`groupedField` GroupedField

  The grouped field that determines the target backend and identifies the cached object.

`patientId` ObjectId

  The identifier of the patient whose object is being retrieved or created.

`factory` Func<Task<T>>

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

`ttl` TimeSpan?

  An optional time-to-live applied to the cached object. When `null`, the backend's default expiration is used.

`cancellationToken` CancellationToken

  The 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

`T`

  The 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

`key` string

  The key used to identify the cached value and to select the appropriate backend.

`loader` Func<Task<string>>

  An asynchronous function that produces the value to cache when no existing entry is found.

`ttl` TimeSpan?

  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

`key` string

  The key used to select the backend and retrieve the associated value.

#### Returns

string

  The value associated with the key, or `null` if 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

`key` string

  The key used to select the backend and identify the stored object.

`obj` T

  The object to store in the selected backend.

`upd` bool

  Indicates whether an update operation should be performed. Defaults to `true`.

#### Returns

Task

  A task that represents the asynchronous store operation.

#### Type Parameters

`T`

  The 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

`key` string

  The key used to select the target backend and identify the object.

`obj` T

  The object to store.

`ttl` TimeSpan?

  The optional time-to-live duration for the stored object.

`upd` bool

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

#### Parameters

`key` string

  The key used to select the backend and identify the value to set.

`value` string

  The value to associate with the specified key.