From 80f9107e4eff9ac0e0d1336f4295585d1d6eb7aa Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 27 Jan 2025 14:50:23 +0100 Subject: [PATCH] =?UTF-8?q?feat(cache):=20Unterst=C3=BCtzung=20f=C3=BCr=20?= =?UTF-8?q?GetOrSetAsync=20mit=20DateTime-Typ=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - GetOrSetAsync für DateTime mit synchronen und asynchronen Fabrikmethoden implementiert. - Bestehende GetOrSetAsync-Methoden für Zeichenfolgen und asynchrone Zeichenfolgen refaktoriert, um Klarheit und Struktur zu verbessern. - Code mit Regionen organisiert, um ähnliche Methoden für bessere Lesbarkeit zu gruppieren. - TODO für weitere Verbesserungen bei der Codegenerierung für GetOrSetAsync-Methoden hinzugefügt. --- .../Extensions/CacheExtensions.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs b/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs index f2adcebd..5e9eebd8 100644 --- a/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/CacheExtensions.cs @@ -33,6 +33,10 @@ namespace EnvelopeGenerator.Application.Extensions return value is null ? null : new(BitConverter.ToInt64(value, 0)); } + //TODO: use code generator + #region GetOrSetAsync + + #region string public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken cToken = default) { var value = await cache.GetStringAsync(key, cToken); @@ -74,5 +78,54 @@ namespace EnvelopeGenerator.Application.Extensions return value; } + #endregion + + #region DateTime + public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken cToken = default) + { + if (await cache.GetDateTimeAsync(key, cToken) is DateTime dateTimeValue) + return dateTimeValue; + else + { + // create new and save + var newValue = factory(); + + Task CacheAsync() => options is null + ? cache.SetDateTimeAsync(key, newValue, cToken: cToken) + : cache.SetDateTimeAsync(key, newValue, options, cToken); + + if (cacheInBackground) + _ = Task.Run(async () => await CacheAsync(), cToken); + else + await CacheAsync(); + + return newValue; + } + } + + public static async Task GetOrSetAsync(this IDistributedCache cache, string key, Func> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken cToken = default) + { + if (await cache.GetDateTimeAsync(key, cToken) is DateTime dateTimeValue) + return dateTimeValue; + else + { + // create new and save + var newValue = await factory(); + + Task CacheAsync() => options is null + ? cache.SetDateTimeAsync(key, newValue, cToken: cToken) + : cache.SetDateTimeAsync(key, newValue, options, cToken); + + if (cacheInBackground) + _ = Task.Run(async () => await CacheAsync(), cToken); + else + await CacheAsync(); + + return newValue; + } + } + #endregion + + #endregion } } \ No newline at end of file