Upgraded projects to .NET 9.0 and added new projects `KBR.Cache`, `KBR.Shared`, and `KBR.Shared.Lite` to the solution. Introduced JWT authentication and OTP handling with new models, services, and configuration options. Updated database schema with new entities `Users` and `RefreshTokens`, and added migrations for schema changes. Implemented caching strategies using `AppDistributedCache` with support for in-memory, SQL Server, and Redis. Enhanced email handling with `MailHelpers` for domain replacement. Updated controllers, repositories, and configuration files to support new features.
97 lines
3.0 KiB
C#
97 lines
3.0 KiB
C#
|
|
using KBR.Shared.Cache;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
|
|
namespace KBR.Cache
|
|
{
|
|
internal class AppDistributedCache : IAppDistributedCache
|
|
{
|
|
private readonly IDistributedCache _cache;
|
|
private readonly DistributedCacheEntryOptions _entryOptions;
|
|
|
|
public AppDistributedCache(IDistributedCache cache)
|
|
{
|
|
this._cache = cache;
|
|
this._entryOptions = new DistributedCacheEntryOptions();
|
|
}
|
|
|
|
public T Get<T>(string key)
|
|
where T : class
|
|
{
|
|
if (this._cache.Get(key) is var result && result != null)
|
|
{
|
|
return result.FromByteArray<T>();
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
public async Task<T> GetAsync<T>(string key, CancellationToken cancellationToken = default)
|
|
where T : class
|
|
{
|
|
var result = await this._cache.GetAsync(key, cancellationToken);
|
|
|
|
if (result != null)
|
|
{
|
|
return result.FromByteArray<T>();
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
public void Set<T>(string key, T obj, DistributedCacheEntryOptions? entryOptions = default)
|
|
{
|
|
this._cache.Set(key, obj.ToByteArray(), entryOptions ?? this._entryOptions);
|
|
}
|
|
|
|
public async Task SetAsync<T>(string key, T obj, DistributedCacheEntryOptions? entryOptions = default, CancellationToken cancellationToken = default)
|
|
{
|
|
await this._cache.SetAsync(key, obj.ToByteArray(), entryOptions ?? this._entryOptions, cancellationToken);
|
|
}
|
|
|
|
public void Remove(string key)
|
|
{
|
|
this._cache.Remove(key);
|
|
}
|
|
|
|
public async Task RemoveAsync(string key, CancellationToken cancellationToken = default)
|
|
{
|
|
await this._cache.RemoveAsync(key, cancellationToken);
|
|
}
|
|
|
|
public TItem GetOrCreate<TItem>(string key, Func<TItem> factory)
|
|
where TItem : class
|
|
{
|
|
byte[] result = this._cache.Get(key);
|
|
var resultData = result?.FromByteArray<TItem>();
|
|
if (result == null || resultData == null)
|
|
{
|
|
var data = factory();
|
|
this._cache.Set(key, data.ToByteArray(), this._entryOptions);
|
|
return data;
|
|
}
|
|
|
|
return resultData;
|
|
}
|
|
|
|
public async Task<TItem> GetOrCreateAsync<TItem>(string key, Func<Task<TItem>> factory, CancellationToken cancellationToken = default)
|
|
where TItem : class
|
|
{
|
|
byte[] result = await this._cache.GetAsync(key, cancellationToken);
|
|
var resultData = result?.FromByteArray<TItem>();
|
|
if (result == null || resultData == null)
|
|
{
|
|
var data = await factory();
|
|
if (data != null)
|
|
{
|
|
await this._cache.SetAsync(key, data.ToByteArray(), this._entryOptions, cancellationToken);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
return resultData;
|
|
}
|
|
}
|
|
}
|