feat: GetOrCreate und GetOrCreateAsync-Methoden zu CacheExtensions hinzugefügt
- GetOrCreate und GetOrCreateAsync-Methoden hinzugefügt, um Caching mit optionalem Hintergrund-Caching zu ermöglichen. - Methoden prüfen zuerst den Cache, und wenn der Wert nicht gefunden wird, wird der Wert mit einer bereitgestellten Fabrikfunktion erstellt und zwischengespeichert. - Unterstützt asynchrones und synchrones Caching mit optionalen DistributedCacheEntryOptions.
This commit is contained in:
parent
2e790b4e4c
commit
1bc31fe0ee
@ -18,6 +18,9 @@
|
||||
<PackageReference Include="DigitalData.Core.DTO" Version="2.0.0" />
|
||||
<PackageReference Include="DigitalData.EmailProfilerDispatcher" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.18" />
|
||||
<PackageReference Include="Otp.NET" Version="1.4.0" />
|
||||
<PackageReference Include="QRCoder" Version="1.6.0" />
|
||||
<PackageReference Include="QRCoder-ImageSharp" Version="0.10.0" />
|
||||
<PackageReference Include="UserManager.Application" Version="2.0.0" />
|
||||
<PackageReference Include="UserManager.Infrastructure" Version="2.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -1,11 +1,4 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Extensions
|
||||
{
|
||||
@ -39,5 +32,51 @@ namespace EnvelopeGenerator.Application.Extensions
|
||||
var value = await cache.GetAsync(key);
|
||||
return value is null ? null : new(BitConverter.ToInt64(value, 0));
|
||||
}
|
||||
|
||||
public static string GetOrCreate(this IDistributedCache cache, string key, Func<string> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default)
|
||||
{
|
||||
var value = cache.GetString(key);
|
||||
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);
|
||||
}
|
||||
|
||||
if (cacheInBackground)
|
||||
_ = Task.Run(() => Cache(), token);
|
||||
else
|
||||
Cache();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static async Task<string> GetOrCreateAsync(this IDistributedCache cache, string key, Func<Task<string>> factory, DistributedCacheEntryOptions? options = null, bool cacheInBackground = false, CancellationToken token = default)
|
||||
{
|
||||
var value = await cache.GetStringAsync(key, token: token);
|
||||
if(value is null)
|
||||
{
|
||||
// create new and save
|
||||
value = await factory();
|
||||
|
||||
Task CacheAsync() => options is null
|
||||
? cache.SetStringAsync(key: key, value: value, token: token)
|
||||
: cache.SetStringAsync(key: key, value: value, options: options, token: token);
|
||||
|
||||
if (cacheInBackground)
|
||||
_ = Task.Run(async () => await CacheAsync(), token);
|
||||
else
|
||||
await CacheAsync();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user