DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Cache/CacheDb.cs
Фролов cdfcb0b2f7 semaphore
2021-10-03 20:08:17 +05:00

40 lines
1.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.Cache
{
public class CacheDb
{
private readonly Dictionary<string, (DateTime, IEnumerable)> cache =
new Dictionary<string, (DateTime, IEnumerable)>();
private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15);
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context)
where TEntity : class
{
var entityTypeName = typeof(TEntity).FullName;
if (!cache.ContainsKey(entityTypeName))
cache[entityTypeName] = (DateTime.Now, new List<TEntity>());
bool isCachedDataObsolete = DateTime.Now - cache[entityTypeName].Item1 > obsolesenceTime;
var tableCache = new CacheTable<TEntity>(context, cache[entityTypeName]);
if (isCachedDataObsolete)
tableCache.Refresh();
return tableCache;
}
public void DropAll() => cache.Clear();
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
}
}