public IActionResult Get() { var cacheKey = $"image_{DateTime.UtcNow:yyyy_MM_dd}"; var imageBytes = _cache.Get <byte[]>(cacheKey); if (imageBytes == null) { imageBytes = _client.GetFileBytes(); var options = new MemoryCacheEntryOptions(); options.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2); _cache.Set <byte[]>(cacheKey, imageBytes, options); } return(new FileContentResult(imageBytes, "image/jpeg")); }
protected override async Task ExecuteAsync(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { string cacheKey = $"image_{DateTime.UtcNow:yyyy_MM_dd}"; byte[] imageBytes = _cache.Get <byte[]>(cacheKey); if (imageBytes == null) { imageBytes = _client.GetFileBytes(); var options = new MemoryCacheEntryOptions(); options.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2); _cache.Set <byte[]>(cacheKey, imageBytes, options); } await Task.Delay(TimeSpan.FromMinutes(1)); } }