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 untrusted user who does not match committer: 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 System;
using System.Collections.Generic;
@ -24,7 +23,6 @@ namespace AsbCloudInfrastructure
/// <param name="token"></param>
/// <returns></returns>
public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, IQueryable<T> query, CancellationToken token)
where T : class
{
var getter = async (CancellationToken token) =>
{
@ -35,19 +33,6 @@ namespace AsbCloudInfrastructure
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>
/// Создать кеш на основе результата выполнения произвольной асинхронной функции.
/// Ключ кеша - полное имя типа T.
@ -58,7 +43,6 @@ namespace AsbCloudInfrastructure
/// <param name="token"></param>
/// <returns></returns>
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 cache = memoryCache.GetOrCreateAsync(key, async (cacheEntry) => {
@ -79,20 +63,11 @@ namespace AsbCloudInfrastructure
/// <param name="query"></param>
/// <returns></returns>
public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, IQueryable<T> query)
where T : class
{
var getter = () => query.ToArray();
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>
/// Создать кеш на основе результата выполнения произвольной функции.
/// Ключ кеша - полное имя типа T.
@ -102,7 +77,6 @@ namespace AsbCloudInfrastructure
/// <param name="getter"></param>
/// <returns></returns>
public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, Func<IEnumerable<T>> getter)
where T : class
{
var key = typeof(T).FullName;
var cache = memoryCache.GetOrCreate(key, cacheEntry => {

View File

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

View File

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

View File

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

View File

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