CacheExtensions.GetOrCreateAsync Method

Definition

Overloads

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

Asynchronously gets the value associated with this key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found.

GetOrCreateAsync<TItem>(IMemoryCache, Object, Func<ICacheEntry,Task<TItem>>, MemoryCacheEntryOptions)

Asynchronously gets the value associated with this key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found.

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

Source:
MemoryCacheExtensions.cs
Source:
MemoryCacheExtensions.cs
Source:
MemoryCacheExtensions.cs

Asynchronously gets the value associated with this key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found.

public static System.Threading.Tasks.Task<TItem?> GetOrCreateAsync<TItem>(this Microsoft.Extensions.Caching.Memory.IMemoryCache cache, object key, Func<Microsoft.Extensions.Caching.Memory.ICacheEntry,System.Threading.Tasks.Task<TItem>> factory);

Type Parameters

TItem

The type of the object to get.

Parameters

cache
IMemoryCache

The IMemoryCache instance this method extends.

key
Object

The key of the entry to look for or create.

factory
Func<ICacheEntry,Task<TItem>>

The factory task that creates the value associated with this key if the key does not exist in the cache.

Returns

Task<TItem>

The task object representing the asynchronous operation.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Standard 2.0 (package-provided)

GetOrCreateAsync<TItem>(IMemoryCache, Object, Func<ICacheEntry,Task<TItem>>, MemoryCacheEntryOptions)

Source:
MemoryCacheExtensions.cs
Source:
MemoryCacheExtensions.cs

Asynchronously gets the value associated with this key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found.

public static System.Threading.Tasks.Task<TItem?> GetOrCreateAsync<TItem>(this Microsoft.Extensions.Caching.Memory.IMemoryCache cache, object key, Func<Microsoft.Extensions.Caching.Memory.ICacheEntry,System.Threading.Tasks.Task<TItem>> factory, Microsoft.Extensions.Caching.Memory.MemoryCacheEntryOptions? createOptions);

Type Parameters

TItem

The type of the object to get.

Parameters

cache
IMemoryCache

The IMemoryCache instance this method extends.

key
Object

The key of the entry to look for or create.

factory
Func<ICacheEntry,Task<TItem>>

The factory task that creates the value associated with this key if the key does not exist in the cache.

createOptions
MemoryCacheEntryOptions

The options to be applied to the ICacheEntry if the key does not exist in the cache.

Returns

Task<TItem>

The task object representing the asynchronous operation.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Standard 2.0 (package-provided)