From f71924edc678defa2d508d7ca9643efb25bf8620 Mon Sep 17 00:00:00 2001 From: ilitirit Date: Tue, 2 May 2023 21:41:47 +0200 Subject: [PATCH] Cache json instead of complex object --- .../ExpedienceRepository.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Expedience.Infrastructure/ExpedienceRepository.cs b/Expedience.Infrastructure/ExpedienceRepository.cs index e8ada07..6ba3766 100644 --- a/Expedience.Infrastructure/ExpedienceRepository.cs +++ b/Expedience.Infrastructure/ExpedienceRepository.cs @@ -3,6 +3,7 @@ using Enyim.Caching; using Expedience.Infrastructure.Models; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; namespace Expedience.Infrastructure { @@ -25,12 +26,22 @@ namespace Expedience.Infrastructure public async Task> 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()); - return records; + 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()