using Microsoft.Extensions.Configuration; using MP.Core.Objects; using MP.Data.DbModels.Mtc; using MP.Data.Repository.Mtc; using StackExchange.Redis; using System.Collections.Generic; using System.Threading.Tasks; namespace MP.Data.Services.Mtc { // A lightweight service that mirrors the old Mongo based archive public class MtcSetupService : BaseServ, IMtcSetupService { private readonly string _className; private readonly IMtcSetupRepository _repo; public MtcSetupService( IConfiguration config, IConnectionMultiplexer redis, IMtcSetupRepository repo) : base(config, redis) { _className = "MtcSetup"; _repo = repo; } /// public async Task ReplaceMachineDataAsync(string idxMacchina, List items) { return await TraceAsync($"{_className}.ReplaceMachineData", async (activity) => { // cerso sul DB var currRec = await _repo.GetByIdxAsync(idxMacchina); var upsRec = new MtcSetupModel { IdxMacchina = idxMacchina, MtcDataItems = items }; string operation = "UPDATE"; bool success = false; if (currRec != null) { success = await _repo.UpdateAsync(upsRec); } else { operation = "INSERT"; success = await _repo.InsertAsync(upsRec); } activity?.SetTag("db.operation", operation); await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); return success; }); } /// public async Task GetMachineDataAsync(string idxMacchina) { return await TraceAsync($"{_className}.GetMachineData", async (activity) => { string cacheKey = $"{_redisBaseKey}:{_className}:MachData:{idxMacchina}"; return await GetOrSetCacheAsync( cacheKey, async () => await _repo.GetByIdxAsync(idxMacchina), LongCache ); }); } } }