Ejemplo n.º 1
0
        public async Task <DownloadedData> GetAsync(string url, CancellationToken token, TimeSpan?duration = null, string key = null)
        {
            string filename = string.IsNullOrWhiteSpace(key) ? _md5Helper.MD5(url) : _md5Helper.MD5(key);
            string basePath = await _diskCache.GetBasePathAsync().ConfigureAwait(false);

            string filepath = basePath == null ? filename : Path.Combine(basePath, filename);

            byte[] data = await _diskCache.TryGetAsync(filename, token).ConfigureAwait(false);

            if (data != null)
            {
                return new DownloadedData(filepath, data)
                       {
                           RetrievedFromDiskCache = true
                       }
            }
            ;

            using (var memoryStream = await DownloadAndCacheAsync(url, filename, filepath, token, duration).ConfigureAwait(false))
            {
                return(new DownloadedData(filepath, memoryStream == null ? null : memoryStream.ToArray()));
            }
        }