From 59eeb8c24f466a915bb3637a7099e604a329c5d8 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 6 May 2021 17:16:00 +0430 Subject: [PATCH] caching for poets --- RMuseum/Controllers/ArtifactController.cs | 3 +-- RMuseum/Controllers/GanjoorController.cs | 27 ++++++++++++++++++----- RMuseum/RMuseum.xml | 11 ++++++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/RMuseum/Controllers/ArtifactController.cs b/RMuseum/Controllers/ArtifactController.cs index 8f3f43f6..01a21f8b 100644 --- a/RMuseum/Controllers/ArtifactController.cs +++ b/RMuseum/Controllers/ArtifactController.cs @@ -1813,8 +1813,7 @@ namespace RMuseum.Controllers /// /// /// - /// - public ArtifactController(IArtifactService artifactService, IUserPermissionChecker userPermissionChecker, IMemoryCache memoryCache, ICaptchaService captchaService, IRNotificationService notificationService) + public ArtifactController(IArtifactService artifactService, IUserPermissionChecker userPermissionChecker, IMemoryCache memoryCache, ICaptchaService captchaService) { _artifactService = artifactService; _userPermissionChecker = userPermissionChecker; diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index ee500cc6..45dac5b3 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -1,13 +1,13 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Auth.ViewModel; using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Models.GanjoorAudio.ViewModels; -using RMuseum.Models.GanjoorIntegration.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Auth.Memory; using RSecurityBackend.Models.Auth.ViewModels; @@ -40,11 +40,19 @@ namespace RMuseum.Controllers [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoets(bool websitePoets = true, bool includeBio = true) { - RServiceResult res = + var cacheKey = $"ganjoor/poets/{websitePoets}/{includeBio}"; + if (!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetViewModel[] poets)) + { + RServiceResult res = await _ganjoorService.GetPoets(websitePoets, includeBio); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - return Ok(res.Result); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + + poets = res.Result; + _memoryCache.Set(cacheKey, poets); + + } + return Ok(poets); } /// @@ -1144,6 +1152,11 @@ namespace RMuseum.Controllers /// protected readonly IImageFileService _imageFileService; + /// + /// IMemoryCache + /// + protected readonly IMemoryCache _memoryCache; + /// /// constructor /// @@ -1151,12 +1164,14 @@ namespace RMuseum.Controllers /// /// /// - public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor, IImageFileService imageFileService) + /// + public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor, IImageFileService imageFileService, IMemoryCache memoryCache) { _ganjoorService = ganjoorService; _appUserService = appUserService; _httpContextAccessor = httpContextAccessor; _imageFileService = imageFileService; + _memoryCache = memoryCache; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index b7031b59..dd20f33a 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -572,7 +572,7 @@ - + constructor @@ -581,7 +581,6 @@ - @@ -943,7 +942,12 @@ Image Service - + + + IMemoryCache + + + constructor @@ -951,6 +955,7 @@ +