- Mark `IEmailTemplateRepository` and `_repository` in `ReadEmailTemplateQueryHandler` as obsolete, suggesting the use of `IRepository`. - Update `ResetEmailTemplateCommand` with additional documentation and examples for `Type`. - Change return type of `CreateEnvelopeReceiverCommand` to `IRequest<CreateEnvelopeReceiverResponse>`. - Improve caching methods in `CacheExtensions.cs` for better functionality and clarity. - Add XML documentation to the `Ok` method in `MappingExtensions`. - Make `UserReference` property required in `ReadHistoryResponse`.
222 lines
7.8 KiB
C#
222 lines
7.8 KiB
C#
using Microsoft.Extensions.Caching.Distributed;
|
|
|
|
namespace EnvelopeGenerator.Application.Extensions;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class CacheExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
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), token: cToken)
|
|
: cache.SetAsync(key, BitConverter.GetBytes(value), options: options, token: cToken);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<long?> GetLongAsync(this IDistributedCache cache, string key, CancellationToken cToken = default)
|
|
{
|
|
var value = await cache.GetAsync(key, cToken);
|
|
return value is null ? null : BitConverter.ToInt64(value, 0);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<DateTime?> GetDateTimeAsync(this IDistributedCache cache, string key, CancellationToken cToken = default)
|
|
{
|
|
var value = await cache.GetAsync(key, cToken);
|
|
return value is null ? null : new(BitConverter.ToInt64(value, 0));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<TimeSpan?> GetTimeSpanAsync(this IDistributedCache cache, string key, CancellationToken cToken = default)
|
|
{
|
|
var value = await cache.GetAsync(key, cToken);
|
|
return value is null ? null : new(BitConverter.ToInt64(value, 0));
|
|
}
|
|
|
|
//TODO: use code generator
|
|
#region GetOrSetAsync
|
|
|
|
#region string
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="factory"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cacheInBackground"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
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 = await cache.GetStringAsync(key, cToken);
|
|
if (value is null)
|
|
{
|
|
// create new and save
|
|
value = factory();
|
|
|
|
Task CacheAsync() => options is null
|
|
? cache.SetStringAsync(key, value, cToken)
|
|
: cache.SetStringAsync(key, value, options, cToken);
|
|
|
|
if (cacheInBackground)
|
|
_ = Task.Run(async () => await CacheAsync(), cToken);
|
|
else
|
|
await CacheAsync();
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="factoryAsync"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cacheInBackground"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
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, cToken);
|
|
if(value is null)
|
|
{
|
|
// create new and save
|
|
value = await factoryAsync();
|
|
|
|
Task CacheAsync() => options is null
|
|
? 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(), cToken);
|
|
else
|
|
await CacheAsync();
|
|
}
|
|
|
|
return value;
|
|
}
|
|
#endregion
|
|
|
|
#region DateTime
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="factory"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cacheInBackground"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<DateTime> GetOrSetAsync(this IDistributedCache cache, string key, Func<DateTime> 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="factory"></param>
|
|
/// <param name="options"></param>
|
|
/// <param name="cacheInBackground"></param>
|
|
/// <param name="cToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<DateTime> GetOrSetAsync(this IDistributedCache cache, string key, Func<Task<DateTime>> 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
|
|
} |