refactor(cache): Unterstützung für CancellationToken in IDistributedCache-Erweiterungsmethoden hinzufügen
- 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.
This commit is contained in:
parent
af5d7c289d
commit
c6e9ecfbca
@ -4,74 +4,70 @@ namespace EnvelopeGenerator.Application.Extensions
|
|||||||
{
|
{
|
||||||
public static class CacheExtensions
|
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
|
=> options is null
|
||||||
? cache.SetAsync(key, BitConverter.GetBytes(value))
|
? cache.SetAsync(key, BitConverter.GetBytes(value), token: cToken)
|
||||||
: cache.SetAsync(key, BitConverter.GetBytes(value), options: options);
|
: cache.SetAsync(key, BitConverter.GetBytes(value), options: options, token: cToken);
|
||||||
|
|
||||||
public static async Task<long?> GetLongAsync(this IDistributedCache cache, string key)
|
public static async Task<long?> 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);
|
return value is null ? null : BitConverter.ToInt64(value, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task SetDateTimeAsync(this IDistributedCache cache, string key, DateTime value, DistributedCacheEntryOptions? options = null)
|
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);
|
=> cache.SetLongAsync(key: key, value: value.Ticks, options: options, cToken: cToken);
|
||||||
|
|
||||||
public static async Task<DateTime?> GetDateTimeAsync(this IDistributedCache cache, string key)
|
public static async Task<DateTime?> 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));
|
return value is null ? null : new(BitConverter.ToInt64(value, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task SetTimeSpanAsync(this IDistributedCache cache, string key, TimeSpan value, DistributedCacheEntryOptions? options = null)
|
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);
|
=> cache.SetLongAsync(key: key, value: value.Ticks, options: options, cToken);
|
||||||
|
|
||||||
public static async Task<TimeSpan?> GetTimeSpanAsync(this IDistributedCache cache, string key)
|
public static async Task<TimeSpan?> 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));
|
return value is null ? null : new(BitConverter.ToInt64(value, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetOrSet(this IDistributedCache cache, string key, Func<string> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default)
|
public static async Task<string> GetOrSetAsync(this IDistributedCache cache, string key, Func<string> 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)
|
if (value is null)
|
||||||
{
|
{
|
||||||
// create new and save
|
// create new and save
|
||||||
value = factory();
|
value = factory();
|
||||||
|
|
||||||
void Cache()
|
Task CacheAsync() => options is null
|
||||||
{
|
? cache.SetStringAsync(key, value, cToken)
|
||||||
if (options is null)
|
: cache.SetStringAsync(key, value, options, cToken);
|
||||||
cache.SetString(key: key, value: value);
|
|
||||||
else
|
|
||||||
cache.SetString(key: key, value: value, options: options);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cacheInBackground)
|
if (cacheInBackground)
|
||||||
_ = Task.Run(() => Cache(), token);
|
_ = Task.Run(async () => await CacheAsync(), cToken);
|
||||||
else
|
else
|
||||||
Cache();
|
await CacheAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<string> GetOrSetAsync(this IDistributedCache cache, string key, Func<Task<string>> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default)
|
public static async Task<string> GetOrSetAsync(this IDistributedCache cache, string key, Func<Task<string>> 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)
|
if(value is null)
|
||||||
{
|
{
|
||||||
// create new and save
|
// create new and save
|
||||||
value = await factory();
|
value = await factoryAsync();
|
||||||
|
|
||||||
Task CacheAsync() => options is null
|
Task CacheAsync() => options is null
|
||||||
? cache.SetStringAsync(key: key, value: value, token: token)
|
? cache.SetStringAsync(key: key, value: value, token: cToken)
|
||||||
: cache.SetStringAsync(key: key, value: value, options: options, token: token);
|
: cache.SetStringAsync(key: key, value: value, options: options, token: cToken);
|
||||||
|
|
||||||
if (cacheInBackground)
|
if (cacheInBackground)
|
||||||
_ = Task.Run(async () => await CacheAsync(), token);
|
_ = Task.Run(async () => await CacheAsync(), cToken);
|
||||||
else
|
else
|
||||||
await CacheAsync();
|
await CacheAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user