Class InMemoryCacheService
In-process cache implementation using ConcurrentDictionary<TKey, TValue>. Suitable for single-node deployments. Entries expire based on TTL. A background sweep periodically removes expired entries.
public sealed class InMemoryCacheService : IDistributedCacheService, IDisposable
- Inheritance
-
InMemoryCacheService
- Implements
- Inherited Members
- Extension Methods
Constructors
InMemoryCacheService(int, TimeSpan?)
public InMemoryCacheService(int maxEntries = 10000, TimeSpan? sweepInterval = null)
Parameters
maxEntriesintMaximum number of cache entries before eviction. Default 10,000.
sweepIntervalTimeSpan?How often to run expiry sweep. Default 60 seconds.
Properties
Count
Number of entries currently in the cache.
public int Count { get; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
ExistsAsync(string, CancellationToken)
Checks if a key exists in the cache.
public Task<bool> ExistsAsync(string key, CancellationToken ct = default)
Parameters
keystringctCancellationToken
Returns
GetAsync(string, CancellationToken)
Gets a cached value by key. Returns null if not found or expired.
public Task<byte[]?> GetAsync(string key, CancellationToken ct = default)
Parameters
keystringctCancellationToken
Returns
RemoveAsync(string, CancellationToken)
Removes a cached entry.
public Task RemoveAsync(string key, CancellationToken ct = default)
Parameters
keystringctCancellationToken
Returns
SetAsync(string, byte[], TimeSpan?, CancellationToken)
Sets a cached value with an optional TTL.
public Task SetAsync(string key, byte[] value, TimeSpan? ttl = null, CancellationToken ct = default)
Parameters
keystringvaluebyte[]ttlTimeSpan?ctCancellationToken