public async Task <IEnumerable <FruitResource> > GetFruitsAsync() { // Note: In the real world, we'd use a caching interface that supports GetOrAddAsync<T> for atomicity const string region = nameof(CachingFruitService) + "|" + nameof(GetFruitsAsync); var key = region; var cached = await _cache.GetStringAsync(key); IEnumerable <FruitResource> result; if (cached != null) { _logger.LogInformation("Cached fruits found"); result = JsonConvert.DeserializeObject <List <FruitResource> >(cached); } else { _logger.LogInformation("Cached fruits not found, retrieving from data store"); result = await _dataLayer.GetFruitsAsync(); await _cache.SetStringAsync(key, JsonConvert.SerializeObject(result)); } return(result.Select(_mapper.Map <FruitResource>)); }
public async Task <IEnumerable <FruitResource> > Get([FromServices] IFruitService service) { _logger.LogInformation("Getting all fruits"); return(await service.GetFruitsAsync()); }