feat: introduced caching in the server + Added serilog

This commit is contained in:
Mikhail Shabanov committed 2021-06-04 23:04:03 +02:00
1 parent 8c9770f8a0
commit 638489a842
25 files changed
+365 -33

No files matched your search

@@ -0,0 +1,17 @@
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
namespace WotBlitzStatisticsPro.Logic.Model
{
public class AccountDataCache
{
public AccountDataCache(AccountInfo accountInfo, AccountInfoHistory accountInfoHistory)
{
AccountInfo = accountInfo;
AccountInfoHistory = accountInfoHistory;
}
public AccountInfo AccountInfo { get; }
public AccountInfoHistory AccountInfoHistory { get; }
}
}
@@ -0,0 +1,28 @@
using System;
namespace WotBlitzStatisticsPro.Logic.Model
{
public class CacheItem<T>
{
private const int ExpirationTimeoutInMinutes = 60;
private DateTime? _lastRefreshed = null;
private T? _itemData = default(T);
public long AccountId { get; set; }
public bool IsExpired => _lastRefreshed != null &&
(DateTime.Now - _lastRefreshed.Value).TotalMinutes > ExpirationTimeoutInMinutes;
public T? Data
{
get => _itemData;
set
{
_itemData = value;
_lastRefreshed = DateTime.Now;
}
}
}
}
@@ -0,0 +1,84 @@
using System.Collections.Generic;
namespace WotBlitzStatisticsPro.Logic.Model
{
public class StatisticsCache
{
private readonly Dictionary<long, CacheItem<AccountDataCache>> _accountsCache = new();
private readonly Dictionary<long, CacheItem<TanksDataCache>> _tanksCache = new();
public AccountDataCache? GetAccountData(long accountId)
{
if (!_accountsCache.ContainsKey(accountId) || _accountsCache[accountId].IsExpired)
{
return null;
}
return _accountsCache[accountId].Data;
}
public TanksDataCache? GetTanksData(long accountId)
{
if (!_tanksCache.ContainsKey(accountId) || _tanksCache[accountId].IsExpired)
{
return null;
}
return _tanksCache[accountId].Data;
}
public void SetAccountData(long accountId, AccountDataCache data)
{
if (!_accountsCache.ContainsKey(accountId))
{
_accountsCache.Add(accountId, new CacheItem<AccountDataCache>());
ClearExpiredAccounts();
}
_accountsCache[accountId].Data = data;
}
public void SetTanksData(long accountId, TanksDataCache data)
{
if (!_tanksCache.ContainsKey(accountId))
{
_tanksCache.Add(accountId, new CacheItem<TanksDataCache>());
ClearExpiredTanks();
}
_tanksCache[accountId].Data = data;
}
private void ClearExpiredAccounts()
{
var expired = new List<long>();
foreach (var cacheItem in _accountsCache)
{
if (cacheItem.Value.IsExpired)
{
expired.Add(cacheItem.Key);
}
}
foreach (var expiredKey in expired)
{
_accountsCache.Remove(expiredKey);
}
}
private void ClearExpiredTanks()
{
var expired = new List<long>();
foreach (var cacheItem in _tanksCache)
{
if (cacheItem.Value.IsExpired)
{
expired.Add(cacheItem.Key);
}
}
foreach (var expiredKey in expired)
{
_accountsCache.Remove(expiredKey);
}
}
}
}
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
namespace WotBlitzStatisticsPro.Logic.Model
{
public class TanksDataCache
{
public TanksDataCache(List<TankInfo> tankInfo, Dictionary<long, TankInfoHistory> tankInfoHistory)
{
TankInfo = tankInfo;
TankInfoHistory = tankInfoHistory;
}
public List<TankInfo> TankInfo { get; }
public Dictionary<long, TankInfoHistory> TankInfoHistory { get; }
}
}