using adas_core.Application.Services.Interfaces;
using adas_core.Domain.Models.GroupedObservations;
using MongoDB.Bson;
namespace adas_core.Application.Services.Caching
{
///
/// Implementación nula de ICacheService.
/// No almacena nada, no devuelve nada y no interfiere con el flujo.
/// Se usa cuando el CacheMode es "None".
///
public class NoCacheService : ICacheService
{
///
/// Stub implementation that performs no action. Intended as a placeholder for storing a value associated with the specified key.
///
/// The identifier used to reference the value.
/// The value intended to be associated with the key.
public void SetValue(string key, string value)
{
// No hacer nada
}
///
/// Retrieves the string value associated with the specified key.
/// Returns null when no value is found for the given key.
///
/// The key used to look up the associated value.
/// The value associated with , or null if no value is found.
public string? GetValue(string key)
{
return null;
}
///
/// Asynchronously retrieves an object of type associated with the specified .
///
/// The identifier of the object to retrieve.
/// Indicates whether the expiration of the entry should be updated upon retrieval.
/// A that represents the asynchronous retrieval, containing the object associated with the key or the default value of if not found.
public Task GetObjectAsync(string key, bool updateExpiration = true)
{
return Task.FromResult(default);
}
///
/// Asynchronously stores the specified object associated with the given key in the underlying data store.
/// When is true, the expiration of the entry is refreshed; otherwise the existing expiration is preserved.
///
/// The unique identifier used to store and later retrieve the object.
/// The object to store in the data store.
/// Indicates whether the expiration time of the cached entry should be updated. Defaults to true.
public Task SetObjectAsync(string key, T obj, bool updateExpiration = true)
{
return Task.CompletedTask;
}
///
/// Asynchronously retrieves an object of type associated with the specified .
/// Supports an optional time-to-live override and an option to update the expiration of the stored entry.
///
/// The unique identifier used to look up the stored object.
/// An optional time-to-live value that, when provided, overrides the default expiration for the entry.
/// Indicates whether the expiration of the entry should be refreshed upon a successful retrieval.
/// A that represents the asynchronous operation, containing the retrieved object or null if no value is found.
public Task GetObjectAsync(string key, TimeSpan? ttlOverride, bool updateExpiration)
{
return Task.FromResult(default);
}
///
/// Asynchronously stores an object associated with the specified key, optionally overriding the time-to-live and updating the expiration.
///
/// The identifier used to store and retrieve the object.
/// The object to be stored.
/// An optional time-to-live value that overrides the default expiration period; null uses the default.
/// A value indicating whether the expiration time should be updated.
/// A task that represents the asynchronous set operation.
public Task SetObjectAsync(string key, T obj, TimeSpan? ttlOverride, bool updateExpiration)
{
return Task.CompletedTask;
}
///
/// Asynchronously deletes items matching the specified pattern and returns the number of items removed.
/// This implementation is a stub that always returns 0, performing no actual deletion regardless of the provided pattern.
///
/// The pattern used to identify the items to delete.
/// A representing the asynchronous operation, with a result of 0 indicating that no items were deleted.
public Task DeleteByPatternAsync(string pattern)
{
return Task.FromResult(0L);
}
///
/// Asynchronously deletes the object identified by the specified key.
/// The operation completes immediately without performing an actual deletion.
///
/// The identifier of the object to delete.
/// A that represents the asynchronous delete operation.
public Task DeleteObjectAsync(string key)
{
return Task.CompletedTask;
}
///
/// Performs a cleanup operation on the cache. Currently, this method has no implementation and does not perform any cleanup actions.
///
public void CleanCache()
{
// Nada que limpiar
}
// ============================================================
// GET OR SET - STRING KEY
// ============================================================
public async Task GetOrSetObjectAsync(
string key,
Func> factory,
TimeSpan? ttl = null,
CancellationToken cancellationToken = default)
{
// En modo NONE no hay caché → siempre ejecutar factory
return await factory();
}
///
/// Asynchronously loads a value using the provided loader function.
///
/// The key associated with the value to retrieve or set.
/// The asynchronous function used to load the value.
/// An optional time-to-live duration for the value.
/// A task that represents the asynchronous operation, containing the loaded value as a nullable string.
public async Task GetOrSetValueAsync(
string key,
Func> loader,
TimeSpan? ttl = null)
{
var result = await loader();
return (string?)result;
}
// ============================================================
// GET OR SET - GroupedField + patientId
// ============================================================
public async Task GetOrSetObjectAsync(
GroupedField groupedField,
ObjectId patientId,
Func> factory,
TimeSpan? ttl = null,
CancellationToken cancellationToken = default)
{
// Igual que arriba: en modo NONE no hay caché
return await factory();
}
}
}