| | | 1 | | using Elsa.Caching.Options; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using Microsoft.Extensions.Primitives; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Caching.Services; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | 7 | 9 | | public class CacheManager(IMemoryCache memoryCache, IChangeTokenSignaler changeTokenSignaler, IOptions<CachingOptions> o |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | 7266 | 12 | | public IOptions<CachingOptions> CachingOptions => options; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public IChangeToken GetToken(string key) |
| | | 16 | | { |
| | 7374 | 17 | | return changeTokenSignaler.GetToken(key); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public ValueTask TriggerTokenAsync(string key, CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 5221 | 23 | | return changeTokenSignaler.TriggerTokenAsync(key, cancellationToken); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async Task<TItem?> FindOrCreateAsync<TItem>(object key, Func<ICacheEntry, Task<TItem>> factory) |
| | | 28 | | { |
| | 14059 | 29 | | return await memoryCache.GetOrCreateAsync(key, async entry => await factory(entry)); |
| | 7197 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Retrieves a cached item by the specified key or creates a new one using the provided factory function. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="key">The key used to identify the cached item.</param> |
| | | 36 | | /// <param name="factory">A factory function that provides the value to be cached if it does not already exist.</par |
| | | 37 | | /// <typeparam name="TItem">The type of the item to retrieve or create.</typeparam> |
| | | 38 | | /// <returns>The cached or newly created item.</returns> |
| | | 39 | | /// <exception cref="InvalidOperationException">Thrown if the factory function returns null.</exception> |
| | | 40 | | public async Task<TItem> GetOrCreateAsync<TItem>(object key, Func<ICacheEntry, Task<TItem>> factory) |
| | | 41 | | { |
| | 1470 | 42 | | return await memoryCache.GetOrCreateAsync(key, async entry => await factory(entry)) ?? throw new InvalidOperatio |
| | 1064 | 43 | | } |
| | | 44 | | } |