From 6327782c74e572bd15d93b9ae933a41c807267e5 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 27 Jul 2021 09:38:20 +0100 Subject: [PATCH] Fixed the discord notification test issue --- src/Ombi.Helpers/CacheService.cs | 17 +++++++++++++---- src/Ombi.Helpers/MediaCacheService.cs | 15 ++++++++++----- src/Ombi/Startup.cs | 1 + 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Ombi.Helpers/CacheService.cs b/src/Ombi.Helpers/CacheService.cs index 3cf392880..c0acf0dea 100644 --- a/src/Ombi.Helpers/CacheService.cs +++ b/src/Ombi.Helpers/CacheService.cs @@ -2,13 +2,14 @@ using System.Threading; using System.Threading.Tasks; using LazyCache; +using Microsoft.Extensions.Caching.Memory; namespace Ombi.Helpers { public class CacheService : ICacheService { - protected readonly IAppCache _memoryCache; - public CacheService(IAppCache memoryCache) + protected readonly IMemoryCache _memoryCache; + public CacheService(IMemoryCache memoryCache) { _memoryCache = memoryCache; } @@ -20,7 +21,11 @@ namespace Ombi.Helpers absoluteExpiration = DateTimeOffset.Now.AddHours(1); } - return await _memoryCache.GetOrAddAsync(cacheKey, () => factory(), absoluteExpiration); + return await _memoryCache.GetOrCreateAsync(cacheKey, entry => + { + entry.AbsoluteExpiration = absoluteExpiration; + return factory(); + }); } public void Remove(string key) @@ -31,7 +36,11 @@ namespace Ombi.Helpers public T GetOrAdd(string cacheKey, Func factory, DateTimeOffset absoluteExpiration) { // locks get and set internally - return _memoryCache.GetOrAdd(cacheKey, () => factory(), absoluteExpiration); + return _memoryCache.GetOrCreate(cacheKey, entry => + { + entry.AbsoluteExpiration = absoluteExpiration; + return factory(); + }); } private static class TypeLock diff --git a/src/Ombi.Helpers/MediaCacheService.cs b/src/Ombi.Helpers/MediaCacheService.cs index 1cb969b60..c514c8e25 100644 --- a/src/Ombi.Helpers/MediaCacheService.cs +++ b/src/Ombi.Helpers/MediaCacheService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using LazyCache; +using Microsoft.Extensions.Caching.Memory; namespace Ombi.Helpers { @@ -14,7 +15,7 @@ namespace Ombi.Helpers { private const string CacheKey = "MediaCacheServiceKeys"; - public MediaCacheService(IAppCache memoryCache) : base(memoryCache) + public MediaCacheService(IMemoryCache memoryCache) : base(memoryCache) { } @@ -33,24 +34,28 @@ namespace Ombi.Helpers // Not in the cache, so add this Key into our MediaServiceCache await UpdateLocalCache(cacheKey); - return await _memoryCache.GetOrAddAsync(cacheKey, () => factory(), absoluteExpiration); + return await _memoryCache.GetOrCreateAsync(cacheKey, entry => + { + entry.AbsoluteExpiration = absoluteExpiration; + return factory(); + }); } private async Task UpdateLocalCache(string cacheKey) { - var mediaServiceCache = await _memoryCache.GetAsync>(CacheKey); + var mediaServiceCache = _memoryCache.Get>(CacheKey); if (mediaServiceCache == null) { mediaServiceCache = new List(); } mediaServiceCache.Add(cacheKey); _memoryCache.Remove(CacheKey); - _memoryCache.Add(CacheKey, mediaServiceCache); + _memoryCache.Set(CacheKey, mediaServiceCache); } public async Task Purge() { - var keys = await _memoryCache.GetAsync>(CacheKey); + var keys = _memoryCache.Get>(CacheKey); if (keys == null) { return; diff --git a/src/Ombi/Startup.cs b/src/Ombi/Startup.cs index 4b82c3124..95fd6447f 100644 --- a/src/Ombi/Startup.cs +++ b/src/Ombi/Startup.cs @@ -84,6 +84,7 @@ namespace Ombi //{ // setup.AddHealthCheckEndpoint("Ombi", "/health"); //}); + services.AddMemoryCache(); services.AddLazyCache(); services.AddHttpClient();