Table of Contents

Class CacheEntryKeyExtensions

Namespace
OpenSettings.Extensions
Assembly
OpenSettings.dll

Provides extension methods for CacheEntry to interact with both in-memory and distributed caches. This includes methods for setting, getting, and creating cached items, as well as handling expiration and callback options.

public static class CacheEntryKeyExtensions
Inheritance
CacheEntryKeyExtensions
Inherited Members

Methods

GetOrCreateAsync<TItem>(CacheEntryKey, IDistributedCache, Func<DistributedCacheEntryOptions, Task<TItem>>, CancellationToken)

Asynchronously gets or creates a value for the specified key in the distributed cache, using the provided factory function.

public static Task<TItem> GetOrCreateAsync<TItem>(this CacheEntryKey cacheEntryKey, IDistributedCache cache, Func<DistributedCacheEntryOptions, Task<TItem>> factory, CancellationToken cancellationToken = default)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IDistributedCache

The distributed cache instance to store the value.

factory Func<DistributedCacheEntryOptions, Task<TItem>>

The factory function to generate the value if it does not exist in the cache.

cancellationToken CancellationToken

A cancellation token to cancel the operation.

Returns

Task<TItem>

A task that represents the asynchronous get or create operation.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

GetOrCreateAsync<TItem>(CacheEntryKey, IMemoryCache, Func<ICacheEntry, Task<TItem>>)

Asynchronously gets or creates a value for the specified key in the in-memory cache, using the provided factory function.

public static Task<TItem> GetOrCreateAsync<TItem>(this CacheEntryKey cacheEntryKey, IMemoryCache cache, Func<ICacheEntry, Task<TItem>> factory)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IMemoryCache

The in-memory cache instance to store the value.

factory Func<ICacheEntry, Task<TItem>>

The factory function to generate the value if it does not exist in the cache.

Returns

Task<TItem>

A task that represents the asynchronous get or create operation.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

GetOrCreate<TItem>(CacheEntryKey, IDistributedCache, Func<DistributedCacheEntryOptions, TItem>)

Gets or creates a value for the specified key in the distributed cache, using the provided factory function.

public static TItem GetOrCreate<TItem>(this CacheEntryKey cacheEntryKey, IDistributedCache distributedCache, Func<DistributedCacheEntryOptions, TItem> factory)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

distributedCache IDistributedCache

The distributed cache instance to store the value.

factory Func<DistributedCacheEntryOptions, TItem>

The factory function to generate the value if it does not exist in the cache.

Returns

TItem

The cached or newly created value.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

GetOrCreate<TItem>(CacheEntryKey, IMemoryCache, Func<ICacheEntry, TItem>)

Gets or creates a value for the specified key in the in-memory cache, using the provided factory function.

public static TItem GetOrCreate<TItem>(this CacheEntryKey cacheEntryKey, IMemoryCache cache, Func<ICacheEntry, TItem> factory)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IMemoryCache

The in-memory cache instance to store the value.

factory Func<ICacheEntry, TItem>

The factory function to generate the value if it does not exist in the cache.

Returns

TItem

The cached or newly created value.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

Remove(CacheEntryKey, IDistributedCache)

Removes a specific cache entry from distributed cache.

The Remove(CacheEntryKey, IDistributedCache) method removes the cache entry corresponding to the cache key from the distributed cache.

public static void Remove(this CacheEntryKey cacheEntryKey, IDistributedCache distributedCache)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

distributedCache IDistributedCache

The IDistributedCache instance used to remove the cache entry.

Exceptions

ArgumentNullException

When any input parameter is null.

Remove(CacheEntryKey, IMemoryCache)

Remove a specific cache entry from memory cache.

The Remove(CacheEntryKey, IMemoryCache) method removes the cache entry corresponding to the cache key from the memory cache.

public static void Remove(this CacheEntryKey cacheEntryKey, IMemoryCache memoryCache)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

memoryCache IMemoryCache

The IMemoryCache instance used to remove the cache entry.

Exceptions

ArgumentNullException

When any input parameter is null.

Remove(CacheEntryKey, IMemoryCache, Func<object, bool>)

Deletes cache entries from memory cache based on the provided predicate.

The Remove(CacheEntryKey, IMemoryCache, Func<object, bool>) method will evaluate the given predicate to filter cache keys, and removes the corresponding cache entries from the memory cache.

public static void Remove(this CacheEntryKey cacheEntryKey, IMemoryCache memoryCache, Func<object, bool> predicate)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

memoryCache IMemoryCache

The IMemoryCache instance used to remove the cache entries.

predicate Func<object, bool>

A function used to evaluate and filter cache keys to be deleted.

Exceptions

ArgumentNullException

When any input parameter is null.

RemoveAsync(CacheEntryKey, IDistributedCache, CancellationToken)

Removes a specific cache entry from distributed cache asynchronously.

The RemoveAsync(CacheEntryKey, IDistributedCache, CancellationToken) method removes the cache entry corresponding to the cache key from the distributed cache asynchronously.

public static Task RemoveAsync(this CacheEntryKey cacheEntryKey, IDistributedCache distributedCache, CancellationToken cancellationToken = default)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

distributedCache IDistributedCache

The IDistributedCache instance used to remove the cache entry.

cancellationToken CancellationToken

A cancellation token to cancel the operation.

Returns

Task

A task representing the asynchronous operation.

Exceptions

ArgumentNullException

When any input parameter is null.

SetAsync<TItem>(CacheEntryKey, IDistributedCache, TItem, DistributedCacheEntryOptions, CancellationToken)

Asynchronously sets a value with the specified key in the distributed cache.

public static Task SetAsync<TItem>(this CacheEntryKey cacheEntryKey, IDistributedCache cache, TItem value, DistributedCacheEntryOptions options, CancellationToken cancellationToken = default)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IDistributedCache

The distributed cache instance to store the value.

value TItem

The item to store in the cache.

options DistributedCacheEntryOptions

The options for the cache entry.

cancellationToken CancellationToken

A cancellation token to cancel the operation.

Returns

Task

A task that represents the asynchronous set operation.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

SetAsync<TItem>(CacheEntryKey, IDistributedCache, TItem, CancellationToken)

Asynchronously sets a value with the specified key in the distributed cache.

public static Task SetAsync<TItem>(this CacheEntryKey cacheEntryKey, IDistributedCache cache, TItem value, CancellationToken cancellationToken = default)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IDistributedCache

The distributed cache instance to store the value.

value TItem

The item to store in the cache.

cancellationToken CancellationToken

A cancellation token to cancel the operation.

Returns

Task

A task that represents the asynchronous set operation.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

Set<TItem>(CacheEntryKey, IDistributedCache, TItem)

Sets a value with the specified key in the cache model.

public static void Set<TItem>(this CacheEntryKey cacheEntryKey, IDistributedCache cache, TItem value)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IDistributedCache

Concrete class which implements the IDistributedCache.

value TItem

Item to set.

Type Parameters

TItem

Item type to set.

Exceptions

ArgumentNullException

When any input parameter is null.

Set<TItem>(CacheEntryKey, IDistributedCache, TItem, DistributedCacheEntryOptions)

Sets a value with the specified key in the cache model.

public static void Set<TItem>(this CacheEntryKey cacheEntryKey, IDistributedCache cache, TItem value, DistributedCacheEntryOptions options)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IDistributedCache

Concrete class which implements the IDistributedCache.

value TItem

Item to set.

options DistributedCacheEntryOptions

The options for the cache entry.

Type Parameters

TItem

Item type to set.

Exceptions

ArgumentNullException

When any input parameter is null.

Set<TItem>(CacheEntryKey, IMemoryCache, TItem)

Sets a value with the specified key in the in-memory cache.

public static void Set<TItem>(this CacheEntryKey cacheEntryKey, IMemoryCache cache, TItem value)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IMemoryCache

The in-memory cache instance to store the value.

value TItem

The item to store in the cache.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

Set<TItem>(CacheEntryKey, IMemoryCache, TItem, MemoryCacheEntryOptions)

Sets a value with the specified key in the in-memory cache.

public static void Set<TItem>(this CacheEntryKey cacheEntryKey, IMemoryCache cache, TItem value, MemoryCacheEntryOptions options)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IMemoryCache

The in-memory cache instance to store the value.

value TItem

The item to store in the cache.

options MemoryCacheEntryOptions

The options for the cache entry.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

Set<TItem>(CacheEntryKey, IMemoryCache, TItem, Action<ICacheEntry>)

Sets a value with the specified key in the in-memory cache.

public static void Set<TItem>(this CacheEntryKey cacheEntryKey, IMemoryCache cache, TItem value, Action<ICacheEntry> action)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

cache IMemoryCache

The in-memory cache instance to store the value.

value TItem

The item to store in the cache.

action Action<ICacheEntry>

The action used to configure cache entry.

Type Parameters

TItem

The type of the item to be cached.

Exceptions

ArgumentNullException

When any input parameter is null.

TryGetValue(CacheEntryKey, IMemoryCache, out object)

Attempts to get a value for the specified key from the in-memory cache.

public static bool TryGetValue(this CacheEntryKey cacheEntryKey, IMemoryCache memoryCache, out object value)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

memoryCache IMemoryCache

The in-memory cache instance to retrieve the value from.

value object

The retrieved value, if found.

Returns

bool

True if the value was found; otherwise, false.

Exceptions

ArgumentNullException

When any input parameter is null.

TryGetValue<T>(CacheEntryKey, IMemoryCache, out T)

Attempts to get a strongly typed value for the specified key from the in-memory cache.

public static bool TryGetValue<T>(this CacheEntryKey cacheEntryKey, IMemoryCache memoryCache, out T value)

Parameters

cacheEntryKey CacheEntryKey

The CacheEntryKey representing the cache key.

memoryCache IMemoryCache

The in-memory cache instance to retrieve the value from.

value T

The retrieved value, if found.

Returns

bool

True if the value was found; otherwise, false.

Type Parameters

T

The type of the value to retrieve.

Exceptions

ArgumentNullException

When any input parameter is null.