Fixed the discord notification test issue

pull/4274/head
tidusjar 4 years ago
parent 83ee2dae1a
commit 4118f0de41

@ -2,13 +2,14 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using LazyCache; using LazyCache;
using Microsoft.Extensions.Caching.Memory;
namespace Ombi.Helpers namespace Ombi.Helpers
{ {
public class CacheService : ICacheService public class CacheService : ICacheService
{ {
protected readonly IAppCache _memoryCache; protected readonly IMemoryCache _memoryCache;
public CacheService(IAppCache memoryCache) public CacheService(IMemoryCache memoryCache)
{ {
_memoryCache = memoryCache; _memoryCache = memoryCache;
} }
@ -20,7 +21,11 @@ namespace Ombi.Helpers
absoluteExpiration = DateTimeOffset.Now.AddHours(1); absoluteExpiration = DateTimeOffset.Now.AddHours(1);
} }
return await _memoryCache.GetOrAddAsync<T>(cacheKey, () => factory(), absoluteExpiration); return await _memoryCache.GetOrCreateAsync<T>(cacheKey, entry =>
{
entry.AbsoluteExpiration = absoluteExpiration;
return factory();
});
} }
public void Remove(string key) public void Remove(string key)
@ -31,7 +36,11 @@ namespace Ombi.Helpers
public T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTimeOffset absoluteExpiration) public T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTimeOffset absoluteExpiration)
{ {
// locks get and set internally // locks get and set internally
return _memoryCache.GetOrAdd<T>(cacheKey, () => factory(), absoluteExpiration); return _memoryCache.GetOrCreate<T>(cacheKey, entry =>
{
entry.AbsoluteExpiration = absoluteExpiration;
return factory();
});
} }
private static class TypeLock<T> private static class TypeLock<T>

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using LazyCache; using LazyCache;
using Microsoft.Extensions.Caching.Memory;
namespace Ombi.Helpers namespace Ombi.Helpers
{ {
@ -14,7 +15,7 @@ namespace Ombi.Helpers
{ {
private const string CacheKey = "MediaCacheServiceKeys"; 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 // Not in the cache, so add this Key into our MediaServiceCache
await UpdateLocalCache(cacheKey); await UpdateLocalCache(cacheKey);
return await _memoryCache.GetOrAddAsync<T>(cacheKey, () => factory(), absoluteExpiration); return await _memoryCache.GetOrCreateAsync<T>(cacheKey, entry =>
{
entry.AbsoluteExpiration = absoluteExpiration;
return factory();
});
} }
private async Task UpdateLocalCache(string cacheKey) private async Task UpdateLocalCache(string cacheKey)
{ {
var mediaServiceCache = await _memoryCache.GetAsync<List<string>>(CacheKey); var mediaServiceCache = _memoryCache.Get<List<string>>(CacheKey);
if (mediaServiceCache == null) if (mediaServiceCache == null)
{ {
mediaServiceCache = new List<string>(); mediaServiceCache = new List<string>();
} }
mediaServiceCache.Add(cacheKey); mediaServiceCache.Add(cacheKey);
_memoryCache.Remove(CacheKey); _memoryCache.Remove(CacheKey);
_memoryCache.Add(CacheKey, mediaServiceCache); _memoryCache.Set(CacheKey, mediaServiceCache);
} }
public async Task Purge() public async Task Purge()
{ {
var keys = await _memoryCache.GetAsync<List<string>>(CacheKey); var keys = _memoryCache.Get<List<string>>(CacheKey);
if (keys == null) if (keys == null)
{ {
return; return;

@ -84,6 +84,7 @@ namespace Ombi
//{ //{
// setup.AddHealthCheckEndpoint("Ombi", "/health"); // setup.AddHealthCheckEndpoint("Ombi", "/health");
//}); //});
services.AddMemoryCache();
services.AddLazyCache(); services.AddLazyCache();
services.AddHttpClient(); services.AddHttpClient();

Loading…
Cancel
Save