using Microsoft.Extensions.Caching.Distributed; namespace EnvelopeGenerator.Application.Extensions; /// /// /// public static class CacheExtensions { /// /// /// /// /// /// /// /// /// 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); /// /// /// /// /// /// /// public static async Task 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); } /// /// /// /// /// /// /// /// /// 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, CancellationToken cToken = default) { 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, CancellationToken cToken = default) => cache.SetLongAsync(key: key, value: value.Ticks, options: options, cToken); /// /// /// /// /// /// /// public static async Task 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 /// /// /// /// /// /// /// /// /// /// 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); 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; } /// /// /// /// /// /// /// /// /// /// 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, 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 /// /// /// /// /// /// /// /// /// /// 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 }