Выделить отдельный Cached репозиторий для систем - источников данных

This commit is contained in:
Roman Efremov 2024-12-13 10:54:47 +05:00
parent 1d40edb535
commit ba75f2c888
4 changed files with 51 additions and 47 deletions

View File

@ -34,7 +34,7 @@ public static class DependencyInjection
services.AddTransient<ITimestampedSetRepository, TimestampedSetRepository>();
services.AddTransient<ITechMessagesRepository, TechMessagesRepository>();
services.AddTransient<IParameterRepository, ParameterRepository>();
services.AddTransient<IDataSourceSystemRepository, DataSourceSystemRepository>();
services.AddTransient<IDataSourceSystemRepository, DataSourceSystemCachedRepository>();
return services;
}

View File

@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Persistence.Models;
namespace Persistence.Repository.Repositories;
public class DataSourceSystemCachedRepository : DataSourceSystemRepository
{
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey";
private readonly IMemoryCache memoryCache;
private const int CacheExpirationInMinutes = 60;
public DataSourceSystemCachedRepository(DbContext db, IMemoryCache memoryCache) : base(db)
{
this.memoryCache = memoryCache;
}
public override async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token)
{
await base.Add(dataSourceSystemDto, token);
memoryCache.Remove(SystemCacheKey);
}
public override async Task<IEnumerable<DataSourceSystemDto>> Get(CancellationToken token)
{
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f =>
{
var dtos = await base.Get(token);
return dtos;
});
return systems ?? [];
}
}

View File

@ -14,40 +14,27 @@ using Persistence.Repositories;
namespace Persistence.Repository.Repositories;
public class DataSourceSystemRepository : IDataSourceSystemRepository
{
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey";
private DbContext db;
private readonly IMemoryCache memoryCache;
private const int CacheExpirationInMinutes = 60;
public DataSourceSystemRepository(DbContext db, IMemoryCache memoryCache)
protected DbContext db;
public DataSourceSystemRepository(DbContext db)
{
this.db = db;
this.memoryCache = memoryCache;
}
protected virtual IQueryable<DataSourceSystem> GetQueryReadOnly() => db.Set<DataSourceSystem>();
public async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token)
public virtual async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token)
{
var entity = dataSourceSystemDto.Adapt<DataSourceSystem>();
await db.Set<DataSourceSystem>().AddAsync(entity);
await db.SaveChangesAsync(token);
memoryCache.Remove(SystemCacheKey);
}
public async Task<IEnumerable<DataSourceSystemDto>> Get(CancellationToken token)
public virtual async Task<IEnumerable<DataSourceSystemDto>> Get(CancellationToken token)
{
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f =>
{
f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes);
var query = GetQueryReadOnly();
var entities = await query.ToListAsync(token);
var dtos = entities.Select(e => e.Adapt<DataSourceSystemDto>());
return dtos;
});
return systems ?? [];
}
}

View File

@ -12,15 +12,13 @@ namespace Persistence.Repository.Repositories
{
public class TechMessagesRepository : ITechMessagesRepository
{
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey";
private const int CacheExpirationInMinutes = 60;
private readonly IMemoryCache memoryCache;
private readonly IDataSourceSystemRepository sourceSystemRepository;
private DbContext db;
public TechMessagesRepository(DbContext db, IMemoryCache memoryCache)
public TechMessagesRepository(DbContext db, IDataSourceSystemRepository sourceSystemRepository)
{
this.memoryCache = memoryCache;
this.db = db;
this.sourceSystemRepository = sourceSystemRepository;
}
protected virtual IQueryable<TechMessage> GetQueryReadOnly() => db.Set<TechMessage>()
@ -116,16 +114,7 @@ namespace Persistence.Repository.Repositories
public async Task<IEnumerable<DataSourceSystemDto>> GetSystems(CancellationToken token)
{
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f =>
{
f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes);
var query = db.Set<Database.Entity.DataSourceSystem>();
var entities = await query.ToListAsync(token);
var dtos = entities.Select(e => e.Adapt<Models.DataSourceSystemDto>());
return dtos;
});
var systems = await sourceSystemRepository.Get(token);
return systems ?? [];
}
@ -151,7 +140,7 @@ namespace Persistence.Repository.Repositories
private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token)
{
var systems = await GetSystems(token);
var systems = await sourceSystemRepository.Get(token);
var system = systems?.FirstOrDefault(e => e.SystemId == systemId);
if (system == null)
@ -161,13 +150,7 @@ namespace Persistence.Repository.Repositories
SystemId = systemId,
Name = string.Empty
};
var entity = system.Adapt<DataSourceSystem>();
await db.Set<Database.Entity.DataSourceSystem>().AddAsync(entity);
await db.SaveChangesAsync(token);
memoryCache.Remove(SystemCacheKey);
await sourceSystemRepository.Add(system, token);
}
}
}