adding expiration to memory cache

This commit is contained in:
Hamid Reza Mohammadi 2025-07-11 10:04:29 +03:30
parent c6c61f720b
commit e64d0e151b
14 changed files with 45 additions and 43 deletions

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using System;
namespace GanjooRazor.Pages
{
@ -55,7 +56,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
catch

View File

@ -14,6 +14,7 @@ using RSecurityBackend.Models.Generic;
using System.Linq;
using static Betalgo.Ranul.OpenAI.ObjectModels.RealtimeModels.RealtimeEventTypes;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
namespace GanjooRazor.Pages
{
@ -59,7 +60,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
@ -81,7 +82,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
@ -104,7 +105,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
return new OkObjectResult(poet);

View File

@ -85,7 +85,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
@ -107,7 +107,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
@ -134,7 +134,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
return new OkObjectResult(poet);

View File

@ -478,7 +478,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
catch
@ -508,7 +508,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
return new OkObjectResult(poet);

View File

@ -101,7 +101,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
@ -123,7 +123,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
@ -146,7 +146,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
return new OkObjectResult(poet);

View File

@ -109,7 +109,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
@ -133,7 +133,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
@ -178,7 +178,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
return new OkObjectResult(poet);

View File

@ -98,7 +98,7 @@ namespace GanjooRazor.Pages
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
@ -122,7 +122,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
@ -145,7 +145,7 @@ namespace GanjooRazor.Pages
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
}
return new OkObjectResult(poet);

View File

@ -85,7 +85,7 @@ namespace RMuseum.Controllers
return BadRequest(itemsInfo.ExceptionString);
}
_memoryCache.Set(cacheKey, itemsInfo);
_memoryCache.Set(cacheKey, itemsInfo, TimeSpan.FromHours(1));
}
if (itemsInfo.Result.Items.Count() > 0)
@ -251,7 +251,7 @@ namespace RMuseum.Controllers
if (itemInfo.Result == null)
return NotFound();
_memoryCache.Set(cacheKey, itemInfo);
_memoryCache.Set(cacheKey, itemInfo, TimeSpan.FromHours(1));
}
@ -291,7 +291,7 @@ namespace RMuseum.Controllers
if (itemInfo.Result == null)
return NotFound();
_memoryCache.Set(cacheKey, itemInfo);
_memoryCache.Set(cacheKey, itemInfo, TimeSpan.FromHours(1));
}
@ -393,7 +393,7 @@ namespace RMuseum.Controllers
if (itemInfo.Result == null)
return NotFound();
_memoryCache.Set(cacheKey, itemInfo);
_memoryCache.Set(cacheKey, itemInfo, TimeSpan.FromHours(1));
}
Response.GetTypedHeaders().LastModified = itemInfo.Result.LastModified;
@ -442,7 +442,7 @@ namespace RMuseum.Controllers
if (itemInfo.Result == null)
return NotFound();
_memoryCache.Set(cacheKey, itemInfo);
_memoryCache.Set(cacheKey, itemInfo, TimeSpan.FromHours(1));
}
}
if (itemInfo == null)
@ -505,7 +505,7 @@ namespace RMuseum.Controllers
if (itemInfo.Result == null)
return NotFound();
_memoryCache.Set(cacheKey, itemInfo);
_memoryCache.Set(cacheKey, itemInfo, TimeSpan.FromHours(1));
}
}
if (itemInfo == null)
@ -701,7 +701,7 @@ namespace RMuseum.Controllers
if (tag.Result == null)
return NotFound();
_memoryCache.Set(cacheKey, tag);
_memoryCache.Set(cacheKey, tag, TimeSpan.FromHours(1));
}

View File

@ -51,7 +51,7 @@ namespace RMuseum.Controllers
poets = res.Result;
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
return Ok(poets);
@ -79,7 +79,7 @@ namespace RMuseum.Controllers
centuries = res.Result;
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, centuries);
_memoryCache.Set(cacheKey, centuries, TimeSpan.FromHours(1));
}
return Ok(centuries);
@ -129,7 +129,7 @@ namespace RMuseum.Controllers
return NotFound();
poet = res.Result;
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
return Ok(poet);
}
@ -158,7 +158,7 @@ namespace RMuseum.Controllers
return NotFound();
poet = res.Result;
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, poet);
_memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1));
}
return Ok(poet);
}
@ -332,8 +332,8 @@ namespace RMuseum.Controllers
imagePath = imgPath.Result;
_memoryCache.Set(cacheKey, imagePath);
_memoryCache.Set(cacheKeyForLastModified, lastModified);
_memoryCache.Set(cacheKey, imagePath, TimeSpan.FromHours(1));
_memoryCache.Set(cacheKeyForLastModified, lastModified, TimeSpan.FromHours(1));
}
@ -427,7 +427,7 @@ namespace RMuseum.Controllers
cat = res.Result;
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, cat);
_memoryCache.Set(cacheKey, cat, TimeSpan.FromHours(1));
}
}
return Ok(cat);
@ -459,7 +459,7 @@ namespace RMuseum.Controllers
return NotFound();
cat = res.Result;
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, cat);
_memoryCache.Set(cacheKey, cat, TimeSpan.FromHours(1));
}
@ -4500,7 +4500,7 @@ namespace RMuseum.Controllers
poets = res.Result;
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
return Ok(poets);

View File

@ -83,7 +83,7 @@ namespace RMuseum.Services.Implementation
res.Sort((a, b) => fa.Compare(a.Nickname, b.Nickname));
poets = res.ToArray();
if (AggressiveCacheEnabled)
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
return new RServiceResult<GanjoorPoetViewModel[]>
@ -111,7 +111,7 @@ namespace RMuseum.Services.Implementation
poetCat = (await GetCatById(cat.Id, catPoems, false, true)).Result;
if (poetCat != null && AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poetCat);
_memoryCache.Set(cacheKey, poetCat, TimeSpan.FromHours(1));
}
}
return new RServiceResult<GanjoorPoetCompleteViewModel>(poetCat);
@ -713,7 +713,7 @@ namespace RMuseum.Services.Implementation
}
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cachKey, page);
_memoryCache.Set(cachKey, page, TimeSpan.FromHours(1));
}
}
@ -1963,7 +1963,7 @@ namespace RMuseum.Services.Implementation
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cachKey, poemViewModel);
_memoryCache.Set(cachKey, poemViewModel, TimeSpan.FromHours(1));
}
}
return new RServiceResult<GanjoorPoemCompleteViewModel>

View File

@ -162,7 +162,7 @@ namespace RMuseum.Services.Implementation
.SingleOrDefaultAsync();
if (ImageInfoCacheEnabled)
{
_memoryCache.Set(cachKey, pictureFile);
_memoryCache.Set(cachKey, pictureFile, TimeSpan.FromHours(1));
}
}

View File

@ -434,7 +434,7 @@ namespace RMuseum.Services.Implementationa
select new RecitationViewModel(audio, audio.Owner, poem, "");
narration = await source.SingleOrDefaultAsync();
_memoryCache.Set(cachKey, narration);
_memoryCache.Set(cachKey, narration, TimeSpan.FromHours(1));
}
return new RServiceResult<RecitationViewModel>(narration);

View File

@ -161,7 +161,7 @@ namespace RMuseum.Services.Implementation
if (!_memoryCache.TryGetValue(_cacheKeyForIdSet, out int[] idSet))
{
idSet = await _context.GanjoorSiteBanners.Where(b => b.Active == true).Select(b => b.Id).ToArrayAsync();
_memoryCache.Set(_cacheKeyForIdSet, idSet);
_memoryCache.Set(_cacheKeyForIdSet, idSet, TimeSpan.FromHours(1));
}
if (idSet.Length == 0)
return new RServiceResult<GanjoorSiteBannerViewModel>(null);//no active banner
@ -185,7 +185,7 @@ namespace RMuseum.Services.Implementation
})
.AsNoTracking()
.SingleAsync();
_memoryCache.Set(cachKey, banner);
_memoryCache.Set(cachKey, banner, TimeSpan.FromHours(1));
}
return new RServiceResult<GanjoorSiteBannerViewModel>(banner);

View File

@ -286,7 +286,7 @@ namespace TajikGanjoor.Pages
if (AggressiveCacheEnabled)
{
_memoryCache.Set(cacheKey, poets);
_memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1));
}
}
catch (Exception e)