Public .NET API ReferenceL2 Cross-Cutting & ReliabilityStableAOT: Full

EricksonLopez.Caching

Enterprise multi-tier caching engine for .NET (In-Memory L1 + Redis L2). Features dual-scope stampede protection, atomic tag invalidation, multi-tenancy isolation, and Native AOT compliance.

2 namespaces17 types148 membersGenerated from XML docs & assemblies · UTC 2026-12-01

Showing 17 of 17 types (148 members)

EricksonLopez.Caching#

14
class

CacheDiagnostics

Assembly: EricksonLopez.Caching
public static class CacheDiagnostics

Provides OpenTelemetry-compatible metrics and distributed tracing instruments for the caching library. Uses `Metrics.Meter` and `Diagnostics.ActivitySource` without external third-party dependencies.

Members (9)

  • methodRecordError
    public static void RecordError(string provider, string errorCode)

    Records an infrastructure error during cache access.

    provider: string
    The cache provider name where the error occurred.
    errorCode: string
    The diagnostic error code identifying the failure.
  • methodRecordEviction
    public static void RecordEviction(string provider, string reason, long count = 1)

    Records the eviction of one or more cache entries.

    provider: string
    The cache provider name from which entries were evicted.
    reason: string
    The eviction reason (e.g., "capacity", "expired", "removed").
    count: long
    The number of evicted entries. Defaults to 1.
  • methodRecordHit
    public static void RecordHit(string provider, string operation = "")

    Records a cache hit for the specified provider and operation using allocation-free TagList.

    provider: string
    The cache provider name (e.g., "memory", "redis", "hybrid").
    operation: string
    The operation name (e.g., "get", "get_or_create"). Defaults to "get".
  • methodRecordMiss
    public static void RecordMiss(string provider, string operation = "")

    Records a cache miss for the specified provider and operation using allocation-free TagList.

    provider: string
    The cache provider name (e.g., "memory", "redis", "hybrid").
    operation: string
    The operation name (e.g., "get", "get_or_create"). Defaults to "get".
  • methodRecordStampedePrevented
    public static void RecordStampedePrevented(string provider)

    Records that a concurrent cache stampede was prevented by single-flight synchronization.

    provider: string
    The cache provider name that synchronized the concurrent requests.
  • methodStartActivity
    public static Activity? StartActivity(string operation, string provider, string key)

    Starts a new distributed trace span for a cache operation if a listener is active.

    operation: string
    The operation name (e.g., "get", "set", "get_or_create", "remove").
    provider: string
    The cache provider name (e.g., "memory", "redis", "hybrid").
    key: string
    The cache key being accessed.

    Returns: The created `Activity`, or `null` if no listeners exist.

  • fieldActivitySource
    public static readonly ActivitySource ActivitySource

    Provides the `Diagnostics.ActivitySource` instance for caching trace spans.

  • fieldActivitySourceName
    public const string ActivitySourceName

    Defines the name of the OpenTelemetry ActivitySource for distributed tracing.

  • fieldMeterName
    public const string MeterName

    Defines the name of the OpenTelemetry meter used by this library.

class

CacheEntryOptions

Assembly: EricksonLopez.Caching
public class CacheEntryOptions

Specifies expiration, eviction, and resilience rules for cached entries.

Members (8)

  • methodFromAbsolute
    public static CacheEntryOptions FromAbsolute(TimeSpan duration)

    Creates options with the specified absolute expiration duration.

    duration: TimeSpan
    The time-to-live relative to now after which the cached entry expires.

    Returns: A new `CacheEntryOptions` with `AbsoluteExpirationRelativeToNow` set to `duration`.

    Exceptions: ArgumentOutOfRangeException: Thrown when `duration` is less than or equal to `TimeSpan.Zero` or exceeds `MaxAllowedDuration`

  • methodFromAbsoluteWithFailSafe
    public static CacheEntryOptions FromAbsoluteWithFailSafe(TimeSpan duration, TimeSpan maxStale)

    Creates options with absolute expiration and fail-safe stale tolerance.

    duration: TimeSpan
    Normal time-to-live before the entry becomes stale.
    maxStale: TimeSpan
    Maximum duration beyond TTL during which the stale entry may be served if factory fails.

    Returns: A new `CacheEntryOptions` with both `AbsoluteExpirationRelativeToNow` and `FailSafeMaxStale` configured.

    Exceptions: ArgumentOutOfRangeException: Thrown when `duration` or `maxStale` is less than or equal to `TimeSpan.Zero` or exceeds `MaxAllowedDuration`

  • methodFromSliding
    public static CacheEntryOptions FromSliding(TimeSpan duration)

    Creates options with the specified sliding expiration duration.

    duration: TimeSpan
    The inactivity window after which the cached entry expires if not accessed.

    Returns: A new `CacheEntryOptions` with `SlidingExpiration` set to `duration`.

    Exceptions: ArgumentOutOfRangeException: Thrown when `duration` is less than or equal to `TimeSpan.Zero` or exceeds `MaxAllowedDuration`

  • propertyAbsoluteExpirationRelativeToNow
    public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; }

    Gets or sets an absolute expiration time relative to now. Must be strictly positive and less than or equal to `MaxAllowedDuration`.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `MaxAllowedDuration`

  • propertyFailSafeMaxStale
    public TimeSpan? FailSafeMaxStale { get; set; }

    Gets or sets an optional maximum duration during which an expired entry can be returned as fallback if the background/underlying factory execution fails. Must be strictly positive and less than or equal to `MaxAllowedDuration`.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `MaxAllowedDuration`

  • propertySlidingExpiration
    public TimeSpan? SlidingExpiration { get; set; }

    Gets or sets how long a cache entry can be inactive before it will also be removed. Must be strictly positive and less than or equal to `MaxAllowedDuration`.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `MaxAllowedDuration`

  • propertyTags
    public IReadOnlyCollection<string>? Tags { get; set; }

    Gets or sets optional tags associated with this cache entry for bulk invalidation.

  • fieldMaxAllowedDuration
    public static readonly TimeSpan MaxAllowedDuration

    Defines the maximum allowable duration for any cache expiration option (10 years).

class

CacheInvalidator

Assembly: EricksonLopez.Caching
public sealed class CacheInvalidator : ICacheInvalidator
Implements: ICacheInvalidator

Canonical implementation of `ICacheInvalidator` backed by `ICacheProvider`.

Members (7)

  • constructorCacheInvalidator
    public CacheInvalidator(ICacheProvider cacheProvider, ILogger<CacheInvalidator>? logger = null)

    Initializes a new instance of the `CacheInvalidator` class.

    cacheProvider: ICacheProvider
    The underlying cache provider.
    logger: ILogger<CacheInvalidator>?
    Optional logger instance.
  • methodInvalidateAsync
    public async Task<Result<bool>> InvalidateAsync(string key, CancellationToken cancellationToken = default)

    Invalidates a single cached entry by its key.

    key: string
    The cache key to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether the entry existed and was evicted.

  • methodInvalidateByPrefixAsync
    public async Task<Result<bool>> InvalidateByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Invalidates all cached entries matching the specified key prefix.

    prefix: string
    The key prefix to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether the invalidation succeeded.

  • methodInvalidateByPrefixesAsync
    public async Task<Result<bool>> InvalidateByPrefixesAsync(IEnumerable<string> prefixes, CancellationToken cancellationToken = default)

    Invalidates all cached entries matching any of the specified key prefixes.

    prefixes: IEnumerable<string>
    The collection of key prefixes to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether all invalidations succeeded.

  • methodInvalidateByTagAsync
    public async Task<Result<bool>> InvalidateByTagAsync(string tag, CancellationToken cancellationToken = default)

    Invalidates all cached entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be evicted.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether the tag invalidation succeeded.

  • methodInvalidateByTagsAsync
    public async Task<Result<bool>> InvalidateByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Invalidates all cached entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be evicted.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether all tag invalidations succeeded.

  • methodInvalidateManyAsync
    public async Task<Result<long>> InvalidateManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Invalidates multiple cached entries by their keys.

    keys: IEnumerable<string>
    The collection of cache keys to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result containing the count of entries evicted.

class

CachingServiceCollectionExtensions

Assembly: EricksonLopez.Caching
public static class CachingServiceCollectionExtensions

Extension methods for configuring caching providers and services in Microsoft Dependency Injection.

Members (6)

  • methodAddCacheInvalidator
    public static IServiceCollection AddCacheInvalidator(this IServiceCollection services)

    Registers the canonical `CacheInvalidator` as the default scoped `ICacheInvalidator`.

    services: this IServiceCollection
    The service collection to configure.

    Returns: The service collection for chaining.

  • methodAddCacheInvalidator<TInvalidator>
    public static IServiceCollection AddCacheInvalidator<TInvalidator>(this IServiceCollection services) where TInvalidator : class, ICacheInvalidator

    Registers a custom implementation of `ICacheInvalidator` in the service collection.

    services: this IServiceCollection
    The service collection to configure.

    Returns: The service collection for chaining.

    Type parameters: TInvalidator: The invalidator implementation type.

  • methodAddHybridCache
    public static IServiceCollection AddHybridCache(this IServiceCollection services, Func<IServiceProvider, ICacheProvider> localCacheFactory, Func<IServiceProvider, ICacheProvider> distributedCacheFactory)

    Registers a `HybridCacheProvider` that coordinates an L1 local cache with an L2 distributed cache.

    services: this IServiceCollection
    The service collection.
    localCacheFactory: Func<IServiceProvider, ICacheProvider>
    Factory to resolve the L1 local in-memory cache.
    distributedCacheFactory: Func<IServiceProvider, ICacheProvider>
    Factory to resolve the L2 distributed cache.

    Returns: The service collection for chaining.

  • methodAddMemoryCacheProvider
    public static IServiceCollection AddMemoryCacheProvider(this IServiceCollection services)

    Registers the `MemoryCacheProvider` as the default singleton `ICacheProvider` and `ITaggedCacheProvider`.

    services: this IServiceCollection
    The service collection to configure.

    Returns: The service collection for chaining.

  • methodAddMemoryCacheProvider
    public static IServiceCollection AddMemoryCacheProvider(this IServiceCollection services, Action<MemoryCacheOptions>? configure)

    Registers the `MemoryCacheProvider` as the default singleton `ICacheProvider` and `ITaggedCacheProvider` with optional custom configuration options.

    services: this IServiceCollection
    The service collection.
    configure: Action<MemoryCacheOptions>?
    Optional configuration action for `MemoryCacheOptions`.

    Returns: The service collection for chaining.

  • methodAddTenantPartitionedCache
    public static IServiceCollection AddTenantPartitionedCache(this IServiceCollection services, Func<IServiceProvider, string?> tenantIdResolver)

    Decorates an existing `ICacheProvider` registration with `TenantPartitionedCacheProvider` using the specified tenant resolution delegate.

    services: this IServiceCollection
    The service collection.
    tenantIdResolver: Func<IServiceProvider, string?>
    Delegate resolving the current tenant identifier from the service provider.

    Returns: The service collection for chaining.

class

HybridCacheEntryOptions

Assembly: EricksonLopez.Caching
public sealed class HybridCacheEntryOptions : CacheEntryOptions
Inherits: CacheEntryOptions

Specifies tiered expiration and eviction rules for hybrid (L1 in-memory + L2 distributed) cache entries.

Members (3)

  • methodFromDurations
    public static HybridCacheEntryOptions FromDurations(TimeSpan localDuration, TimeSpan distributedDuration)

    Creates a new instance of `HybridCacheEntryOptions` with distinct durations for L1 and L2 caches.

    localDuration: TimeSpan
    Duration for the L1 local in-memory cache.
    distributedDuration: TimeSpan
    Duration for the L2 distributed cache.

    Returns: Configured `HybridCacheEntryOptions` instance.

    Exceptions: ArgumentOutOfRangeException: Thrown when either duration is less than or equal to `TimeSpan.Zero` or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertyDistributedCacheDuration
    public TimeSpan? DistributedCacheDuration { get; set; }

    Gets or sets an optional duration relative to now specifically for the L2 distributed cache. When `null`, falls back to `CacheEntryOptions.AbsoluteExpirationRelativeToNow`.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertyLocalCacheDuration
    public TimeSpan? LocalCacheDuration { get; set; }

    Gets or sets an optional duration relative to now specifically for the L1 local in-memory cache. When `null`, falls back to `CacheEntryOptions.AbsoluteExpirationRelativeToNow`.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `CacheEntryOptions.MaxAllowedDuration`

class

HybridCacheProvider

Assembly: EricksonLopez.Caching
public sealed class HybridCacheProvider : ITaggedCacheProvider
Implements: ITaggedCacheProvider

Multi-tier hybrid cache coordinating an in-memory L1 cache with a distributed L2 cache. Provides tiered TTL configuration, graceful degradation upon L2 failure, and tag-based invalidation.

Members (16)

  • constructorHybridCacheProvider
    public HybridCacheProvider(ICacheProvider localCache, ICacheProvider distributedCache)

    Initializes a new instance of the `HybridCacheProvider` class with default fail-open removal semantics. Preserves binary backward compatibility with pre-compiled assemblies.

    localCache: ICacheProvider
    The L1 fast local in-memory cache. Cannot be `null`.
    distributedCache: ICacheProvider
    The L2 shared distributed cache. Cannot be `null`.
  • constructorHybridCacheProvider
    public HybridCacheProvider(ICacheProvider localCache, ICacheProvider distributedCache, bool failOpenOnRemoval)

    Initializes a new instance of the `HybridCacheProvider` class with configurable removal fault tolerance.

    localCache: ICacheProvider
    The L1 fast local in-memory cache. Cannot be `null`.
    distributedCache: ICacheProvider
    The L2 shared distributed cache. Cannot be `null`.
    failOpenOnRemoval: bool
    If `true`, failures in L2 during removal/invalidation will be ignored and reported as success (fail-open). Defaults to `true` for backward compatibility, or `false` for strict distributed invalidation consistency.
  • methodExistsAsync
    public async Task<Result<bool>> ExistsAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodExpireAsync
    public async Task<Result<bool>> ExpireAsync(string key, TimeSpan timeToLive, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    timeToLive: TimeSpan
    cancellationToken: CancellationToken
  • methodGetAsync<T>
    public async Task<Result<T?>> GetAsync<T>(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodGetManyAsync<T>
    public async Task<Result<IReadOnlyDictionary<string, T>>> GetManyAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodGetOrCreateAsync<T>
    public async Task<Result<T>> GetOrCreateAsync<T>(string key, Func<CancellationToken, Task<T>> factory, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    factory: Func<CancellationToken, Task<T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodRefreshAsync
    public async Task<Result<bool>> RefreshAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveAsync
    public async Task<Result<bool>> RemoveAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveByPrefixAsync
    public async Task<Result<bool>> RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    prefix: string
    cancellationToken: CancellationToken
  • methodRemoveByTagAsync
    public async Task<Result<bool>> RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveByTagsAsync
    public async Task<Result<bool>> RemoveByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveManyAsync
    public async Task<Result<long>> RemoveManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodSetAsync<T>
    public async Task<Result<bool>> SetAsync<T>(string key, T value, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    value: T
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetManyAsync<T>
    public async Task<Result<bool>> SetManyAsync<T>(IEnumerable<KeyValuePair<string, T>> items, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    items: IEnumerable<KeyValuePair<string, T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetWithTagsAsync<T>
    public async Task<Result<bool>> SetWithTagsAsync<T>(string key, T value, IEnumerable<string> tags, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets a value in the cache associated with one or more tags.

    key: string
    The unique cache key.
    value: T
    The value to cache.
    tags: IEnumerable<string>
    The collection of tags to associate with this cache entry.
    options: CacheEntryOptions?
    Optional expiration and eviction options.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the operation succeeded.

    Type parameters: T: The type of the item being cached.

interface

ICacheInvalidator

Assembly: EricksonLopez.Caching
public interface ICacheInvalidator

Defines contracts for high-throughput cache invalidation by key prefix or hierarchical tag.

Members (6)

  • methodInvalidateAsync
    Task<Result<bool>> InvalidateAsync(string key, CancellationToken cancellationToken = default)

    Invalidates a single cached entry by its key.

    key: string
    The cache key to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether the entry existed and was evicted.

  • methodInvalidateByPrefixAsync
    Task<Result<bool>> InvalidateByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Invalidates all cached entries matching the specified key prefix.

    prefix: string
    The key prefix to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether the invalidation succeeded.

  • methodInvalidateByPrefixesAsync
    Task<Result<bool>> InvalidateByPrefixesAsync(IEnumerable<string> prefixes, CancellationToken cancellationToken = default)

    Invalidates all cached entries matching any of the specified key prefixes.

    prefixes: IEnumerable<string>
    The collection of key prefixes to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether all invalidations succeeded.

  • methodInvalidateByTagAsync
    Task<Result<bool>> InvalidateByTagAsync(string tag, CancellationToken cancellationToken = default)

    Invalidates all cached entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be evicted.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether the tag invalidation succeeded.

  • methodInvalidateByTagsAsync
    Task<Result<bool>> InvalidateByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Invalidates all cached entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be evicted.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result indicating whether all tag invalidations succeeded.

  • methodInvalidateManyAsync
    Task<Result<long>> InvalidateManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Invalidates multiple cached entries by their keys.

    keys: IEnumerable<string>
    The collection of cache keys to evict.
    cancellationToken: CancellationToken
    A token to monitor for cancellation requests.

    Returns: A Result containing the count of entries evicted.

interface

ICacheProvider

Assembly: EricksonLopez.Caching
public interface ICacheProvider

Defines standard contract for caching operations with functional Result returns and stampede protection.

Members (11)

  • methodExistsAsync
    Task<Result<bool>> ExistsAsync(string key, CancellationToken cancellationToken = default)

    Checks whether an entry exists for the given key and is not expired.

    key: string
    The unique cache key. Cannot be `null`.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` containing `true` if the entry exists and is active; otherwise `false`.

    Exceptions: ArgumentNullException: Thrown when `key` is `null`

  • methodExpireAsync
    Task<Result<bool>> ExpireAsync(string key, TimeSpan timeToLive, CancellationToken cancellationToken = default)

    Sets a new absolute expiration timeout for the specified key.

    key: string
    The unique cache key. Cannot be `null`.
    timeToLive: TimeSpan
    The new time-to-live duration from now. Must be positive.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` indicating whether the timeout was updated.

  • methodGetAsync<T>
    Task<Result<T?>> GetAsync<T>(string key, CancellationToken cancellationToken = default)

    Retrieves a cached item by its key.

    key: string
    The unique cache key identifying the entry. Cannot be `null`.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` containing the cached value on a hit, or `null` on a cache miss. A cache miss returns `Result<T>.Success` with a `null` value — it is never a failure.

    Type parameters: T: The type of the cached value. Exceptions: ArgumentNullException: Thrown when `key` is `null`

  • methodGetManyAsync<T>
    async Task<Result<IReadOnlyDictionary<string, T>>> GetManyAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Retrieves multiple cached items by their keys.

    keys: IEnumerable<string>
    The unique cache keys identifying the entries. Cannot be `null`.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` containing a read-only dictionary of key-value pairs for all hits.

    Type parameters: T: The type of the cached values. Exceptions: ArgumentNullException: Thrown when `keys` is `null`

  • methodGetOrCreateAsync<T>
    Task<Result<T>> GetOrCreateAsync<T>(string key, Func<CancellationToken, Task<T>> factory, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Gets an existing item or acquires an atomic lock to execute the factory and populate the cache (stampede-proof).

    key: string
    The unique cache key. Cannot be `null`.
    factory: Func<CancellationToken, Task<T>>
    The async delegate invoked exactly once under concurrent cache misses (single-flight guarantee). Cannot be `null`.
    options: CacheEntryOptions?
    Optional expiration, sliding TTL, fail-safe stale window, and tag options. When `null`, provider defaults apply.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests. Propagated to the factory delegate.

    Returns: A `Result<T>` containing the cached or freshly computed value. Returns a failure result only if the factory throws a non-cancellation exception and no fail-safe stale value is available.

    Type parameters: T: The type of the cached value. Exceptions: ArgumentNullException: Thrown when `key` or `factory` is `null`; OperationCanceledException: Propagated when `cancellationToken` is cancelled. Never swallowed

  • methodRefreshAsync
    Task<Result<bool>> RefreshAsync(string key, CancellationToken cancellationToken = default)

    Refreshes the expiration window of a cached item without reading or altering its value.

    key: string
    The unique cache key. Cannot be `null`.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` indicating whether the entry was found and refreshed.

  • methodRemoveAsync
    Task<Result<bool>> RemoveAsync(string key, CancellationToken cancellationToken = default)

    Removes an item from the cache by its key.

    key: string
    The unique cache key to remove. Cannot be `null`.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` of `bool`: `true` if the entry existed and was removed; `false` if no entry was found for the key.

    Exceptions: ArgumentNullException: Thrown when `key` is `null`

  • methodRemoveByPrefixAsync
    Task<Result<bool>> RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Removes all cached entries matching the specified key prefix.

    prefix: string
    The prefix to match against cache keys. Cannot be `null`, empty, or whitespace. An empty or whitespace prefix is rejected to prevent accidental whole-cache wipeouts.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` of `bool` indicating whether the operation completed successfully. Returns `true` even when no matching keys were found.

    Exceptions: ArgumentException: Thrown when `prefix` is `null`, empty, or whitespace

  • methodRemoveManyAsync
    async Task<Result<long>> RemoveManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Removes multiple items from the cache by their keys.

    keys: IEnumerable<string>
    The collection of unique cache keys to remove. Cannot be `null`.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` containing the count of entries successfully removed.

    Exceptions: ArgumentNullException: Thrown when `keys` is `null`

  • methodSetAsync<T>
    Task<Result<bool>> SetAsync<T>(string key, T value, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets a value in the cache with the given expiration options.

    key: string
    The unique cache key. Cannot be `null`.
    value: T
    The value to store. May be `null` for reference types.
    options: CacheEntryOptions?
    Optional expiration and tag options. When `null`, provider defaults apply.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` of `bool` indicating whether the set operation succeeded.

    Type parameters: T: The type of the value to cache. Exceptions: ArgumentNullException: Thrown when `key` is `null`

  • methodSetManyAsync<T>
    async Task<Result<bool>> SetManyAsync<T>(IEnumerable<KeyValuePair<string, T>> items, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets multiple key-value pairs in the cache with the given expiration and tag options.

    items: IEnumerable<KeyValuePair<string, T>>
    The key-value pairs to store. Cannot be `null`.
    options: CacheEntryOptions?
    Optional expiration and tag options.
    cancellationToken: CancellationToken
    Token to observe for cancellation requests.

    Returns: A `Result<T>` indicating whether all items were successfully set.

    Type parameters: T: The type of the values to cache. Exceptions: ArgumentNullException: Thrown when `items` is `null`

interface

ICacheSerializer

Assembly: EricksonLopez.Caching
public interface ICacheSerializer

Defines an abstraction for serializing and deserializing cached objects for distributed cache stores.

Members (4)

  • methodDeserialize<T>
    T? Deserialize<T>(string value)

    Deserializes a string representation into an object of type `T`.

    value: string
    The serialized string value.

    Returns: The deserialized object, or `null` if the payload represents null.

    Type parameters: T: The target object type.

  • methodDeserializeFromUtf8Bytes<T>
    T? DeserializeFromUtf8Bytes<T>(byte[] bytes)

    Deserializes a UTF-8 byte array into an object of type `T`.

    bytes: byte[]
    The UTF-8 encoded bytes.

    Returns: The deserialized object, or `null` if the payload represents null.

    Type parameters: T: The target object type.

  • methodSerialize<T>
    string Serialize<T>(T value)

    Serializes the specified value to a string representation.

    value: T
    The value to serialize.

    Returns: A string representation of the serialized value.

    Type parameters: T: The type of the object to serialize.

  • methodSerializeToUtf8Bytes<T>
    byte[] SerializeToUtf8Bytes<T>(T value)

    Serializes the specified value to a UTF-8 byte array.

    value: T
    The value to serialize.

    Returns: A UTF-8 encoded byte array representing the serialized value.

    Type parameters: T: The type of the object to serialize.

interface

ITaggedCacheProvider

Assembly: EricksonLopez.Caching
public interface ITaggedCacheProvider : ICacheProvider
Implements: ICacheProvider

Defines a cache provider that supports categorizing entries with tags for multi-key bulk invalidation.

Members (3)

  • methodRemoveByTagAsync
    Task<Result<bool>> RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveByTagsAsync
    Task<Result<bool>> RemoveByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodSetWithTagsAsync<T>
    Task<Result<bool>> SetWithTagsAsync<T>(string key, T value, IEnumerable<string> tags, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets a value in the cache associated with one or more tags.

    key: string
    The unique cache key.
    value: T
    The value to cache.
    tags: IEnumerable<string>
    The collection of tags to associate with this cache entry.
    options: CacheEntryOptions?
    Optional expiration and eviction options.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the operation succeeded.

    Type parameters: T: The type of the item being cached.

class

MemoryCacheOptions

Assembly: EricksonLopez.Caching
public sealed class MemoryCacheOptions

Configuration options for the in-memory cache provider.

Members (3)

  • propertyCompactPercentage
    public double CompactPercentage { get; set; }

    Gets or sets the percentage of entries to evict when `MaxCapacity` is reached. Defaults to 0.25 (25%). Must be between 0.05 and 0.50.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than 0.05 or greater than 0.50

  • propertyExpirationScanFrequency
    public TimeSpan? ExpirationScanFrequency { get; set; }

    Gets or sets the frequency at which a background timer actively scans and evicts expired cache entries and prunes empty tag indexes. When `null`, active background sweeps are disabled and eviction occurs lazily or via explicit calls to `MemoryCacheProvider.RemoveExpiredEntries`.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero`

  • propertyMaxCapacity
    public int? MaxCapacity { get; set; }

    Gets or sets the maximum number of entries allowed in the cache. When the limit is exceeded, compaction will evict the least recently accessed entries according to the `CompactPercentage` setting.

    When `null` (the default), capacity is unbounded. ⚠ Production warning: In production environments, always configure a finite `MaxCapacity` to prevent out-of-memory conditions under high key cardinality or adversarial workloads. A reasonable starting value is between 10,000 and 100,000 entries depending on entry size and available memory. Example: `options.MaxCapacity = 50_000;` Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to 0

class

MemoryCacheProvider

Assembly: EricksonLopez.Caching
public sealed class MemoryCacheProvider : ITaggedCacheProvider, IDisposable
Implements: IDisposable, ITaggedCacheProvider

High-throughput, thread-safe in-memory cache provider with stampede protection, tag-based eviction, fail-safe stale tolerance, LRU compaction, active background cleanup, and prefix invalidation.

Members (19)

  • constructorMemoryCacheProvider
    public MemoryCacheProvider(IOptions<MemoryCacheOptions>? options, TimeProvider? timeProvider = null)

    Initializes a new instance of the `MemoryCacheProvider` class with Microsoft DI options.

    options: IOptions<MemoryCacheOptions>?
    Configuration options wrapped in `IOptions<MemoryCacheOptions>`.
    timeProvider: TimeProvider?
    Optional time provider for unit testing expiration.
  • constructorMemoryCacheProvider
    public MemoryCacheProvider(MemoryCacheOptions? options, TimeProvider? timeProvider = null)

    Initializes a new instance of the `MemoryCacheProvider` class with explicit configuration options.

    options: MemoryCacheOptions?
    Configuration options.
    timeProvider: TimeProvider?
    Optional time provider for unit testing expiration.
  • constructorMemoryCacheProvider
    public MemoryCacheProvider(TimeProvider? timeProvider = null)

    Initializes a new instance of the `MemoryCacheProvider` class with optional time provider.

    timeProvider: TimeProvider?
    Optional time provider for unit testing expiration.
  • methodDispose
    public void Dispose()

    Inherits documentation from base type or interface.

  • methodExistsAsync
    public Task<Result<bool>> ExistsAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodExpireAsync
    public Task<Result<bool>> ExpireAsync(string key, TimeSpan timeToLive, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    timeToLive: TimeSpan
    cancellationToken: CancellationToken
  • methodGetAsync<T>
    public Task<Result<T?>> GetAsync<T>(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodGetManyAsync<T>
    public Task<Result<IReadOnlyDictionary<string, T>>> GetManyAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodGetOrCreateAsync<T>
    public async Task<Result<T>> GetOrCreateAsync<T>(string key, Func<CancellationToken, Task<T>> factory, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    factory: Func<CancellationToken, Task<T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodRefreshAsync
    public Task<Result<bool>> RefreshAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveAsync
    public Task<Result<bool>> RemoveAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveByPrefixAsync
    public Task<Result<bool>> RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    prefix: string
    cancellationToken: CancellationToken
  • methodRemoveByTagAsync
    public Task<Result<bool>> RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveByTagsAsync
    public async Task<Result<bool>> RemoveByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveExpiredEntries
    public int RemoveExpiredEntries()

    Scans the internal cache storage and actively evicts all entries whose expiration duration has elapsed and are no longer eligible for stale fallback. Cleans up associated tag indices.

    Returns: The number of expired entries successfully evicted from memory.

  • methodRemoveManyAsync
    public Task<Result<long>> RemoveManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodSetAsync<T>
    public Task<Result<bool>> SetAsync<T>(string key, T value, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    value: T
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetManyAsync<T>
    public async Task<Result<bool>> SetManyAsync<T>(IEnumerable<KeyValuePair<string, T>> items, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    items: IEnumerable<KeyValuePair<string, T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetWithTagsAsync<T>
    public Task<Result<bool>> SetWithTagsAsync<T>(string key, T value, IEnumerable<string> tags, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets a value in the cache associated with one or more tags.

    key: string
    The unique cache key.
    value: T
    The value to cache.
    tags: IEnumerable<string>
    The collection of tags to associate with this cache entry.
    options: CacheEntryOptions?
    Optional expiration and eviction options.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the operation succeeded.

    Type parameters: T: The type of the item being cached.

class

SystemTextJsonCacheSerializer

Assembly: EricksonLopez.Caching
public sealed class SystemTextJsonCacheSerializer : ICacheSerializer
Implements: ICacheSerializer

Default `ICacheSerializer` implementation using `JsonSerializer`.

Members (6)

  • constructorSystemTextJsonCacheSerializer
    public SystemTextJsonCacheSerializer(JsonSerializerOptions? options = null)

    Initializes a new instance of the `SystemTextJsonCacheSerializer` class.

    options: JsonSerializerOptions?
    Optional custom `JsonSerializerOptions`.
  • methodDeserialize<T>
    public T? Deserialize<T>(string value)

    Deserializes a string representation into an object of type `T`.

    value: string
    The serialized string value.

    Returns: The deserialized object, or `null` if the payload represents null.

    Type parameters: T: The target object type.

  • methodDeserializeFromUtf8Bytes<T>
    public T? DeserializeFromUtf8Bytes<T>(byte[] bytes)

    Deserializes a UTF-8 byte array into an object of type `T`.

    bytes: byte[]
    The UTF-8 encoded bytes.

    Returns: The deserialized object, or `null` if the payload represents null.

    Type parameters: T: The target object type.

  • methodSerialize<T>
    public string Serialize<T>(T value)

    Serializes the specified value to a string representation.

    value: T
    The value to serialize.

    Returns: A string representation of the serialized value.

    Type parameters: T: The type of the object to serialize.

  • methodSerializeToUtf8Bytes<T>
    public byte[] SerializeToUtf8Bytes<T>(T value)

    Serializes the specified value to a UTF-8 byte array.

    value: T
    The value to serialize.

    Returns: A UTF-8 encoded byte array representing the serialized value.

    Type parameters: T: The type of the object to serialize.

  • propertyDefault
    public static SystemTextJsonCacheSerializer Default { get; }

    Gets the default singleton instance configured with default serialization options.

class

TenantPartitionedCacheProvider

Assembly: EricksonLopez.Caching
public sealed class TenantPartitionedCacheProvider : ITaggedCacheProvider
Implements: ITaggedCacheProvider

Partitioned cache provider decorator that enforces tenant isolation by automatically prefixing all cache keys, tags, and bulk operations with the current tenant identifier. Prevents cross-tenant data crossover and cache poisoning.

Members (15)

  • constructorTenantPartitionedCacheProvider
    public TenantPartitionedCacheProvider(ICacheProvider innerProvider, Func<string?> tenantIdResolver)

    Initializes a new instance of the `TenantPartitionedCacheProvider` class.

    innerProvider: ICacheProvider
    The underlying cache provider. Cannot be `null`.
    tenantIdResolver: Func<string?>
    Delegate resolving the current tenant identifier. Cannot be `null`.
  • methodExistsAsync
    public Task<Result<bool>> ExistsAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodExpireAsync
    public Task<Result<bool>> ExpireAsync(string key, TimeSpan timeToLive, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    timeToLive: TimeSpan
    cancellationToken: CancellationToken
  • methodGetAsync<T>
    public Task<Result<T?>> GetAsync<T>(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodGetManyAsync<T>
    public async Task<Result<IReadOnlyDictionary<string, T>>> GetManyAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodGetOrCreateAsync<T>
    public Task<Result<T>> GetOrCreateAsync<T>(string key, Func<CancellationToken, Task<T>> factory, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    factory: Func<CancellationToken, Task<T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodRefreshAsync
    public Task<Result<bool>> RefreshAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveAsync
    public Task<Result<bool>> RemoveAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveByPrefixAsync
    public Task<Result<bool>> RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    prefix: string
    cancellationToken: CancellationToken
  • methodRemoveByTagAsync
    public Task<Result<bool>> RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveByTagsAsync
    public Task<Result<bool>> RemoveByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveManyAsync
    public Task<Result<long>> RemoveManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodSetAsync<T>
    public Task<Result<bool>> SetAsync<T>(string key, T value, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    value: T
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetManyAsync<T>
    public Task<Result<bool>> SetManyAsync<T>(IEnumerable<KeyValuePair<string, T>> items, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    items: IEnumerable<KeyValuePair<string, T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetWithTagsAsync<T>
    public Task<Result<bool>> SetWithTagsAsync<T>(string key, T value, IEnumerable<string> tags, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets a value in the cache associated with one or more tags.

    key: string
    The unique cache key.
    value: T
    The value to cache.
    tags: IEnumerable<string>
    The collection of tags to associate with this cache entry.
    options: CacheEntryOptions?
    Optional expiration and eviction options.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the operation succeeded.

    Type parameters: T: The type of the item being cached.

EricksonLopez.Caching.Redis#

3
class

CachingRedisServiceCollectionExtensions

Assembly: EricksonLopez.Caching.Redis
public static class CachingRedisServiceCollectionExtensions

Extension methods for registering the Redis cache provider in the DI container.

Members (3)

  • methodAddRedisCaching
    public static IServiceCollection AddRedisCaching(this IServiceCollection services, Action<RedisCacheOptions> configure)

    Adds the Redis-backed L2 cache provider as the `ICacheProvider` and `ITaggedCacheProvider` implementation.

    services: this IServiceCollection
    The service collection.
    configure: Action<RedisCacheOptions>
    Delegate to configure `RedisCacheOptions`.

    Returns: The service collection for chaining.

  • methodAddRedisCaching
    public static IServiceCollection AddRedisCaching(this IServiceCollection services, Func<IServiceProvider, IConnectionMultiplexer> connectionMultiplexerFactory, Action<RedisCacheOptions>? configure = null)

    Adds the Redis-backed L2 cache provider using a factory to resolve or pre-warm `IConnectionMultiplexer`. Enables non-blocking, deferred, or custom multiplexer lifecycle management.

    services: this IServiceCollection
    The service collection.
    connectionMultiplexerFactory: Func<IServiceProvider, IConnectionMultiplexer>
    Factory resolving the connection multiplexer.
    configure: Action<RedisCacheOptions>?
    Optional delegate to configure `RedisCacheOptions`.

    Returns: The service collection for chaining.

  • methodAddRedisCaching
    public static IServiceCollection AddRedisCaching(this IServiceCollection services, IConnectionMultiplexer connectionMultiplexer, Action<RedisCacheOptions>? configure = null)

    Adds the Redis-backed L2 cache provider using an existing `IConnectionMultiplexer`.

    services: this IServiceCollection
    The service collection.
    connectionMultiplexer: IConnectionMultiplexer
    An existing Redis connection multiplexer.
    configure: Action<RedisCacheOptions>?
    Optional delegate to configure `RedisCacheOptions`.

    Returns: The service collection for chaining.

class

RedisCacheOptions

Assembly: EricksonLopez.Caching.Redis
public sealed class RedisCacheOptions

Configuration options for the Redis L2 cache provider.

Members (11)

  • propertyConfiguration
    public string Configuration { get; set; }

    Gets or sets the StackExchange.Redis configuration string (e.g. "localhost:6379,abortConnect=false").

    Exceptions: ArgumentException: Thrown when value is null, empty, or whitespace

  • propertyDatabase
    public int Database { get; set; }

    Gets or sets the Redis database index to use (-1 for default database, or >= 0). Defaults to 0.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than -1

  • propertyDefaultAbsoluteExpiration
    public TimeSpan DefaultAbsoluteExpiration { get; set; }

    Gets or sets the default absolute expiration relative to now for cache entries when not explicitly specified.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertyFailOpen
    public bool FailOpen { get; set; }

    Gets or sets a value indicating whether cache read operations (such as `GetAsync` and `ExistsAsync`) should fail open (return cache miss `Result.Success(default)`) when Redis is unavailable, rather than returning an error result. Defaults to `false`.

  • propertyInstanceName
    public string? InstanceName { get; set; }

    Gets or sets the optional instance name prefix prepended to all cache keys for namespace isolation.

  • propertyLockRetryInterval
    public TimeSpan LockRetryInterval { get; set; }

    Polling interval while waiting for another instance holding the distributed lock.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertyLockTimeout
    public TimeSpan LockTimeout { get; set; }

    Time-to-live for the distributed stampede protection lock in Redis. Prevents deadlocks if the lock holder terminates unexpectedly.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is less than or equal to `TimeSpan.Zero` or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertyMaxLockWaitTime
    public TimeSpan MaxLockWaitTime { get; set; }

    Maximum total time to wait for a distributed lock before executing the factory as fallback.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is negative or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertyMaxRetries
    public int MaxRetries { get; set; }

    Maximum number of retry attempts for Redis operations before returning a failure result.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is negative

  • propertyRetryDelay
    public TimeSpan RetryDelay { get; set; }

    Delay between retry attempts for transient network failures.

    Exceptions: ArgumentOutOfRangeException: Thrown when value is negative or exceeds `CacheEntryOptions.MaxAllowedDuration`

  • propertySerializer
    public ICacheSerializer? Serializer { get; set; }

    Optional custom serializer for cached items. Defaults to `SystemTextJsonCacheSerializer.Default`.

class

RedisCacheProvider

Assembly: EricksonLopez.Caching.Redis
public sealed class RedisCacheProvider : ITaggedCacheProvider, IDisposable, IAsyncDisposable
Implements: IAsyncDisposable, IDisposable, ITaggedCacheProvider

Redis-backed L2 cache provider with distributed stampede protection, tag-based eviction, configurable retry loop, and prefix-based invalidation. Thread-safe for concurrent access across multiple application instances.

Members (18)

  • constructorRedisCacheProvider
    public RedisCacheProvider(IConnectionMultiplexer connection, IOptions<RedisCacheOptions> options, ILogger<RedisCacheProvider> logger, ICacheSerializer? serializer = null)

    Initializes a new instance using an externally managed connection multiplexer.

    connection: IConnectionMultiplexer
    An existing Redis connection multiplexer. Cannot be `null`.
    options: IOptions<RedisCacheOptions>
    Redis cache configuration options. Cannot be `null`.
    logger: ILogger<RedisCacheProvider>
    Logger instance. Cannot be `null`.
    serializer: ICacheSerializer?
    Optional custom serializer. Defaults to `SystemTextJsonCacheSerializer.Default`.
  • constructorRedisCacheProvider
    public RedisCacheProvider(IConnectionMultiplexer connection, IOptions<RedisCacheOptions> options, ILogger<RedisCacheProvider> logger, ICacheSerializer? serializer, TimeProvider? timeProvider)

    Initializes a new instance using an externally managed connection multiplexer and custom time provider.

    connection: IConnectionMultiplexer
    An existing Redis connection multiplexer. Cannot be `null`.
    options: IOptions<RedisCacheOptions>
    Redis cache configuration options. Cannot be `null`.
    logger: ILogger<RedisCacheProvider>
    Logger instance. Cannot be `null`.
    serializer: ICacheSerializer?
    Custom serializer, or `null` for default.
    timeProvider: TimeProvider?
    Optional time provider for time-dependent operations. Defaults to `TimeProvider.System`.
  • methodDispose
    public void Dispose()

    Inherits documentation from base type or interface.

  • methodDisposeAsync
    public async ValueTask DisposeAsync()

    Inherits documentation from base type or interface.

  • methodExistsAsync
    public async Task<Result<bool>> ExistsAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodExpireAsync
    public async Task<Result<bool>> ExpireAsync(string key, TimeSpan timeToLive, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    timeToLive: TimeSpan
    cancellationToken: CancellationToken
  • methodGetAsync<T>
    public async Task<Result<T?>> GetAsync<T>(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodGetManyAsync<T>
    public async Task<Result<IReadOnlyDictionary<string, T>>> GetManyAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodGetOrCreateAsync<T>
    public async Task<Result<T>> GetOrCreateAsync<T>(string key, Func<CancellationToken, Task<T>> factory, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    factory: Func<CancellationToken, Task<T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodRefreshAsync
    public async Task<Result<bool>> RefreshAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveAsync
    public async Task<Result<bool>> RemoveAsync(string key, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    cancellationToken: CancellationToken
  • methodRemoveByPrefixAsync
    public async Task<Result<bool>> RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    prefix: string
    cancellationToken: CancellationToken
  • methodRemoveByTagAsync
    public async Task<Result<bool>> RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with the specified tag.

    tag: string
    The tag whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveByTagsAsync
    public async Task<Result<bool>> RemoveByTagsAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default)

    Evicts all cache entries associated with any of the specified tags.

    tags: IEnumerable<string>
    The collection of tags whose associated entries will be removed.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the eviction succeeded.

  • methodRemoveManyAsync
    public async Task<Result<long>> RemoveManyAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    keys: IEnumerable<string>
    cancellationToken: CancellationToken
  • methodSetAsync<T>
    public async Task<Result<bool>> SetAsync<T>(string key, T value, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    key: string
    value: T
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetManyAsync<T>
    public async Task<Result<bool>> SetManyAsync<T>(IEnumerable<KeyValuePair<string, T>> items, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Inherits documentation from base type or interface.

    items: IEnumerable<KeyValuePair<string, T>>
    options: CacheEntryOptions?
    cancellationToken: CancellationToken
  • methodSetWithTagsAsync<T>
    public async Task<Result<bool>> SetWithTagsAsync<T>(string key, T value, IEnumerable<string> tags, CacheEntryOptions? options = null, CancellationToken cancellationToken = default)

    Sets a value in the cache associated with one or more tags.

    key: string
    The unique cache key.
    value: T
    The value to cache.
    tags: IEnumerable<string>
    The collection of tags to associate with this cache entry.
    options: CacheEntryOptions?
    Optional expiration and eviction options.
    cancellationToken: CancellationToken
    Cancellation token.

    Returns: A `Result<T>` indicating whether the operation succeeded.

    Type parameters: T: The type of the item being cached.

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.