From c6e9ecfbca89dc7080762916c516f98c3f114cc2 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 27 Jan 2025 14:23:06 +0100 Subject: [PATCH] =?UTF-8?q?refactor(cache):=20Unterst=C3=BCtzung=20f=C3=BC?= =?UTF-8?q?r=20CancellationToken=20in=20IDistributedCache-Erweiterungsmeth?= =?UTF-8?q?oden=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Aktualisierte `SetLongAsync`, `GetLongAsync`, `SetDateTimeAsync`, `GetDateTimeAsync`, `SetTimeSpanAsync` und `GetTimeSpanAsync`, um optionale `CancellationToken`-Parameter zu unterstützen. - Modifizierte `GetOrSetAsync`, um zwischen synchronen und asynchronen Fabrikfunktionen zu unterscheiden. - Sicherstellung einer konsistenten Handhabung von `CancellationToken` in allen Cache-bezogenen Operationen. --- .../Extensions/CacheExtensions.cs | 56 +++++++++---------- .../CacheExtensions.cs | 12 ---- 2 files changed, 26 insertions(+), 42 deletions(-) delete mode 100644 EnvelopeGenerator.Extensions/CacheExtensions.cs diff --git a/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs b/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs index 45f43109..f2adcebd 100644 --- a/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs @@ -4,74 +4,70 @@ namespace EnvelopeGenerator.Application.Extensions { public static class CacheExtensions { - public static Task SetLongAsync(this IDistributedCache cache, string key, long value, DistributedCacheEntryOptions? options = null) + public static Task SetLongAsync(this IDistributedCache cache, string key, long value, DistributedCacheEntryOptions? options = null, CancellationToken cToken = default) => options is null - ? cache.SetAsync(key, BitConverter.GetBytes(value)) - : cache.SetAsync(key, BitConverter.GetBytes(value), options: options); + ? cache.SetAsync(key, BitConverter.GetBytes(value), token: cToken) + : cache.SetAsync(key, BitConverter.GetBytes(value), options: options, token: cToken); - public static async Task GetLongAsync(this IDistributedCache cache, string key) + public static async Task GetLongAsync(this IDistributedCache cache, string key, CancellationToken cToken = default) { - var value = await cache.GetAsync(key); + var value = await cache.GetAsync(key, cToken); return value is null ? null : BitConverter.ToInt64(value, 0); } - public static Task SetDateTimeAsync(this IDistributedCache cache, string key, DateTime value, DistributedCacheEntryOptions? options = null) - => cache.SetLongAsync(key: key, value: value.Ticks, options: options); + public static Task SetDateTimeAsync(this IDistributedCache cache, string key, DateTime value, DistributedCacheEntryOptions? options = null, CancellationToken cToken = default) + => cache.SetLongAsync(key: key, value: value.Ticks, options: options, cToken: cToken); - public static async Task GetDateTimeAsync(this IDistributedCache cache, string key) + public static async Task GetDateTimeAsync(this IDistributedCache cache, string key, CancellationToken cToken = default) { - var value = await cache.GetAsync(key); + var value = await cache.GetAsync(key, cToken); return value is null ? null : new(BitConverter.ToInt64(value, 0)); } - public static Task SetTimeSpanAsync(this IDistributedCache cache, string key, TimeSpan value, DistributedCacheEntryOptions? options = null) - => cache.SetLongAsync(key: key, value: value.Ticks, options: options); + public static Task SetTimeSpanAsync(this IDistributedCache cache, string key, TimeSpan value, DistributedCacheEntryOptions? options = null, CancellationToken cToken = default) + => cache.SetLongAsync(key: key, value: value.Ticks, options: options, cToken); - public static async Task GetTimeSpanAsync(this IDistributedCache cache, string key) + public static async Task GetTimeSpanAsync(this IDistributedCache cache, string key, CancellationToken cToken = default) { - var value = await cache.GetAsync(key); + var value = await cache.GetAsync(key, cToken); return value is null ? null : new(BitConverter.ToInt64(value, 0)); } - public static string GetOrSet(this IDistributedCache cache, string key, Func factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default) + public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken cToken = default) { - var value = cache.GetString(key); + var value = await cache.GetStringAsync(key, cToken); if (value is null) { // create new and save value = factory(); - void Cache() - { - if (options is null) - cache.SetString(key: key, value: value); - else - cache.SetString(key: key, value: value, options: options); - } + Task CacheAsync() => options is null + ? cache.SetStringAsync(key, value, cToken) + : cache.SetStringAsync(key, value, options, cToken); if (cacheInBackground) - _ = Task.Run(() => Cache(), token); + _ = Task.Run(async () => await CacheAsync(), cToken); else - Cache(); + await CacheAsync(); } return value; } - public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default) + public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func> factoryAsync, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken cToken = default) { - var value = await cache.GetStringAsync(key, token: token); + var value = await cache.GetStringAsync(key, cToken); if(value is null) { // create new and save - value = await factory(); + value = await factoryAsync(); Task CacheAsync() => options is null - ? cache.SetStringAsync(key: key, value: value, token: token) - : cache.SetStringAsync(key: key, value: value, options: options, token: token); + ? cache.SetStringAsync(key: key, value: value, token: cToken) + : cache.SetStringAsync(key: key, value: value, options: options, token: cToken); if (cacheInBackground) - _ = Task.Run(async () => await CacheAsync(), token); + _ = Task.Run(async () => await CacheAsync(), cToken); else await CacheAsync(); } diff --git a/EnvelopeGenerator.Extensions/CacheExtensions.cs b/EnvelopeGenerator.Extensions/CacheExtensions.cs deleted file mode 100644 index 94e54af5..00000000 --- a/EnvelopeGenerator.Extensions/CacheExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.Extensions.Caching.Distributed; - -namespace EnvelopeGenerator.Extensions -{ - public static class CacheExtensions - { - public static IDistributedCache Cache(this IDistributedCache cache) - { - cache.SetStringAsync() - } - } -} \ No newline at end of file