mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-31 03:50:08 -07:00
Add the Issue Reporting functionality (#1811)
* Added issuesreporting and the ability to add categories to the UI * Added lazy loading!
This commit is contained in:
parent
438f56eceb
commit
246f1c07cf
109 changed files with 2905 additions and 526 deletions
80
src/Ombi.Helpers/CacheService.cs
Normal file
80
src/Ombi.Helpers/CacheService.cs
Normal file
|
@ -0,0 +1,80 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Nito.AsyncEx;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class CacheService : ICacheService
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
private readonly AsyncLock _mutex = new AsyncLock();
|
||||
public CacheService(IMemoryCache memoryCache)
|
||||
{
|
||||
_memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
|
||||
}
|
||||
|
||||
public async Task<T> GetOrAdd<T>(string cacheKey, Func<Task<T>> factory, DateTime absoluteExpiration = default(DateTime))
|
||||
{
|
||||
if (absoluteExpiration == default(DateTime))
|
||||
{
|
||||
absoluteExpiration = DateTime.Now.AddHours(1);
|
||||
}
|
||||
// locks get and set internally
|
||||
if (_memoryCache.TryGetValue<T>(cacheKey, out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
using (await _mutex.LockAsync())
|
||||
{
|
||||
if (_memoryCache.TryGetValue(cacheKey, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
result = await factory();
|
||||
_memoryCache.Set(cacheKey, result, absoluteExpiration);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
_memoryCache.Remove(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTime 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;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TypeLock<T>
|
||||
{
|
||||
public static object Lock { get; } = new object();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue