Improve Caching
This commit is contained in:
parent
c7cc39e914
commit
3c4f0dacb3
@ -110,17 +110,11 @@ namespace qtc_api.Controllers
|
||||
await _chatGWContext.Clients.All.SendAsync("RefreshContactsList");
|
||||
|
||||
// always try to overwrite cache when updating pfp
|
||||
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
||||
using(var stream = file.OpenReadStream())
|
||||
{
|
||||
using(var ms = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(ms);
|
||||
await _cache.SetImageAsync(recordId, ms.ToArray());
|
||||
ms.Dispose();
|
||||
}
|
||||
stream.Dispose();
|
||||
}
|
||||
string recordId = $"UserPfp_{userId}";
|
||||
using var stream = file.OpenReadStream();
|
||||
using var ms = new MemoryStream();
|
||||
stream.CopyTo(ms);
|
||||
await _cache.SetImageAsync(recordId, ms.ToArray(), TimeSpan.FromHours(1));
|
||||
|
||||
return Ok(response);
|
||||
} else
|
||||
@ -137,8 +131,8 @@ namespace qtc_api.Controllers
|
||||
[Authorize]
|
||||
public async Task<ActionResult> GetUserProfilePicture(string userId)
|
||||
{
|
||||
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
||||
byte[] pfpBytes = await _cache.GetImageAsync(recordId);
|
||||
string recordId = $"UserPfp_{userId}";
|
||||
byte[]? pfpBytes = await _cache.GetImageAsync(recordId);
|
||||
|
||||
var result = new ServiceResponse<FileContentResult>();
|
||||
if (pfpBytes == null)
|
||||
@ -147,7 +141,7 @@ namespace qtc_api.Controllers
|
||||
if (result != null && result.Success && result.Data != null)
|
||||
{
|
||||
pfpBytes = result.Data.FileContents;
|
||||
await _cache.SetImageAsync(recordId, pfpBytes);
|
||||
await _cache.SetImageAsync(recordId, pfpBytes, TimeSpan.FromHours(1));
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -155,7 +149,6 @@ namespace qtc_api.Controllers
|
||||
// explicitly set from cache
|
||||
result.Success = true;
|
||||
result.Data = new FileContentResult(pfpBytes, "image/jpeg");
|
||||
result.Message = $"{userId}.pfp";
|
||||
}
|
||||
|
||||
if (result != null && result.Success != false)
|
||||
|
||||
@ -5,23 +5,21 @@ namespace qtc_api.Extensions
|
||||
{
|
||||
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();
|
||||
|
||||
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromSeconds(15);
|
||||
options.SlidingExpiration = unusuedExpireTime;
|
||||
|
||||
var jsonData = JsonSerializer.Serialize(data);
|
||||
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();
|
||||
|
||||
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromMinutes(5);
|
||||
options.SlidingExpiration = unusuedExpireTime;
|
||||
options.AbsoluteExpirationRelativeToNow = absoluteExpireTime ?? TimeSpan.FromMinutes(30);
|
||||
|
||||
await cache.SetAsync(recordId, data, options);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user