From c18a8616e5b3fce11cd11d81ad872cd27255d2c8 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 7 May 2021 12:52:10 +0430 Subject: [PATCH] caching poet by id --- RMuseum/Controllers/GanjoorController.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 637de613..5b67999a 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -68,13 +68,19 @@ namespace RMuseum.Controllers [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPoetById(int id) { - RServiceResult res = + var cacheKey = $"poet/byid/{id}"; + if (!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetCompleteViewModel poet)) + { + RServiceResult res = await _ganjoorService.GetPoetById(id); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - if (res.Result == null) - return NotFound(); - return Ok(res.Result); + 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); } ///