using System; using System.Threading; using System.Threading.Tasks; using LazyCache; using Microsoft.Extensions.Caching.Memory; namespace Ombi.Helpers { public class CacheService : ICacheService { protected readonly IMemoryCache _memoryCache; public CacheService(IMemoryCache memoryCache) { _memoryCache = memoryCache; } public virtual async Task GetOrAddAsync(string cacheKey, Func> factory, DateTimeOffset absoluteExpiration = default) { if (absoluteExpiration == default) { absoluteExpiration = DateTimeOffset.Now.AddHours(1); } return await _memoryCache.GetOrCreateAsync(cacheKey, entry => { entry.AbsoluteExpiration = absoluteExpiration; return factory(); }); } public void Remove(string key) { _memoryCache.Remove(key); } public T GetOrAdd(string cacheKey, Func factory, DateTimeOffset absoluteExpiration) { // locks get and set internally return _memoryCache.GetOrCreate(cacheKey, entry => { entry.AbsoluteExpiration = absoluteExpiration; return factory(); }); } private static class TypeLock { public static object Lock { get; } = new object(); } } }