Files
annotations/Azaion.CommonSecurity/Services/Cache.cs
T
Alex Bezdieniezhnykh 0c66607ed7 failsafe load dlls
add user config queue offsets
throttle improvements
2025-04-17 01:19:48 +03:00

27 lines
715 B
C#

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