private async Task <byte[]> DownloadAndCacheAsync(string url, string filename, string filepath, TimeSpan?duration) { if (duration == null) { duration = new TimeSpan(30, 0, 0, 0); // by default we cache data 30 days } var data = await _httpClient.GetByteArrayAsync(url).ConfigureAwait(false); if (data == null) { data = new byte[0]; } if (data.Length == 0) { // this is a strange situation so let's not cache this too long: here 5 minutes duration = new TimeSpan(0, 5, 0); } // this ensures the fullpath exists await _diskCache.AddOrUpdateAsync(filename, data, duration.Value).ConfigureAwait(false); return(data); }