From e64d0e151b49e793fa93ce4f4bc1e836f0b8f4bc Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 11 Jul 2025 10:04:29 +0330 Subject: [PATCH] adding expiration to memory cache --- GanjooRazor/Pages/Books.cshtml.cs | 3 ++- GanjooRazor/Pages/Contribs.cshtml.cs | 7 ++++--- GanjooRazor/Pages/Hashieha.cshtml.cs | 6 +++--- GanjooRazor/Pages/Index.cshtml.cs | 4 ++-- GanjooRazor/Pages/Search.cshtml.cs | 6 +++--- GanjooRazor/Pages/Simi.cshtml.cs | 6 +++--- GanjooRazor/Pages/Tagged.cshtml.cs | 6 +++--- RMuseum/Controllers/ArtifactController.cs | 14 +++++++------- RMuseum/Controllers/GanjoorController.cs | 18 +++++++++--------- .../Services/Implementation/GanjoorService.cs | 8 ++++---- .../Implementation/PictureFileService.cs | 2 +- .../Implementation/RecitationService.cs | 2 +- .../Implementation/SiteBannersService.cs | 4 ++-- TajikGanjoor/Pages/Index.cshtml.cs | 2 +- 14 files changed, 45 insertions(+), 43 deletions(-) diff --git a/GanjooRazor/Pages/Books.cshtml.cs b/GanjooRazor/Pages/Books.cshtml.cs index 150581fe..3489e5c8 100644 --- a/GanjooRazor/Pages/Books.cshtml.cs +++ b/GanjooRazor/Pages/Books.cshtml.cs @@ -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>(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poets); + _memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1)); } } catch diff --git a/GanjooRazor/Pages/Contribs.cshtml.cs b/GanjooRazor/Pages/Contribs.cshtml.cs index 5e8779ad..be4cfd89 100644 --- a/GanjooRazor/Pages/Contribs.cshtml.cs +++ b/GanjooRazor/Pages/Contribs.cshtml.cs @@ -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>(); 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(); 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(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poet); + _memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1)); } } return new OkObjectResult(poet); diff --git a/GanjooRazor/Pages/Hashieha.cshtml.cs b/GanjooRazor/Pages/Hashieha.cshtml.cs index dc108faa..b121091f 100644 --- a/GanjooRazor/Pages/Hashieha.cshtml.cs +++ b/GanjooRazor/Pages/Hashieha.cshtml.cs @@ -85,7 +85,7 @@ namespace GanjooRazor.Pages poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); 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(); 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(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poet); + _memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1)); } } return new OkObjectResult(poet); diff --git a/GanjooRazor/Pages/Index.cshtml.cs b/GanjooRazor/Pages/Index.cshtml.cs index 5d084faf..db222202 100644 --- a/GanjooRazor/Pages/Index.cshtml.cs +++ b/GanjooRazor/Pages/Index.cshtml.cs @@ -478,7 +478,7 @@ namespace GanjooRazor.Pages poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); 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(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poet); + _memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1)); } } return new OkObjectResult(poet); diff --git a/GanjooRazor/Pages/Search.cshtml.cs b/GanjooRazor/Pages/Search.cshtml.cs index 2d177cd7..8e469042 100644 --- a/GanjooRazor/Pages/Search.cshtml.cs +++ b/GanjooRazor/Pages/Search.cshtml.cs @@ -101,7 +101,7 @@ namespace GanjooRazor.Pages poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); 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(); 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(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poet); + _memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1)); } } return new OkObjectResult(poet); diff --git a/GanjooRazor/Pages/Simi.cshtml.cs b/GanjooRazor/Pages/Simi.cshtml.cs index 0e396b7b..5da6379a 100644 --- a/GanjooRazor/Pages/Simi.cshtml.cs +++ b/GanjooRazor/Pages/Simi.cshtml.cs @@ -109,7 +109,7 @@ namespace GanjooRazor.Pages poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); 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(); 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(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poet); + _memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1)); } } return new OkObjectResult(poet); diff --git a/GanjooRazor/Pages/Tagged.cshtml.cs b/GanjooRazor/Pages/Tagged.cshtml.cs index 7ac3071a..24679ee7 100644 --- a/GanjooRazor/Pages/Tagged.cshtml.cs +++ b/GanjooRazor/Pages/Tagged.cshtml.cs @@ -98,7 +98,7 @@ namespace GanjooRazor.Pages poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); 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(); 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(); if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poet); + _memoryCache.Set(cacheKey, poet, TimeSpan.FromHours(1)); } } return new OkObjectResult(poet); diff --git a/RMuseum/Controllers/ArtifactController.cs b/RMuseum/Controllers/ArtifactController.cs index de7b8ae3..ef244fb8 100644 --- a/RMuseum/Controllers/ArtifactController.cs +++ b/RMuseum/Controllers/ArtifactController.cs @@ -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)); } diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 329a9e22..0248a91b 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -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); diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 733ec7db..a47a9e68 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -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 @@ -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(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 diff --git a/RMuseum/Services/Implementation/PictureFileService.cs b/RMuseum/Services/Implementation/PictureFileService.cs index 6cfc3b30..5779ee74 100644 --- a/RMuseum/Services/Implementation/PictureFileService.cs +++ b/RMuseum/Services/Implementation/PictureFileService.cs @@ -162,7 +162,7 @@ namespace RMuseum.Services.Implementation .SingleOrDefaultAsync(); if (ImageInfoCacheEnabled) { - _memoryCache.Set(cachKey, pictureFile); + _memoryCache.Set(cachKey, pictureFile, TimeSpan.FromHours(1)); } } diff --git a/RMuseum/Services/Implementation/RecitationService.cs b/RMuseum/Services/Implementation/RecitationService.cs index a1b427bb..1b05ac38 100644 --- a/RMuseum/Services/Implementation/RecitationService.cs +++ b/RMuseum/Services/Implementation/RecitationService.cs @@ -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(narration); diff --git a/RMuseum/Services/Implementation/SiteBannersService.cs b/RMuseum/Services/Implementation/SiteBannersService.cs index 8debab2d..89c65c01 100644 --- a/RMuseum/Services/Implementation/SiteBannersService.cs +++ b/RMuseum/Services/Implementation/SiteBannersService.cs @@ -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(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(banner); diff --git a/TajikGanjoor/Pages/Index.cshtml.cs b/TajikGanjoor/Pages/Index.cshtml.cs index e1f62177..9c941f05 100644 --- a/TajikGanjoor/Pages/Index.cshtml.cs +++ b/TajikGanjoor/Pages/Index.cshtml.cs @@ -286,7 +286,7 @@ namespace TajikGanjoor.Pages if (AggressiveCacheEnabled) { - _memoryCache.Set(cacheKey, poets); + _memoryCache.Set(cacheKey, poets, TimeSpan.FromHours(1)); } } catch (Exception e)