Improve Caching

This commit is contained in:
Alan Moon 2025-11-14 17:03:43 -08:00
parent c7cc39e914
commit 3c4f0dacb3
2 changed files with 11 additions and 20 deletions

View File

@ -110,17 +110,11 @@ namespace qtc_api.Controllers
await _chatGWContext.Clients.All.SendAsync("RefreshContactsList"); await _chatGWContext.Clients.All.SendAsync("RefreshContactsList");
// always try to overwrite cache when updating pfp // always try to overwrite cache when updating pfp
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}"; string recordId = $"UserPfp_{userId}";
using(var stream = file.OpenReadStream()) using var stream = file.OpenReadStream();
{ using var ms = new MemoryStream();
using(var ms = new MemoryStream())
{
stream.CopyTo(ms); stream.CopyTo(ms);
await _cache.SetImageAsync(recordId, ms.ToArray()); await _cache.SetImageAsync(recordId, ms.ToArray(), TimeSpan.FromHours(1));
ms.Dispose();
}
stream.Dispose();
}
return Ok(response); return Ok(response);
} else } else
@ -137,8 +131,8 @@ namespace qtc_api.Controllers
[Authorize] [Authorize]
public async Task<ActionResult> GetUserProfilePicture(string userId) public async Task<ActionResult> GetUserProfilePicture(string userId)
{ {
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}"; string recordId = $"UserPfp_{userId}";
byte[] pfpBytes = await _cache.GetImageAsync(recordId); byte[]? pfpBytes = await _cache.GetImageAsync(recordId);
var result = new ServiceResponse<FileContentResult>(); var result = new ServiceResponse<FileContentResult>();
if (pfpBytes == null) if (pfpBytes == null)
@ -147,7 +141,7 @@ namespace qtc_api.Controllers
if (result != null && result.Success && result.Data != null) if (result != null && result.Success && result.Data != null)
{ {
pfpBytes = result.Data.FileContents; pfpBytes = result.Data.FileContents;
await _cache.SetImageAsync(recordId, pfpBytes); await _cache.SetImageAsync(recordId, pfpBytes, TimeSpan.FromHours(1));
} }
} }
else else
@ -155,7 +149,6 @@ namespace qtc_api.Controllers
// explicitly set from cache // explicitly set from cache
result.Success = true; result.Success = true;
result.Data = new FileContentResult(pfpBytes, "image/jpeg"); result.Data = new FileContentResult(pfpBytes, "image/jpeg");
result.Message = $"{userId}.pfp";
} }
if (result != null && result.Success != false) if (result != null && result.Success != false)

View File

@ -5,23 +5,21 @@ namespace qtc_api.Extensions
{ {
public static class DistributedCacheExtensions public static class DistributedCacheExtensions
{ {
public static async Task SetRecordAsync<T> (this IDistributedCache cache, string recordId, T data, TimeSpan? absoluteExpireTime = null, TimeSpan? unusuedExpireTime = null) public static async Task SetRecordAsync<T> (this IDistributedCache cache, string recordId, T data, TimeSpan? absoluteExpireTime = null)
{ {
var options = new DistributedCacheEntryOptions(); var options = new DistributedCacheEntryOptions();
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromSeconds(15); options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromSeconds(15);
options.SlidingExpiration = unusuedExpireTime;
var jsonData = JsonSerializer.Serialize(data); var jsonData = JsonSerializer.Serialize(data);
await cache.SetStringAsync(recordId, jsonData, options); await cache.SetStringAsync(recordId, jsonData, options);
} }
public static async Task SetImageAsync(this IDistributedCache cache, string recordId, byte[] data, TimeSpan? absoluteExpireTime = null, TimeSpan? unusuedExpireTime = null) public static async Task SetImageAsync(this IDistributedCache cache, string recordId, byte[] data, TimeSpan? absoluteExpireTime = null)
{ {
var options = new DistributedCacheEntryOptions(); var options = new DistributedCacheEntryOptions();
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromMinutes(5); options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromMinutes(30);
options.SlidingExpiration = unusuedExpireTime;
await cache.SetAsync(recordId, data, options); await cache.SetAsync(recordId, data, options);
} }