diff --git a/Controllers/EditionsController.cs b/Controllers/EditionsController.cs index c2ede15..e6aea2d 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 @@ -25,19 +33,40 @@ 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 [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(); + var 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 +74,25 @@ public async Task GetByIdAsync(string id) return NotFound(); } + this.cacheService.SetValue(cacheKey, result); 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) { + + 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; + } + + cacheResult = this.legacyGlobalRepository.GetEdition(id); + this.cacheService.SetValue(cacheKey, cacheResult); + return cacheResult; + } private string GetLanguage() {