|
| 1 | +using Microsoft.Extensions.Caching.Memory; |
| 2 | + |
| 3 | +namespace AutSoft.Common.Caching; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Extension functions for <see cref="IMemoryCache"/> |
| 7 | +/// </summary> |
| 8 | +public static class MemoryCacheExtensions |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Get or create a cached element with error handling |
| 12 | + /// </summary> |
| 13 | + /// <typeparam name="TItem">The type of the cached element</typeparam> |
| 14 | + /// <param name="cache">An <see cref="IMemoryCache"/> object</param> |
| 15 | + /// <param name="key">The key of the cached element</param> |
| 16 | + /// <param name="factory">The factory method, which can create the element if it isn't cached</param> |
| 17 | + /// <param name="options">Options of the caching</param> |
| 18 | + /// <returns>The finded or created element</returns> |
| 19 | + public static async Task<TItem> GetOrCreateWithErrorHandlingAsync<TItem>( |
| 20 | + this IMemoryCache cache, |
| 21 | + string key, |
| 22 | + Func<ICacheEntry, Task<TItem>> factory, |
| 23 | + MemoryCacheEntryOptions? options = null) |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + return await cache.GetOrCreateAsync(key, entry => |
| 28 | + { |
| 29 | + entry.SetOptions(options); |
| 30 | + return factory(entry); |
| 31 | + }); |
| 32 | + } |
| 33 | + catch |
| 34 | + { |
| 35 | + cache.Remove(key); |
| 36 | + throw; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Create or override an element in the cache |
| 42 | + /// </summary> |
| 43 | + /// <typeparam name="TItem">The type of the cached element</typeparam> |
| 44 | + /// <param name="cache">An <see cref="IMemoryCache"/> object</param> |
| 45 | + /// <param name="key">The key of the cached element</param> |
| 46 | + /// <param name="value">The cached value</param> |
| 47 | + /// <param name="size">The size of the cached value</param> |
| 48 | + /// <param name="options">Options of the caching</param> |
| 49 | + public static void SetWithErrorHandling<TItem>( |
| 50 | + this IMemoryCache cache, |
| 51 | + string key, |
| 52 | + TItem value, |
| 53 | + int size, |
| 54 | + MemoryCacheEntryOptions? options = null) |
| 55 | + { |
| 56 | + try |
| 57 | + { |
| 58 | + using var entry = cache.CreateEntry(key); |
| 59 | + entry.SetOptions(options); |
| 60 | + entry.Size = size; |
| 61 | + entry.Value = value; |
| 62 | + } |
| 63 | + catch |
| 64 | + { |
| 65 | + cache.Remove(key); |
| 66 | + throw; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static void SetOptions(this ICacheEntry entry, MemoryCacheEntryOptions? options = null) |
| 71 | + { |
| 72 | + if (options == null) |
| 73 | + { |
| 74 | + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1); |
| 75 | + entry.SlidingExpiration = TimeSpan.FromMinutes(10); |
| 76 | + entry.Priority = CacheItemPriority.Normal; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; |
| 81 | + entry.SlidingExpiration = options.SlidingExpiration; |
| 82 | + entry.Priority = options.Priority; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Invalidate a cached element's value |
| 88 | + /// </summary> |
| 89 | + /// <param name="cache">An <see cref="IMemoryCache"/> object</param> |
| 90 | + /// <param name="key">The key of the cached element</param> |
| 91 | + public static void Invalidate(this IMemoryCache cache, string key) |
| 92 | + => cache.Remove(key); |
| 93 | +} |
0 commit comments