From 3bd6c7d0fb2ac1f5afa9d38c00301c832ddb5e10 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 22 Feb 2023 09:40:02 +0500 Subject: [PATCH] MemoryCacheExtentions delete GetOrCreateBasic* based on DbContext --- .../MemoryCacheExtentions.cs | 28 +------------------ .../Repository/WellOperationRepository.cs | 4 +-- .../Services/SAUB/TelemetryService.cs | 11 +++----- .../OperationsStatService.cs | 6 ++-- .../Services/WellService.cs | 8 ++---- 5 files changed, 13 insertions(+), 44 deletions(-) diff --git a/AsbCloudInfrastructure/MemoryCacheExtentions.cs b/AsbCloudInfrastructure/MemoryCacheExtentions.cs index f227781f..eb4a591a 100644 --- a/AsbCloudInfrastructure/MemoryCacheExtentions.cs +++ b/AsbCloudInfrastructure/MemoryCacheExtentions.cs @@ -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 /// /// public static Task> GetOrCreateBasicAsync(this IMemoryCache memoryCache, IQueryable 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(this IMemoryCache memoryCache, IQueryable query, CancellationToken token)")] - public static Task> GetOrCreateBasicAsync(this IMemoryCache memoryCache, IAsbCloudDbContext dbContext, CancellationToken token) - where T : class - { - var getter = async (CancellationToken token) => - { - var entities = await dbContext.Set() - .ToArrayAsync(token); - return entities.AsEnumerable(); - }; - return memoryCache.GetOrCreateBasicAsync(getter, token); - } - /// /// Создать кеш на основе результата выполнения произвольной асинхронной функции. /// Ключ кеша - полное имя типа T. @@ -58,7 +43,6 @@ namespace AsbCloudInfrastructure /// /// public static Task> GetOrCreateBasicAsync(this IMemoryCache memoryCache, Func>> getterAsync, CancellationToken token) - where T : class { var key = typeof(T).FullName; var cache = memoryCache.GetOrCreateAsync(key, async (cacheEntry) => { @@ -79,20 +63,11 @@ namespace AsbCloudInfrastructure /// /// public static IEnumerable GetOrCreateBasic(this IMemoryCache memoryCache, IQueryable query) - where T : class { var getter = () => query.ToArray(); return memoryCache.GetOrCreateBasic(getter); } - [Obsolete(message: "use GetOrCreateBasic(this IMemoryCache memoryCache, IQueryable query)")] - public static IEnumerable GetOrCreateBasic(this IMemoryCache memoryCache, IAsbCloudDbContext dbContext) - where T : class - { - var getter = () => dbContext.Set().ToArray(); - return memoryCache.GetOrCreateBasic(getter); - } - /// /// Создать кеш на основе результата выполнения произвольной функции. /// Ключ кеша - полное имя типа T. @@ -102,7 +77,6 @@ namespace AsbCloudInfrastructure /// /// public static IEnumerable GetOrCreateBasic(this IMemoryCache memoryCache, Func> getter) - where T : class { var key = typeof(T).FullName; var cache = memoryCache.GetOrCreate(key, cacheEntry => { diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index db45b357..1b5f3406 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -37,7 +37,7 @@ namespace AsbCloudInfrastructure.Repository public IEnumerable GetCategories(bool includeParents) { var categories = memoryCache - .GetOrCreateBasic(db); + .GetOrCreateBasic(db.Set()); if (!includeParents) { @@ -60,7 +60,7 @@ namespace AsbCloudInfrastructure.Repository /// public IDictionary GetSectionTypes() => memoryCache - .GetOrCreateBasic(db) + .GetOrCreateBasic(db.Set()) .ToDictionary(s => s.Id, s => s.Caption); public async Task> GetOperationsPlanAsync(int idWell, CancellationToken token) diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs index fd74ad3e..1c4c7e5b 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs @@ -38,13 +38,10 @@ namespace AsbCloudInfrastructure.Services.SAUB } private IEnumerable GetTelemetryCache() - { - var getter = () => db.Set() - .Include(t => t.Well) - .ToArray(); - return memoryCache.GetOrCreateBasic(getter); - } - + => memoryCache.GetOrCreateBasic( + db.Set() + .Include(t => t.Well)); + private void DropTelemetryCache() { memoryCache.DropBasic(); diff --git a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs index 900d5f5d..a360a014 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs @@ -41,7 +41,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService var statsWells = await GetWellsStatAsync(wells, token).ConfigureAwait(false); var cluster = (await memoryCache - .GetOrCreateBasicAsync(db, token)) + .GetOrCreateBasicAsync(db.Set(), token)) .FirstOrDefault(c => c.Id == idCluster); var statClusterDto = new StatClusterDto { @@ -132,7 +132,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService private async Task CalcWellStatAsync(Well well, CancellationToken token = default) { var wellType = (await memoryCache - .GetOrCreateBasicAsync(db, token)) + .GetOrCreateBasicAsync(db.Set(), token)) .FirstOrDefault(t => t.Id == well.IdWellType); var statWellDto = new StatWellDto { @@ -169,7 +169,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService .Distinct(); var sectionTypes = memoryCache - .GetOrCreateBasic(db) + .GetOrCreateBasic(db.Set()) .Where(s => sectionTypeIds.Contains(s.Id)) .ToDictionary(s => s.Id); diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 39374cf1..eea16706 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -47,13 +47,11 @@ namespace AsbCloudInfrastructure.Services private Task> GetCacheRelationCompanyWellAsync(CancellationToken token) { - var getter = async (CancellationToken token) - => (await dbContext.Set() + return memoryCache.GetOrCreateBasicAsync( + dbContext.Set() .Include(r => r.Company) .Include(r => r.Well) - .ToArrayAsync(token)) - .AsEnumerable(); - return memoryCache.GetOrCreateBasicAsync(getter, token); + , token); } private void DropCacheRelationCompanyWell()