improve threading synchronization for date lookup cache

This commit is contained in:
RaidMax 2023-04-04 21:53:01 -05:00
parent 5f5fb8230e
commit 5f5c0f1cfb
2 changed files with 98 additions and 97 deletions

View File

@ -68,10 +68,15 @@ namespace Data.Helpers
foreach (var id in ids)
{
if (_cacheStates[key].ContainsKey(id))
var cacheInstance = _cacheStates[key];
lock (_cacheStates)
{
if (cacheInstance.ContainsKey(id))
{
continue;
}
}
var state = new CacheState<TReturnType>
{
@ -80,11 +85,13 @@ namespace Data.Helpers
ExpirationTime = expirationTime ?? TimeSpan.FromMinutes(DefaultExpireMinutes)
};
_cacheStates[key].Add(id, state);
lock (_cacheStates)
{
cacheInstance.Add(id, state);
}
_autoRefresh = autoRefresh;
if (!_autoRefresh || expirationTime == TimeSpan.MaxValue)
{
return;
@ -96,8 +103,8 @@ namespace Data.Helpers
}
}
public async Task<TReturnType> GetCacheItem(string keyName, CancellationToken cancellationToken = default) =>
await GetCacheItem(keyName, null, cancellationToken);
public Task<TReturnType> GetCacheItem(string keyName, CancellationToken cancellationToken = default) =>
GetCacheItem(keyName, null, cancellationToken);
public async Task<TReturnType> GetCacheItem(string keyName, object id = null,
CancellationToken cancellationToken = default)
@ -107,7 +114,14 @@ namespace Data.Helpers
throw new ArgumentException("No cache found for key {key}", keyName);
}
var state = id is null ? _cacheStates[keyName].Values.First() : _cacheStates[keyName][id];
var cacheInstance = _cacheStates[keyName];
CacheState<TReturnType> state;
lock (_cacheStates)
{
state = id is null ? cacheInstance.Values.First() : _cacheStates[keyName][id];
}
// when auto refresh is off we want to check the expiration and value
// when auto refresh is on, we want to only check the value, because it'll be refreshed automatically

View File

@ -8,14 +8,14 @@ using System.Threading;
using System.Threading.Tasks;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Data.Helpers
namespace Data.Helpers;
public class LookupCache<T> : ILookupCache<T> where T : class, IUniqueId
{
public class LookupCache<T> : ILookupCache<T> where T : class, IUniqueId
{
private readonly ILogger _logger;
private readonly IDatabaseContextFactory _contextFactory;
private Dictionary<long, T> _cachedItems;
private readonly SemaphoreSlim _onOperation = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _onOperation = new(1, 1);
public LookupCache(ILogger<LookupCache<T>> logger, IDatabaseContextFactory contextFactory)
{
@ -35,7 +35,7 @@ namespace Data.Helpers
if (existingItem != null)
{
_logger.LogDebug("Cached item already added for {type} {id} {value}", typeof(T).Name, item.Id,
_logger.LogDebug("Cached item already added for {Type} {Id} {Value}", typeof(T).Name, item.Id,
item.Value);
_onOperation.Release();
return existingItem;
@ -43,7 +43,7 @@ namespace Data.Helpers
try
{
_logger.LogDebug("Adding new {type} with {id} {value}", typeof(T).Name, item.Id, item.Value);
_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();
@ -52,7 +52,7 @@ namespace Data.Helpers
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not add item to cache for {type}", typeof(T).Name);
_logger.LogError(ex, "Could not add item to cache for {Type}", typeof(T).Name);
throw new Exception("Could not add item to cache");
}
finally
@ -66,22 +66,12 @@ namespace Data.Helpers
public async Task<T> FirstAsync(Func<T, bool> query)
{
await _onOperation.WaitAsync();
try
{
await _onOperation.WaitAsync();
var cachedResult = _cachedItems.Values.Where(query);
if (cachedResult.Any())
{
return cachedResult.FirstOrDefault();
}
}
catch
{
}
finally
{
if (_onOperation.CurrentCount == 0)
@ -89,8 +79,6 @@ namespace Data.Helpers
_onOperation.Release(1);
}
}
return null;
}
public IEnumerable<T> GetAll()
@ -107,8 +95,7 @@ namespace Data.Helpers
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not initialize caching for {cacheType}", typeof(T).Name);
}
_logger.LogError(ex, "Could not initialize caching for {CacheType}", typeof(T).Name);
}
}
}