From bc8f6e3a15bd71767d31c3d124a237d1443f7fa0 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 7 May 2021 12:55:34 +0430 Subject: [PATCH] caching poet by url --- RMuseum/Controllers/GanjoorController.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 5b67999a..6fe50f38 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -96,13 +96,19 @@ namespace RMuseum.Controllers [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPoetByUrl(string url) { - RServiceResult res = - await _ganjoorService.GetPoetByUrl(url); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - if (res.Result == null) - return NotFound(); - return Ok(res.Result); + var cacheKey = $"poet/byurl/{url}"; + if (!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetCompleteViewModel poet)) + { + RServiceResult res = + await _ganjoorService.GetPoetByUrl(url); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + if (res.Result == null) + return NotFound(); + poet = res.Result; + _memoryCache.Set(cacheKey, poet); + } + return Ok(poet); } ///