forked from ddrilling/AsbCloudServer
replace CacheDb in TelemetryTracker
This commit is contained in:
parent
de6bbcfa72
commit
04414b3c75
44
AsbCloudInfrastructure/CacheExtentions.cs
Normal file
44
AsbCloudInfrastructure/CacheExtentions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
||||
#nullable enable
|
||||
public class TelemetryService : ITelemetryService
|
||||
{
|
||||
private const string telemetryCacheTag = "telemetryCache";
|
||||
private const string CacheTag = "TelemetryCache";
|
||||
private static readonly TimeSpan telemetryCacheObsolescence = TimeSpan.FromMinutes(5);
|
||||
|
||||
private readonly IAsbCloudDbContext db;
|
||||
@ -39,15 +39,15 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
||||
|
||||
private IEnumerable<Telemetry> GetTelemetryCache()
|
||||
{
|
||||
var cache = db.Telemetries
|
||||
var cache = db.Set<Telemetry>()
|
||||
.Include(t => t.Well)
|
||||
.FromCache(telemetryCacheTag, telemetryCacheObsolescence);
|
||||
.FromCache(CacheTag, telemetryCacheObsolescence);
|
||||
return cache;
|
||||
}
|
||||
|
||||
private void DropTelemetryCache()
|
||||
{
|
||||
db.Telemetries.DropCache(telemetryCacheTag);
|
||||
db.Telemetries.DropCache(CacheTag);
|
||||
}
|
||||
|
||||
public DateTime GetLastTelemetryDate(int idTelemetry)
|
||||
|
@ -3,6 +3,7 @@ using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Services.Cache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
@ -39,14 +40,14 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
||||
|
||||
private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats;
|
||||
|
||||
public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration)
|
||||
public TelemetryTracker(IConfiguration configuration, IMemoryCache memoryCache)
|
||||
{
|
||||
var contextOptions = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||
.Options;
|
||||
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());
|
||||
foreach (var telemetry in cacheTelemetry)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user