Table of Contents

Class InMemoryCacheService

Namespace
JD.AI.Core.Caching
Assembly
JD.AI.Core.dll

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

maxEntries int

Maximum number of cache entries before eviction. Default 10,000.

sweepInterval TimeSpan?

How often to run expiry sweep. Default 60 seconds.

Properties

Count

Number of entries currently in the cache.

public int Count { get; }

Property Value

int

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

key string
ct CancellationToken

Returns

Task<bool>

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

key string
ct CancellationToken

Returns

Task<byte[]>

RemoveAsync(string, CancellationToken)

Removes a cached entry.

public Task RemoveAsync(string key, CancellationToken ct = default)

Parameters

key string
ct CancellationToken

Returns

Task

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

key string
value byte[]
ttl TimeSpan?
ct CancellationToken

Returns

Task