replace CacheDb in TelemetryTracker

This commit is contained in:
ngfrolov 2022-11-18 15:07:27 +05:00
parent de6bbcfa72
commit 04414b3c75
3 changed files with 51 additions and 6 deletions

View File

@ -0,0 +1,44 @@
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure
{
public static class MemoryCacheExtentions
{
private static readonly TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
public static Task<IEnumerable<T>> GetOrCreateBasicAsync<T>(this IMemoryCache memoryCache, IAsbCloudDbContext dbContext, CancellationToken token)
where T : class
{
var cacheTag = typeof(T).FullName;
var cache = memoryCache.GetOrCreateAsync(cacheTag, async (cacheEntry) => {
cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence;
cacheEntry.SlidingExpiration = CacheOlescence;
var entities = await dbContext.Set<T>().ToArrayAsync(token);
return entities.AsEnumerable();
});
return cache;
}
public static IEnumerable<T> GetOrCreateBasic<T>(this IMemoryCache memoryCache, IAsbCloudDbContext dbContext)
where T : class
{
var cacheTag = typeof(T).FullName;
var cache = memoryCache.GetOrCreate(cacheTag, cacheEntry => {
cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence;
cacheEntry.SlidingExpiration = CacheOlescence;
var entities = dbContext.Set<T>().ToArray();
return entities.AsEnumerable();
});
return cache;
}
}
}

View File

@ -17,7 +17,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
#nullable enable #nullable enable
public class TelemetryService : ITelemetryService public class TelemetryService : ITelemetryService
{ {
private const string telemetryCacheTag = "telemetryCache"; private const string CacheTag = "TelemetryCache";
private static readonly TimeSpan telemetryCacheObsolescence = TimeSpan.FromMinutes(5); private static readonly TimeSpan telemetryCacheObsolescence = TimeSpan.FromMinutes(5);
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
@ -39,15 +39,15 @@ namespace AsbCloudInfrastructure.Services.SAUB
private IEnumerable<Telemetry> GetTelemetryCache() private IEnumerable<Telemetry> GetTelemetryCache()
{ {
var cache = db.Telemetries var cache = db.Set<Telemetry>()
.Include(t => t.Well) .Include(t => t.Well)
.FromCache(telemetryCacheTag, telemetryCacheObsolescence); .FromCache(CacheTag, telemetryCacheObsolescence);
return cache; return cache;
} }
private void DropTelemetryCache() private void DropTelemetryCache()
{ {
db.Telemetries.DropCache(telemetryCacheTag); db.Telemetries.DropCache(CacheTag);
} }
public DateTime GetLastTelemetryDate(int idTelemetry) public DateTime GetLastTelemetryDate(int idTelemetry)

View File

@ -3,6 +3,7 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.Cache;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
@ -39,14 +40,14 @@ namespace AsbCloudInfrastructure.Services.SAUB
private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats; private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats;
public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration) public TelemetryTracker(IConfiguration configuration, IMemoryCache memoryCache)
{ {
var contextOptions = new DbContextOptionsBuilder<AsbCloudDbContext>() var contextOptions = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql(configuration.GetConnectionString("DefaultConnection")) .UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
.Options; .Options;
var db = new AsbCloudDbContext(contextOptions); var db = new AsbCloudDbContext(contextOptions);
var cacheTelemetry = cacheDb.GetCachedTable<Telemetry>(db); var cacheTelemetry = memoryCache.GetOrCreateBasic<Telemetry>(db);
var keyValuePairs = new Dictionary<string, TrackerStat>(cacheTelemetry.Count()); var keyValuePairs = new Dictionary<string, TrackerStat>(cacheTelemetry.Count());
foreach (var telemetry in cacheTelemetry) foreach (var telemetry in cacheTelemetry)
{ {