From 124aab416cddfbf8fc539617398168e0b5455723 Mon Sep 17 00:00:00 2001 From: Fernando Date: Mon, 23 Sep 2019 14:42:17 -0300 Subject: [PATCH 1/3] Add cache --- Controllers/EditionsController.cs | 83 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/Controllers/EditionsController.cs b/Controllers/EditionsController.cs index c2ede15..5a3f23b 100644 --- a/Controllers/EditionsController.cs +++ b/Controllers/EditionsController.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; using vopen_api.Models; using vopen_api.Repositories; @@ -14,10 +16,16 @@ public class EditionsController : ControllerBase private readonly EditionsRepository editionsRepository; private readonly LegacyGlobalRepository legacyGlobalRepository; - public EditionsController(EditionsRepository editionsRepository, LegacyGlobalRepository legacyGlobalRepository) + private readonly ILogger logger; + private readonly CacheService cacheService; + + public EditionsController(EditionsRepository editionsRepository, LegacyGlobalRepository legacyGlobalRepository, ILogger logger, IMemoryCache memoryCache) { this.editionsRepository = editionsRepository; this.legacyGlobalRepository = legacyGlobalRepository; + + this.logger = logger; + this.cacheService = new CacheService(memoryCache); } // GET api/v1/editions @@ -32,12 +40,21 @@ public async Task> GetAllAsync() [HttpGet("{id}")] public async Task GetByIdAsync(string id) { + if (id == "vopen-global-legacy") - { - return Ok(this.legacyGlobalRepository.GetEdition(id)); + { + return Ok(GetEditionFromLegacy(id)); } - var language = this.GetLanguage(); + var language = this.GetLanguage(); + string cacheKey = $"Editions-GetByIdAsync-{id}-{language}"; + var cacheResult = this.cacheService.GetValue(cacheKey); + + if (cacheResult != null) { + this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache"); + return Ok(cacheResult); + } + var result = await this.editionsRepository.GetByLanguageAndId(language, id); if (result == null) @@ -45,49 +62,27 @@ public async Task GetByIdAsync(string id) return NotFound(); } + this.cacheService.SetValue(cacheKey, cacheResult); return Ok(result); } - // POST api/v1/edition - //[HttpPost] - //public async Task Post([FromBody] EditionDTO newEdition) - //{ - // var result = await this.editionsRepository.Create(newEdition); - - // if (result == null) - // { - // return null; - // } - - // return CreatedAtAction(nameof(GetByIdAsync), new { id = newEdition.Id }, newEdition); - //} - - //// PUT api/v1/edition/5 - //[HttpPut("{id}")] - //public async Task Put(String id, [FromBody] EditionDTO updatedEdition) - //{ - // if (id != updatedEdition.Id) - // { - // return BadRequest(); - // } - - // var result = await this.editionsRepository.Update(updatedEdition); - - // if (result == null) - // { - // return NotFound(); - // } - - // return Ok(result); - //} - - //// DELETE api/v1/event/5 - //[HttpDelete("{id}")] - //public async Task Delete(string id) - //{ - // await this.editionsRepository.Delete(id); - // return NoContent(); - //} + private EditionDTO GetEditionFromLegacy(string id) { + + string cacheKey = $"Editions-GetByIdAsync-legacy-{id}"; + var cacheResult = this.cacheService.GetValue(cacheKey); + + if (cacheResult != null) + { + this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache"); + } + else + { + cacheResult = this.legacyGlobalRepository.GetEdition(id); + this.cacheService.SetValue(cacheKey, cacheResult); + } + + return cacheResult; + } private string GetLanguage() { From 641c631231592c95c1dfb1fed24159c9754ae7c5 Mon Sep 17 00:00:00 2001 From: Fernando Sonego Date: Tue, 24 Sep 2019 12:12:54 -0300 Subject: [PATCH 2/3] Editions: Cache GetByIdAsync --- Controllers/EditionsController.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Controllers/EditionsController.cs b/Controllers/EditionsController.cs index 5a3f23b..8c1cf19 100644 --- a/Controllers/EditionsController.cs +++ b/Controllers/EditionsController.cs @@ -47,7 +47,7 @@ public async Task GetByIdAsync(string id) } var language = this.GetLanguage(); - string cacheKey = $"Editions-GetByIdAsync-{id}-{language}"; + var cacheKey = $"Editions-GetByIdAsync-{id}-{language}"; var cacheResult = this.cacheService.GetValue(cacheKey); if (cacheResult != null) { @@ -62,25 +62,23 @@ public async Task GetByIdAsync(string id) return NotFound(); } - this.cacheService.SetValue(cacheKey, cacheResult); + this.cacheService.SetValue(cacheKey, result); return Ok(result); } private EditionDTO GetEditionFromLegacy(string id) { - string cacheKey = $"Editions-GetByIdAsync-legacy-{id}"; + var cacheKey = $"Editions-GetByIdAsync-legacy-{id}"; var cacheResult = this.cacheService.GetValue(cacheKey); if (cacheResult != null) { this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache"); + return cacheResult; } - else - { - cacheResult = this.legacyGlobalRepository.GetEdition(id); - this.cacheService.SetValue(cacheKey, cacheResult); - } - + + cacheResult = this.legacyGlobalRepository.GetEdition(id); + this.cacheService.SetValue(cacheKey, cacheResult); return cacheResult; } From c871af801f75ed718e11221697011ec005fdfcc8 Mon Sep 17 00:00:00 2001 From: Fernando Sonego Date: Tue, 24 Sep 2019 12:18:52 -0300 Subject: [PATCH 3/3] Editions: Cache GetAllAsync --- Controllers/EditionsController.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Controllers/EditionsController.cs b/Controllers/EditionsController.cs index 8c1cf19..e6aea2d 100644 --- a/Controllers/EditionsController.cs +++ b/Controllers/EditionsController.cs @@ -33,7 +33,19 @@ public EditionsController(EditionsRepository editionsRepository, LegacyGlobalRep public async Task> GetAllAsync() { var language = this.GetLanguage(); - return await this.editionsRepository.GetAllByLanguage(language); + var cacheKey = $"Editions-GetByIdAsync"; + var cacheResult = this.cacheService.GetValue>(cacheKey); + + if (cacheResult != null) + { + this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache"); + return cacheResult; + } + + cacheResult = await this.editionsRepository.GetAllByLanguage(language); + this.cacheService.SetValue(cacheKey, cacheResult); + + return cacheResult; } // GET api/v1/editions/5