Table of Contents

Class MemoryCacheService

Namespace
FoundationaLLM.Common.Services.Cache
Assembly
FoundationaLLM.Common.dll

Provides an in-memory cache service.

public class MemoryCacheService : ICacheService
Inheritance
MemoryCacheService
Implements
Inherited Members
Extension Methods

Constructors

MemoryCacheService(ILogger<MemoryCacheService>)

Provides an in-memory cache service.

public MemoryCacheService(ILogger<MemoryCacheService> logger)

Parameters

logger ILogger<MemoryCacheService>

The logger used for logging.

Methods

GetItemsCount(string)

Gets the number of items in the cache that belong to a specified category.

public int GetItemsCount(string categoryName)

Parameters

categoryName string

The name of the category.

Returns

int

Get<T>(CacheKey)

Gets an object from the cache.

public T? Get<T>(CacheKey key)

Parameters

key CacheKey

The CacheKey key identifying the object in the cache.

Returns

T

The object identified by the key.

Type Parameters

T

The type of object retrieved from the cache.

Get<T>(CacheKey, Func<Task<T>>, bool, TimeSpan?)

Gets an object from the cache. If the object is not found in the cache, will attempt to retrieve it using a specified retriever.

public Task<T?> Get<T>(CacheKey key, Func<Task<T>> valueRetriever, bool allowNull, TimeSpan? expirationTime)

Parameters

key CacheKey

The CacheKey key identifying the object in the cache.

valueRetriever Func<Task<T>>

The retriever used to get the value to populate the cache.

allowNull bool

Indicates whether null values returned by the value retriever should be cached or not.

expirationTime TimeSpan?

The TimeSpan time to live.

Returns

Task<T>

The object identified by the key.

Type Parameters

T

The type of object retrieved from the cache.

Remove(CacheKey)

Removes an object from the cache.

public void Remove(CacheKey key)

Parameters

key CacheKey

The CacheKey key identifying the object in the cache.

RemoveByCategory(CacheKey)

Removes all objects belonging to a category from the cache.

public void RemoveByCategory(CacheKey key)

Parameters

key CacheKey

The CacheKey key identifying the object in the cache.

Set<T>(CacheKey, T?, TimeSpan?)

Adds an object to the cache.

public void Set<T>(CacheKey key, T? value, TimeSpan? expirationTime)

Parameters

key CacheKey

The CacheKey key identifying the object in the cache.

value T

The object to be added to the cache.

expirationTime TimeSpan?

The TimeSpan time to live.

Type Parameters

T

The type of object retrieved from the cache.