Files
2026-04-23 19:15:16 +02:00

76 lines
2.3 KiB
C#

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;
}
/// <inheritdoc />
public async Task<bool> ReplaceMachineDataAsync(string idxMacchina, List<MachDataItem> 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;
});
}
/// <inheritdoc />
public async Task<MtcSetupModel> 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
);
});
}
}
}