forked from ddrilling/AsbCloudServer
CS2-13: Добавлена потокобезопасность для данных в CaсheTable
This commit is contained in:
parent
7b00cedb47
commit
ae85cdedd5
@ -12,6 +12,7 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
private readonly DbContext context;
|
||||
private (DateTime refreshDate, IEnumerable<object> entities) data;
|
||||
private readonly List<TEntity> cached;
|
||||
private readonly ReaderWriterLockSlim cacheLocker = new ReaderWriterLockSlim();
|
||||
|
||||
internal CacheTable(DbContext context, (DateTime refreshDate, IEnumerable<object> entities) data)
|
||||
{
|
||||
@ -24,20 +25,36 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
|
||||
public int Refresh()
|
||||
{
|
||||
cached.Clear();
|
||||
var dbEntities = context.Set<TEntity>().ToList();
|
||||
cached.AddRange(dbEntities);
|
||||
data.refreshDate = DateTime.Now;
|
||||
return cached.Count;
|
||||
cacheLocker.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
cached.Clear();
|
||||
var dbEntities = context.Set<TEntity>().ToList();
|
||||
cached.AddRange(dbEntities);
|
||||
data.refreshDate = DateTime.Now;
|
||||
return cached.Count;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> RefreshAsync(CancellationToken token = default)
|
||||
{
|
||||
cached.Clear();
|
||||
var dbEntities = await context.Set<TEntity>().ToListAsync(token).ConfigureAwait(false);
|
||||
cached.AddRange(dbEntities);
|
||||
data.refreshDate = DateTime.Now;
|
||||
return cached.Count;
|
||||
cacheLocker.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
cached.Clear();
|
||||
var dbEntities = await context.Set<TEntity>().ToListAsync(token).ConfigureAwait(false);
|
||||
cached.AddRange(dbEntities);
|
||||
data.refreshDate = DateTime.Now;
|
||||
return cached.Count;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
private bool CheckRefresh(RefreshMode refreshMode)
|
||||
@ -184,193 +201,300 @@ namespace AsbCloudInfrastructure.Services.Cache
|
||||
|
||||
public IEnumerable<TEntity> Update(Func<TEntity, bool> predicate, Action<TEntity> mutation)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntities = dbSet.Where(predicate);
|
||||
if (dbEntities.Any())
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var dbEntity in dbEntities)
|
||||
mutation(dbEntity);
|
||||
context.SaveChanges();
|
||||
var dbSet = context.Set<TEntity>();
|
||||
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;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> UpdateAsync(Func<TEntity, bool> predicate, Action<TEntity> mutation, CancellationToken token = default)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntities = dbSet.Where(predicate);
|
||||
if (dbEntities.Any())
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var dbEntity in dbEntities)
|
||||
mutation(dbEntity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
var dbSet = context.Set<TEntity>();
|
||||
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;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
|
||||
public TEntity Upsert(TEntity entity)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
if (cached.Contains(entity))
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
var dbSet = context.Set<TEntity>();
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
if (cached.Contains(entity))
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
cached.Add(entityEntry.Entity);
|
||||
|
||||
return entityEntry.Entity;
|
||||
}
|
||||
else
|
||||
finally
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
cached.Add(entityEntry.Entity);
|
||||
|
||||
return entityEntry.Entity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<TEntity> UpsertAsync(TEntity entity, CancellationToken token = default)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
if (cached.Contains(entity))
|
||||
try
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
var dbSet = context.Set<TEntity>();
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
|
||||
if (cached.Contains(entity))
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
|
||||
cached.Add(entityEntry.Entity);
|
||||
|
||||
return entityEntry.Entity;
|
||||
}
|
||||
else
|
||||
finally
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
|
||||
cached.Add(entityEntry.Entity);
|
||||
|
||||
return entityEntry.Entity;
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Upsert(IEnumerable<TEntity> entities)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
var upsertedEntries = new List<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity>>(entities.Count());
|
||||
foreach (var entity in entities)
|
||||
try
|
||||
{
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
if (cached.Contains(entity))
|
||||
var dbSet = context.Set<TEntity>();
|
||||
|
||||
var upsertedEntries = new List<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity>>(entities.Count());
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
if (cached.Contains(entity))
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
}
|
||||
upsertedEntries.Add(entityEntry);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
}
|
||||
upsertedEntries.Add(entityEntry);
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
var upserted = upsertedEntries.Select(e => e.Entity);
|
||||
|
||||
cached.AddRange(upserted);
|
||||
|
||||
return upserted;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
var upserted = upsertedEntries.Select(e => e.Entity);
|
||||
|
||||
cached.AddRange(upserted);
|
||||
|
||||
return upserted;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> UpsertAsync(IEnumerable<TEntity> entities, CancellationToken token = default)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
var upsertedEntries = new List<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity>>(entities.Count());
|
||||
foreach (var entity in entities)
|
||||
try
|
||||
{
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
if (cached.Contains(entity))
|
||||
var dbSet = context.Set<TEntity>();
|
||||
|
||||
var upsertedEntries = new List<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity>>(entities.Count());
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> entityEntry;
|
||||
if (cached.Contains(entity))
|
||||
{
|
||||
entityEntry = dbSet.Update(entity);
|
||||
cached.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
}
|
||||
upsertedEntries.Add(entityEntry);
|
||||
}
|
||||
else
|
||||
{
|
||||
entityEntry = dbSet.Add(entity);
|
||||
}
|
||||
upsertedEntries.Add(entityEntry);
|
||||
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
|
||||
var upserted = upsertedEntries.Select(e => e.Entity);
|
||||
|
||||
cached.AddRange(upserted);
|
||||
|
||||
return upserted;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
|
||||
var upserted = upsertedEntries.Select(e => e.Entity);
|
||||
|
||||
cached.AddRange(upserted);
|
||||
|
||||
return upserted;
|
||||
}
|
||||
public void Remove(Func<TEntity, bool> predicate)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
dbSet.RemoveRange(dbSet.Where(predicate));
|
||||
context.SaveChanges();
|
||||
return;
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
dbSet.RemoveRange(dbSet.Where(predicate));
|
||||
context.SaveChanges();
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
dbSet.RemoveRange(dbSet.Where(predicate));
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
return;
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
cached.RemoveAll(e => predicate(e));
|
||||
dbSet.RemoveRange(dbSet.Where(predicate));
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public TEntity Insert(TEntity entity)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntity = dbSet.Add(entity).Entity;
|
||||
context.SaveChanges();
|
||||
cached.Add(dbEntity);
|
||||
return dbEntity;
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntity = dbSet.Add(entity).Entity;
|
||||
context.SaveChanges();
|
||||
cached.Add(dbEntity);
|
||||
return dbEntity;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEntity> InsertAsync(TEntity entity, CancellationToken token = default)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntity = dbSet.Add(entity).Entity;
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
cached.Add(dbEntity);
|
||||
return dbEntity;
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntity = dbSet.Add(entity).Entity;
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
cached.Add(dbEntity);
|
||||
return dbEntity;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Insert(IEnumerable<TEntity> newEntities)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntities = new List<TEntity>(newEntities.Count());
|
||||
foreach (var item in newEntities)
|
||||
dbEntities.Add(dbSet.Add(item).Entity);
|
||||
context.SaveChanges();
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntities = new List<TEntity>(newEntities.Count());
|
||||
foreach (var item in newEntities)
|
||||
dbEntities.Add(dbSet.Add(item).Entity);
|
||||
context.SaveChanges();
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> InsertAsync(IEnumerable<TEntity> newEntities, CancellationToken token = default)
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntities = new List<TEntity>(newEntities.Count());
|
||||
foreach (var item in newEntities)
|
||||
dbEntities.Add(dbSet.Add(item).Entity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
cacheLocker.EnterWriteLock();
|
||||
|
||||
try
|
||||
{
|
||||
var dbSet = context.Set<TEntity>();
|
||||
var dbEntities = new List<TEntity>(newEntities.Count());
|
||||
foreach (var item in newEntities)
|
||||
dbEntities.Add(dbSet.Add(item).Entity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
cached.AddRange(dbEntities);
|
||||
return dbEntities;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cacheLocker.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user