Cache json instead of complex object

main
ilitirit 3 years ago
parent b74fc46fe2
commit f71924edc6

@ -3,6 +3,7 @@
using Enyim.Caching; using Enyim.Caching;
using Expedience.Infrastructure.Models; using Expedience.Infrastructure.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace Expedience.Infrastructure namespace Expedience.Infrastructure
{ {
@ -25,12 +26,22 @@ namespace Expedience.Infrastructure
public async Task<List<DutyCompletionRecord>> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken) public async Task<List<DutyCompletionRecord>> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken)
{ {
var cacheKey = $"xpd-dcr-{expac}"; var cacheKey = $"xpd-dcr-{expac}";
var cacheSeconds = 600;
var records = await _memcachedClient.GetValueOrCreateAsync(cacheKey, cacheSeconds,
async () => await _dbContext.DutyCompletionRecords.FromSqlInterpolated($"SELECT * FROM public.get_dutycompletionrecords({expac})")
.ToListAsync());
return records; if (_memcachedClient.TryGet<string>(cacheKey, out var cachedRecords))
{
return JsonConvert.DeserializeObject<List<DutyCompletionRecord>>(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() public void Dispose()

Loading…
Cancel
Save