mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
Add max concurrent players over 24 hours badge to home
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Data.Abstractions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -19,7 +20,7 @@ namespace Data.Helpers
|
||||
public string Key { get; set; }
|
||||
public DateTime LastRetrieval { get; set; }
|
||||
public TimeSpan ExpirationTime { get; set; }
|
||||
public Func<DbSet<T>, Task<V>> Getter { get; set; }
|
||||
public Func<DbSet<T>, CancellationToken, Task<V>> Getter { get; set; }
|
||||
public V Value { get; set; }
|
||||
public bool IsExpired => (DateTime.Now - LastRetrieval.Add(ExpirationTime)).TotalSeconds > 0;
|
||||
}
|
||||
@ -30,14 +31,14 @@ namespace Data.Helpers
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public void SetCacheItem(Func<DbSet<T>, Task<V>> getter, string key, TimeSpan? expirationTime = null)
|
||||
public void SetCacheItem(Func<DbSet<T>, CancellationToken, Task<V>> getter, string key, TimeSpan? expirationTime = null)
|
||||
{
|
||||
if (_cacheStates.ContainsKey(key))
|
||||
{
|
||||
_logger.LogDebug("Cache key {key} is already added", key);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var state = new CacheState()
|
||||
{
|
||||
Key = key,
|
||||
@ -48,7 +49,7 @@ namespace Data.Helpers
|
||||
_cacheStates.Add(key, state);
|
||||
}
|
||||
|
||||
public async Task<V> GetCacheItem(string keyName)
|
||||
public async Task<V> GetCacheItem(string keyName, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!_cacheStates.ContainsKey(keyName))
|
||||
{
|
||||
@ -59,19 +60,19 @@ namespace Data.Helpers
|
||||
|
||||
if (state.IsExpired)
|
||||
{
|
||||
await RunCacheUpdate(state);
|
||||
await RunCacheUpdate(state, cancellationToken);
|
||||
}
|
||||
|
||||
return state.Value;
|
||||
}
|
||||
|
||||
private async Task RunCacheUpdate(CacheState state)
|
||||
private async Task RunCacheUpdate(CacheState state, CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = _contextFactory.CreateContext(false);
|
||||
var set = context.Set<T>();
|
||||
var value = await state.Getter(set);
|
||||
var value = await state.Getter(set, token);
|
||||
state.Value = value;
|
||||
state.LastRetrieval = DateTime.Now;
|
||||
}
|
||||
|
Reference in New Issue
Block a user