2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
|
|
|
|
2023-04-04 22:53:01 -04:00
|
|
|
|
namespace Data.Helpers;
|
|
|
|
|
|
|
|
|
|
public class LookupCache<T> : ILookupCache<T> where T : class, IUniqueId
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
|
|
|
|
private Dictionary<long, T> _cachedItems;
|
|
|
|
|
private readonly SemaphoreSlim _onOperation = new(1, 1);
|
|
|
|
|
|
|
|
|
|
public LookupCache(ILogger<LookupCache<T>> logger, IDatabaseContextFactory contextFactory)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_contextFactory = contextFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<T> AddAsync(T item)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
await _onOperation.WaitAsync();
|
|
|
|
|
T existingItem = null;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-04-04 22:53:01 -04:00
|
|
|
|
if (_cachedItems.ContainsKey(item.Id))
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
existingItem = _cachedItems[item.Id];
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 22:53:01 -04:00
|
|
|
|
if (existingItem != null)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
_logger.LogDebug("Cached item already added for {Type} {Id} {Value}", typeof(T).Name, item.Id,
|
|
|
|
|
item.Value);
|
|
|
|
|
_onOperation.Release();
|
|
|
|
|
return existingItem;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-04-04 22:53:01 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("Adding new {Type} with {Id} {Value}", typeof(T).Name, item.Id, item.Value);
|
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
|
|
|
|
context.Set<T>().Add(item);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
_cachedItems.Add(item.Id, item);
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not add item to cache for {Type}", typeof(T).Name);
|
|
|
|
|
throw new Exception("Could not add item to cache");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onOperation.CurrentCount == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
_onOperation.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 22:53:01 -04:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-04-04 22:53:01 -04:00
|
|
|
|
public async Task<T> FirstAsync(Func<T, bool> query)
|
|
|
|
|
{
|
|
|
|
|
try
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
await _onOperation.WaitAsync();
|
2023-04-04 22:53:01 -04:00
|
|
|
|
var cachedResult = _cachedItems.Values.Where(query);
|
|
|
|
|
return cachedResult.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onOperation.CurrentCount == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
_onOperation.Release(1);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 22:53:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> GetAll()
|
|
|
|
|
{
|
|
|
|
|
return _cachedItems.Values;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-04-04 22:53:01 -04:00
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
_cachedItems = await context.Set<T>().ToDictionaryAsync(item => item.Id);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-04-04 22:53:01 -04:00
|
|
|
|
catch (Exception ex)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-04-04 22:53:01 -04:00
|
|
|
|
_logger.LogError(ex, "Could not initialize caching for {CacheType}", typeof(T).Name);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-28 10:35:01 -05:00
|
|
|
|
}
|