using System; using Enyim.Caching; using Expedience.Infrastructure.Models; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace Expedience.Infrastructure { public interface IExpedienceRepository : IDisposable { Task> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken); } public class ExpedienceRepository : IExpedienceRepository { private readonly ExpedienceContext _dbContext; private readonly IMemcachedClient _memcachedClient; public ExpedienceRepository(ExpedienceContext dbContext, IMemcachedClient memcachedClient) { _dbContext = dbContext; _memcachedClient = memcachedClient; } public async Task> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken) { var cacheKey = $"xpd-dcr-{expac}"; if (_memcachedClient.TryGet(cacheKey, out var cachedRecords)) { return JsonConvert.DeserializeObject>(cachedRecords); } else { var records = await _dbContext.DutyCompletionRecords.FromSqlInterpolated($"SELECT * FROM public.get_dutycompletionrecords({expac})") .ToListAsync(); var json = JsonConvert.SerializeObject(records); await _memcachedClient.AddAsync(cacheKey, json, TimeSpan.FromMinutes(10)); return records; } } public void Dispose() { _memcachedClient.Dispose(); } } }