forked from ddrilling/AsbCloudServer
Changed CacheTable internal List to ConcurrentBag
This commit is contained in:
parent
ff1a75858b
commit
6a4b426aa2
@ -17,7 +17,10 @@ namespace AsbCloudDevOperations
|
||||
{
|
||||
var host = "http://localhost:5000";
|
||||
|
||||
var json = JsonSerializer.Serialize(new List<TelemetryDataSaubDto>());
|
||||
var json = JsonSerializer.Serialize(new List<TelemetryDataSaubDto>()
|
||||
{
|
||||
new TelemetryDataSaubDto()
|
||||
});
|
||||
|
||||
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
|
@ -8,7 +8,9 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
|
||||
public class CacheDb
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, (DateTime, IEnumerable<object>)> cache = new ConcurrentDictionary<string, (DateTime, IEnumerable<object>)>();
|
||||
private readonly ConcurrentDictionary<string, (DateTime, object)> cache =
|
||||
new ConcurrentDictionary<string, (DateTime, object)>();
|
||||
|
||||
private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15);
|
||||
|
||||
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context)
|
||||
@ -17,7 +19,7 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
var entityTypeName = typeof(TEntity).FullName;
|
||||
|
||||
if (!cache.ContainsKey(entityTypeName))
|
||||
cache[entityTypeName] = (DateTime.Now, new List<TEntity>(8));
|
||||
cache[entityTypeName] = (DateTime.Now, new ConcurrentBag<TEntity>());
|
||||
|
||||
bool isCachedDataObsolete = DateTime.Now - cache[entityTypeName].Item1 > obsolesenceTime;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -11,15 +12,15 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
where TEntity : class
|
||||
{
|
||||
private readonly DbContext context;
|
||||
private (DateTime refreshDate, IEnumerable<object> entities) data;
|
||||
private readonly List<TEntity> cached;
|
||||
private (DateTime refreshDate, object entities) data;
|
||||
private readonly ConcurrentBag<TEntity> cached;
|
||||
private readonly DbSet<TEntity> dbSet;
|
||||
|
||||
internal CacheTable(DbContext context, (DateTime refreshDate, IEnumerable<object> entities) data)
|
||||
internal CacheTable(DbContext context, (DateTime refreshDate, object entities) data)
|
||||
{
|
||||
this.context = context;
|
||||
this.data = data;
|
||||
this.cached = (List<TEntity>)data.entities;
|
||||
this.cached = (ConcurrentBag<TEntity>)data.entities;
|
||||
dbSet = context.Set<TEntity>();
|
||||
}
|
||||
|
||||
@ -27,41 +28,24 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
|
||||
public int Refresh()
|
||||
{
|
||||
if (cached.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
cached.Clear();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore
|
||||
// TODO: figure out what the hell
|
||||
}
|
||||
cached.Clear();
|
||||
|
||||
}
|
||||
var dbEntities = context.Set<TEntity>().AsNoTracking().ToList();
|
||||
cached.AddRange(dbEntities);
|
||||
foreach(var e in dbEntities)
|
||||
cached.Add(e);
|
||||
data.refreshDate = DateTime.Now;
|
||||
return cached.Count;
|
||||
}
|
||||
|
||||
public async Task<int> RefreshAsync(CancellationToken token = default)
|
||||
{
|
||||
if (cached.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
cached.Clear();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore
|
||||
// TODO: figure out what the well
|
||||
}
|
||||
}
|
||||
var dbEntities = await context.Set<TEntity>().AsNoTracking().ToListAsync(token).ConfigureAwait(false);
|
||||
cached.AddRange(dbEntities);
|
||||
cached.Clear();
|
||||
|
||||
var dbEntities = await context.Set<TEntity>().AsNoTracking()
|
||||
.ToListAsync(token).ConfigureAwait(false);
|
||||
|
||||
foreach (var e in dbEntities)
|
||||
cached.Add(e);
|
||||
data.refreshDate = DateTime.Now;
|
||||
return cached.Count;
|
||||
}
|
||||
@ -202,33 +186,40 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
return result;
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Mutate(Func<TEntity, bool> predicate, Action<TEntity> mutation)
|
||||
{
|
||||
var dbEntities = dbSet.Where(predicate);
|
||||
if (dbEntities.Any())
|
||||
{
|
||||
foreach (var dbEntity in dbEntities)
|
||||
mutation(dbEntity);
|
||||
context.SaveChanges();
|
||||
}
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
//public IEnumerable<TEntity> Mutate(Func<TEntity, bool> predicate,
|
||||
// Action<TEntity> mutation)
|
||||
//{
|
||||
// var dbEntities = dbSet.Where(predicate);
|
||||
// if (dbEntities.Any())
|
||||
// {
|
||||
// foreach (var dbEntity in dbEntities)
|
||||
// mutation(dbEntity);
|
||||
// context.SaveChanges();
|
||||
// }
|
||||
// var matchedByPredicate = cached.Select(el => predicate(el));
|
||||
// foreach (var item in matchedByPredicate)
|
||||
// cached.TryTake(out var t);
|
||||
// cached = cached.RemoveAll(e => predicate(e));
|
||||
// foreach (var e in dbEntities)
|
||||
// cached.Add(e);
|
||||
// return dbEntities;
|
||||
//}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> MutateAsync(Func<TEntity, bool> predicate, Action<TEntity> mutation, CancellationToken token = default)
|
||||
{
|
||||
var dbEntities = dbSet.Where(predicate);
|
||||
if (dbEntities.Any())
|
||||
{
|
||||
foreach (var dbEntity in dbEntities)
|
||||
mutation(dbEntity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
}
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
//public async Task<IEnumerable<TEntity>> MutateAsync(Func<TEntity,
|
||||
// bool> predicate, Action<TEntity> mutation, CancellationToken token = default)
|
||||
//{
|
||||
// var dbEntities = dbSet.Where(predicate);
|
||||
// if (dbEntities.Any())
|
||||
// {
|
||||
// foreach (var dbEntity in dbEntities)
|
||||
// mutation(dbEntity);
|
||||
// await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
// }
|
||||
// cached.RemoveAll(e => predicate(e));
|
||||
// foreach (var e in dbEntities)
|
||||
// cached.Add(e);
|
||||
// return dbEntities;
|
||||
//}
|
||||
|
||||
public TEntity Upsert(TEntity entity)
|
||||
{
|
||||
@ -286,34 +277,34 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
|
||||
public void Remove(Func<TEntity, bool> predicate)
|
||||
{
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
dbSet.RemoveRange(dbSet.Where(predicate));
|
||||
context.SaveChanges();
|
||||
Refresh();
|
||||
return;
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
|
||||
{
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
dbSet.RemoveRange(dbSet.Where(predicate));
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
await RefreshAsync(token).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
public TEntity Insert(TEntity entity)
|
||||
{
|
||||
var dbEntity = dbSet.Add(entity).Entity;
|
||||
var entry = dbSet.Add(entity);
|
||||
context.SaveChanges();
|
||||
cached.Add(dbEntity);
|
||||
return dbEntity;
|
||||
cached.Add(entry.Entity);
|
||||
return entry.Entity;
|
||||
}
|
||||
|
||||
public async Task<TEntity> InsertAsync(TEntity entity, CancellationToken token = default)
|
||||
{
|
||||
var dbEntity = dbSet.Add(entity).Entity;
|
||||
var entry = dbSet.Add(entity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
cached.Add(dbEntity);
|
||||
return dbEntity;
|
||||
cached.Add(entry.Entity);
|
||||
return entry.Entity;
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Insert(IEnumerable<TEntity> newEntities)
|
||||
@ -322,7 +313,8 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
foreach (var item in newEntities)
|
||||
dbEntities.Add(dbSet.Add(item).Entity);
|
||||
context.SaveChanges();
|
||||
cached.AddRange(dbEntities);
|
||||
foreach (var e in dbEntities)
|
||||
cached.Add(e);
|
||||
return dbEntities;
|
||||
}
|
||||
|
||||
@ -332,7 +324,8 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
foreach (var item in newEntities)
|
||||
dbEntities.Add(dbSet.Add(item).Entity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
cached.AddRange(dbEntities);
|
||||
foreach (var e in dbEntities)
|
||||
cached.Add(e);
|
||||
return dbEntities;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user