Files
admin/Azaion.Services/Cache.cs
T
Alex Bezdieniezhnykh 49de0351c1 add Cache.cs
fix hardware hash stack in the jwt token claims
2025-01-18 14:37:17 +02:00

27 lines
763 B
C#

using LazyCache;
namespace Azaion.Services;
public interface ICache
{
Task<T> GetFromCacheAsync<T>(string key, Func<Task<T>> fetchFunc, TimeSpan? expiration = null);
void Invalidate(string key);
}
public class MemoryCache : ICache
{
private readonly IAppCache _cache = new CachingService();
public async Task<T> GetFromCacheAsync<T>(string key, Func<Task<T>> fetchFunc, TimeSpan? expiration = null)
{
expiration ??= TimeSpan.FromHours(4);
return await _cache.GetOrAddAsync(key, async entry =>
{
var result = await fetchFunc();
entry.AbsoluteExpirationRelativeToNow = expiration;
return result;
});
}
public void Invalidate(string key) => _cache.Remove(key);
}