Cache json instead of complex object

main
ilitirit 3 years ago
parent b74fc46fe2
commit f71924edc6

@ -3,6 +3,7 @@
using Enyim.Caching;
using Expedience.Infrastructure.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace Expedience.Infrastructure
{
@ -25,13 +26,23 @@ namespace Expedience.Infrastructure
public async Task<List<DutyCompletionRecord>> GetDutyCompletionRecords(string expac, CancellationToken cancellationToken)
{
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());
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()
{

Loading…
Cancel
Save