Exemple #1
0
        public async Task <DownloadedData> GetAsync(string url, CancellationToken token, Action <DownloadInformation> onDownloadStarted, TimeSpan?duration = null, string key = null, CacheType?cacheType = null)
        {
            string filename         = (string.IsNullOrWhiteSpace(key) ? _md5Helper.MD5(url) : _md5Helper.MD5(key))?.ToSanitizedKey();
            var    allowDiskCaching = AllowDiskCaching(cacheType);
            string filepath         = allowDiskCaching == false ? null : await _diskCache.GetFilePathAsync(filename).ConfigureAwait(false);

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

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

            var downloadInformation = new DownloadInformation(url, key, filename, allowDiskCaching, duration);

            onDownloadStarted?.Invoke(downloadInformation);

            var bytes = await DownloadBytesAndCacheAsync(url, filename, token, duration, cacheType).ConfigureAwait(false);

            return(new DownloadedData(filepath, bytes));
        }
Exemple #2
0
        public async Task <DownloadedData> GetAsync(string url, TimeSpan?duration = null)
        {
            string filename = _md5Helper.MD5(url);
            string filepath = Path.Combine(_diskCache.BasePath, filename);

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

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

            data = await DownloadAndCacheAsync(url, filename, filepath, duration).ConfigureAwait(false);

            return(new DownloadedData(filepath, data));
        }
        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 filepath = await _diskCache.GetFilePathAsync(filename);

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

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

            var bytes = await DownloadBytesAndCacheAsync(url, filename, filepath, token, duration).ConfigureAwait(false);

            return(new DownloadedData(filepath, bytes));
        }
Exemple #4
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()));
            }
        }