Class CacheModelExtensions
- Namespace
- OpenSettings.Extensions
- Assembly
- OpenSettings.dll
Provides extension methods for CacheModel 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 CacheModelExtensions
- Inheritance
-
CacheModelExtensions
- Inherited Members
Methods
GetOrCreateAsync<TItem>(CacheModel, IDistributedCache, Func<CacheModel, 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 CacheModel model, IDistributedCache cache, Func<CacheModel, Task<TItem>> factory, CancellationToken cancellationToken = default)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IDistributedCacheThe distributed cache instance to store the value.
factory
Func<CacheModel, Task<TItem>>The factory function to generate the value if it does not exist in the cache.
cancellationToken
CancellationTokenA cancellation token to monitor for cancellation requests.
Returns
- Task<TItem>
A task that represents the asynchronous get or create operation.
Type Parameters
TItem
The type of the item to be cached.
GetOrCreateAsync<TItem>(CacheModel, IMemoryCache, Func<CacheModel, 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 CacheModel model, IMemoryCache cache, Func<CacheModel, Task<TItem>> factory)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IMemoryCacheThe in-memory cache instance to store the value.
factory
Func<CacheModel, 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.
GetOrCreate<TItem>(CacheModel, IDistributedCache, Func<CacheModel, 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 CacheModel model, IDistributedCache cache, Func<CacheModel, TItem> factory)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IDistributedCacheThe distributed cache instance to store the value.
factory
Func<CacheModel, 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.
GetOrCreate<TItem>(CacheModel, IMemoryCache, Func<CacheModel, 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 CacheModel model, IMemoryCache cache, Func<CacheModel, TItem> factory)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IMemoryCacheThe in-memory cache instance to store the value.
factory
Func<CacheModel, 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.
SetAsync<TItem>(CacheModel, IDistributedCache, TItem, CancellationToken)
Asynchronously sets a value with the specified key in the distributed cache.
public static Task SetAsync<TItem>(this CacheModel model, IDistributedCache cache, TItem value, CancellationToken cancellationToken = default)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IDistributedCacheThe distributed cache instance to store the value.
value
TItemThe item to store in the cache.
cancellationToken
CancellationTokenA cancellation token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous set operation.
Type Parameters
TItem
The type of the item to be cached.
Set<TItem>(CacheModel, IDistributedCache, TItem)
Sets a value with the specified key in the cache model.
public static void Set<TItem>(this CacheModel model, IDistributedCache cache, TItem value)
Parameters
model
CacheModelcache
IDistributedCacheConcrete class which implements the IDistributedCache.
value
TItemItem to set.
Type Parameters
TItem
Item type to set.
Set<TItem>(CacheModel, IMemoryCache, TItem)
Sets a value with the specified key in the in-memory cache.
public static void Set<TItem>(this CacheModel model, IMemoryCache cache, TItem value)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IMemoryCacheThe in-memory cache instance to store the value.
value
TItemThe item to store in the cache.
Type Parameters
TItem
The type of the item to be cached.
TryGetValue(CacheModel, IMemoryCache, out object)
Attempts to get a value for the specified key from the in-memory cache.
public static bool TryGetValue(this CacheModel model, IMemoryCache cache, out object value)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IMemoryCacheThe in-memory cache instance to retrieve the value from.
value
objectThe retrieved value, if found.
Returns
- bool
True if the value was found; otherwise, false.
TryGetValue<T>(CacheModel, 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 CacheModel model, IMemoryCache cache, out T value)
Parameters
model
CacheModelThe CacheModel representing the cache settings.
cache
IMemoryCacheThe in-memory cache instance to retrieve the value from.
value
TThe retrieved value, if found.
Returns
- bool
True if the value was found; otherwise, false.
Type Parameters
T
The type of the value to retrieve.