mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-20 05:13:18 -07:00
Added the Digital Release Date back in.
Improved performance and system load when loading content #4082
This commit is contained in:
parent
4082f6ad41
commit
0c4bd10b0f
25 changed files with 320 additions and 227 deletions
|
@ -1,48 +1,26 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Nito.AsyncEx;
|
||||
using LazyCache;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class CacheService : ICacheService
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
private readonly AsyncLock _mutex = new AsyncLock();
|
||||
public CacheService(IMemoryCache memoryCache)
|
||||
protected readonly IAppCache _memoryCache;
|
||||
public CacheService(IAppCache memoryCache)
|
||||
{
|
||||
_memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
public async Task<T> GetOrAdd<T>(string cacheKey, Func<Task<T>> factory, DateTime absoluteExpiration = default(DateTime), CancellationToken cancellationToken = default(CancellationToken))
|
||||
public virtual async Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> factory, DateTimeOffset absoluteExpiration = default)
|
||||
{
|
||||
if (absoluteExpiration == default(DateTime))
|
||||
if (absoluteExpiration == default)
|
||||
{
|
||||
absoluteExpiration = DateTime.Now.AddHours(1);
|
||||
}
|
||||
// locks get and set internally
|
||||
if (_memoryCache.TryGetValue<T>(cacheKey, out var result))
|
||||
{
|
||||
return result;
|
||||
absoluteExpiration = DateTimeOffset.Now.AddHours(1);
|
||||
}
|
||||
|
||||
if (_memoryCache.TryGetValue(cacheKey, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
|
||||
result = await factory();
|
||||
_memoryCache.Set(cacheKey, result, absoluteExpiration);
|
||||
|
||||
return result;
|
||||
return await _memoryCache.GetOrAddAsync<T>(cacheKey, () => factory(), absoluteExpiration);
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
|
@ -50,28 +28,10 @@ namespace Ombi.Helpers
|
|||
_memoryCache.Remove(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTime absoluteExpiration)
|
||||
public T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTimeOffset absoluteExpiration)
|
||||
{
|
||||
// locks get and set internally
|
||||
if (_memoryCache.TryGetValue<T>(cacheKey, out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
lock (TypeLock<T>.Lock)
|
||||
{
|
||||
if (_memoryCache.TryGetValue(cacheKey, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
result = factory();
|
||||
_memoryCache.Set(cacheKey, result, absoluteExpiration);
|
||||
|
||||
return result;
|
||||
}
|
||||
return _memoryCache.GetOrAdd<T>(cacheKey, () => factory(), absoluteExpiration);
|
||||
}
|
||||
|
||||
private static class TypeLock<T>
|
||||
|
|
|
@ -6,8 +6,8 @@ namespace Ombi.Helpers
|
|||
{
|
||||
public interface ICacheService
|
||||
{
|
||||
Task<T> GetOrAdd<T>(string cacheKey, Func<Task<T>> factory, DateTime absoluteExpiration = default(DateTime), CancellationToken cancellationToken = default(CancellationToken));
|
||||
T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTime absoluteExpiration);
|
||||
Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> factory, DateTimeOffset absoluteExpiration = default);
|
||||
T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTimeOffset absoluteExpiration);
|
||||
void Remove(string key);
|
||||
}
|
||||
}
|
61
src/Ombi.Helpers/MediaCacheService.cs
Normal file
61
src/Ombi.Helpers/MediaCacheService.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LazyCache;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public interface IMediaCacheService
|
||||
{
|
||||
Task<T> GetOrAddAsync<T>(string cacheKey, System.Func<Task<T>> factory, DateTimeOffset absoluteExpiration = default);
|
||||
Task Purge();
|
||||
}
|
||||
public class MediaCacheService : CacheService, IMediaCacheService
|
||||
{
|
||||
private const string CacheKey = "MediaCacheServiceKeys";
|
||||
|
||||
public MediaCacheService(IAppCache memoryCache) : base(memoryCache)
|
||||
{
|
||||
}
|
||||
|
||||
public async override Task<T> GetOrAddAsync<T>(string cacheKey, System.Func<Task<T>> factory, DateTimeOffset absoluteExpiration = default)
|
||||
{
|
||||
if (absoluteExpiration == default)
|
||||
{
|
||||
absoluteExpiration = DateTimeOffset.Now.AddHours(1);
|
||||
}
|
||||
|
||||
if (_memoryCache.TryGetValue<T>($"MediaCacheService_{cacheKey}", out var result))
|
||||
{
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
// Not in the cache, so add this Key into our MediaServiceCache
|
||||
await UpdateLocalCache(cacheKey);
|
||||
|
||||
return await _memoryCache.GetOrAddAsync<T>(cacheKey, () => factory(), absoluteExpiration);
|
||||
}
|
||||
|
||||
private async Task UpdateLocalCache(string cacheKey)
|
||||
{
|
||||
var mediaServiceCache = await _memoryCache.GetAsync<List<string>>(CacheKey);
|
||||
if (mediaServiceCache == null)
|
||||
{
|
||||
mediaServiceCache = new List<string>();
|
||||
}
|
||||
mediaServiceCache.Add(cacheKey);
|
||||
_memoryCache.Remove(CacheKey);
|
||||
_memoryCache.Add(CacheKey, mediaServiceCache);
|
||||
}
|
||||
|
||||
public async Task Purge()
|
||||
{
|
||||
var keys = await _memoryCache.GetAsync<List<string>>(CacheKey);
|
||||
foreach (var key in keys)
|
||||
{
|
||||
base.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EasyCrypto" Version="3.3.2" />
|
||||
<PackageReference Include="LazyCache.AspNetCore" Version="2.1.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue