MemoryCacheExtentions delete GetOrCreateBasic* based on DbContext

This commit is contained in:
ngfrolov 2023-02-22 09:40:02 +05:00
parent 539d04d3cf
commit 3bd6c7d0fb
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
5 changed files with 13 additions and 44 deletions

View File

@ -1,5 +1,4 @@
using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -24,7 +23,6 @@ namespace AsbCloudInfrastructure
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, IQueryable<T> query, CancellationToken token) public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, IQueryable<T> query, CancellationToken token)
where T : class
{ {
var getter = async (CancellationToken token) => var getter = async (CancellationToken token) =>
{ {
@ -35,19 +33,6 @@ namespace AsbCloudInfrastructure
return memoryCache.GetOrCreateBasicAsync(getter, token); return memoryCache.GetOrCreateBasicAsync(getter, token);
} }
[Obsolete(message: "use GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, IQueryable<T> query, CancellationToken token)")]
public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, IAsbCloudDbContext dbContext, CancellationToken token)
where T : class
{
var getter = async (CancellationToken token) =>
{
var entities = await dbContext.Set<T>()
.ToArrayAsync(token);
return entities.AsEnumerable();
};
return memoryCache.GetOrCreateBasicAsync(getter, token);
}
/// <summary> /// <summary>
/// Создать кеш на основе результата выполнения произвольной асинхронной функции. /// Создать кеш на основе результата выполнения произвольной асинхронной функции.
/// Ключ кеша - полное имя типа T. /// Ключ кеша - полное имя типа T.
@ -58,7 +43,6 @@ namespace AsbCloudInfrastructure
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, Func<CancellationToken, Task<IEnumerable<T>>> getterAsync, CancellationToken token) public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, Func<CancellationToken, Task<IEnumerable<T>>> getterAsync, CancellationToken token)
where T : class
{ {
var key = typeof(T).FullName; var key = typeof(T).FullName;
var cache = memoryCache.GetOrCreateAsync(key, async (cacheEntry) => { var cache = memoryCache.GetOrCreateAsync(key, async (cacheEntry) => {
@ -79,20 +63,11 @@ namespace AsbCloudInfrastructure
/// <param name="query"></param> /// <param name="query"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, IQueryable<T> query) public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, IQueryable<T> query)
where T : class
{ {
var getter = () => query.ToArray(); var getter = () => query.ToArray();
return memoryCache.GetOrCreateBasic(getter); return memoryCache.GetOrCreateBasic(getter);
} }
[Obsolete(message: "use GetOrCreateBasic<T>(this IMemoryCache memoryCache, IQueryable<T> query)")]
public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, IAsbCloudDbContext dbContext)
where T : class
{
var getter = () => dbContext.Set<T>().ToArray();
return memoryCache.GetOrCreateBasic(getter);
}
/// <summary> /// <summary>
/// Создать кеш на основе результата выполнения произвольной функции. /// Создать кеш на основе результата выполнения произвольной функции.
/// Ключ кеша - полное имя типа T. /// Ключ кеша - полное имя типа T.
@ -102,7 +77,6 @@ namespace AsbCloudInfrastructure
/// <param name="getter"></param> /// <param name="getter"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, Func<IEnumerable<T>> getter) public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, Func<IEnumerable<T>> getter)
where T : class
{ {
var key = typeof(T).FullName; var key = typeof(T).FullName;
var cache = memoryCache.GetOrCreate(key, cacheEntry => { var cache = memoryCache.GetOrCreate(key, cacheEntry => {

View File

@ -37,7 +37,7 @@ namespace AsbCloudInfrastructure.Repository
public IEnumerable<WellOperationCategoryDto> GetCategories(bool includeParents) public IEnumerable<WellOperationCategoryDto> GetCategories(bool includeParents)
{ {
var categories = memoryCache var categories = memoryCache
.GetOrCreateBasic<WellOperationCategory>(db); .GetOrCreateBasic(db.Set<WellOperationCategory>());
if (!includeParents) if (!includeParents)
{ {
@ -60,7 +60,7 @@ namespace AsbCloudInfrastructure.Repository
/// <inheritdoc/> /// <inheritdoc/>
public IDictionary<int, string> GetSectionTypes() public IDictionary<int, string> GetSectionTypes()
=> memoryCache => memoryCache
.GetOrCreateBasic<WellSectionType>(db) .GetOrCreateBasic(db.Set<WellSectionType>())
.ToDictionary(s => s.Id, s => s.Caption); .ToDictionary(s => s.Id, s => s.Caption);
public async Task<IEnumerable<WellOperationDto>> GetOperationsPlanAsync(int idWell, CancellationToken token) public async Task<IEnumerable<WellOperationDto>> GetOperationsPlanAsync(int idWell, CancellationToken token)

View File

@ -38,13 +38,10 @@ namespace AsbCloudInfrastructure.Services.SAUB
} }
private IEnumerable<Telemetry> GetTelemetryCache() private IEnumerable<Telemetry> GetTelemetryCache()
{ => memoryCache.GetOrCreateBasic(
var getter = () => db.Set<Telemetry>() db.Set<Telemetry>()
.Include(t => t.Well) .Include(t => t.Well));
.ToArray();
return memoryCache.GetOrCreateBasic(getter);
}
private void DropTelemetryCache() private void DropTelemetryCache()
{ {
memoryCache.DropBasic<Telemetry>(); memoryCache.DropBasic<Telemetry>();

View File

@ -41,7 +41,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
var statsWells = await GetWellsStatAsync(wells, token).ConfigureAwait(false); var statsWells = await GetWellsStatAsync(wells, token).ConfigureAwait(false);
var cluster = (await memoryCache var cluster = (await memoryCache
.GetOrCreateBasicAsync<Cluster>(db, token)) .GetOrCreateBasicAsync(db.Set<Cluster>(), token))
.FirstOrDefault(c => c.Id == idCluster); .FirstOrDefault(c => c.Id == idCluster);
var statClusterDto = new StatClusterDto var statClusterDto = new StatClusterDto
{ {
@ -132,7 +132,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
private async Task<StatWellDto> CalcWellStatAsync(Well well, CancellationToken token = default) private async Task<StatWellDto> CalcWellStatAsync(Well well, CancellationToken token = default)
{ {
var wellType = (await memoryCache var wellType = (await memoryCache
.GetOrCreateBasicAsync<WellType>(db, token)) .GetOrCreateBasicAsync(db.Set<WellType>(), token))
.FirstOrDefault(t => t.Id == well.IdWellType); .FirstOrDefault(t => t.Id == well.IdWellType);
var statWellDto = new StatWellDto var statWellDto = new StatWellDto
{ {
@ -169,7 +169,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
.Distinct(); .Distinct();
var sectionTypes = memoryCache var sectionTypes = memoryCache
.GetOrCreateBasic<WellSectionType>(db) .GetOrCreateBasic(db.Set<WellSectionType>())
.Where(s => sectionTypeIds.Contains(s.Id)) .Where(s => sectionTypeIds.Contains(s.Id))
.ToDictionary(s => s.Id); .ToDictionary(s => s.Id);

View File

@ -47,13 +47,11 @@ namespace AsbCloudInfrastructure.Services
private Task<IEnumerable<RelationCompanyWell>> GetCacheRelationCompanyWellAsync(CancellationToken token) private Task<IEnumerable<RelationCompanyWell>> GetCacheRelationCompanyWellAsync(CancellationToken token)
{ {
var getter = async (CancellationToken token) return memoryCache.GetOrCreateBasicAsync(
=> (await dbContext.Set<RelationCompanyWell>() dbContext.Set<RelationCompanyWell>()
.Include(r => r.Company) .Include(r => r.Company)
.Include(r => r.Well) .Include(r => r.Well)
.ToArrayAsync(token)) , token);
.AsEnumerable();
return memoryCache.GetOrCreateBasicAsync(getter, token);
} }
private void DropCacheRelationCompanyWell() private void DropCacheRelationCompanyWell()