Exemplo n.º 1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var item = value as ResturentItems;

            if (item == null)
            {
                return(null);
            }

            var key = _md5.MD5($"ResturentItems://{item.ID}");

            return(new CustomStreamImageSource()
            {
                Key = key,
                Stream = (token) =>
                {
                    if (token.IsCancellationRequested)
                    {
                        return null;
                    }

                    return _diskCache.TryGetStreamAsync(key);
                }
            });
        }
Exemplo n.º 2
0
        public async Task <CacheStream> GetStreamAsync(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);

            var diskStream = await _diskCache.TryGetStreamAsync(filename).ConfigureAwait(false);

            if (diskStream != null)
            {
                return(new CacheStream(diskStream, true));
            }

            var memoryStream = await DownloadStreamAndCacheAsync(url, filename, filepath, token, duration).ConfigureAwait(false);

            return(new CacheStream(memoryStream, false));
        }
Exemplo n.º 3
0
        public async Task <CacheStream> GetStreamAsync(string url, CancellationToken token, TimeSpan?duration = null)
        {
            string filename = _md5Helper.MD5(url);
            string basePath = await _diskCache.GetBasePathAsync().ConfigureAwait(false);

            string filepath   = basePath == null ? filename : Path.Combine(basePath, filename);
            var    diskStream = await _diskCache.TryGetStreamAsync(filename).ConfigureAwait(false);

            if (diskStream != null)
            {
                return(new CacheStream(diskStream, true));
            }

            var memoryStream = await DownloadAndCacheAsync(url, filename, filepath, token, duration).ConfigureAwait(false);

            return(new CacheStream(memoryStream, false));
        }
Exemplo n.º 4
0
        public async Task <CacheStream> GetStreamAsync(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);

            if (allowDiskCaching)
            {
                var diskStream = await _diskCache.TryGetStreamAsync(filename).ConfigureAwait(false);

                if (diskStream != null)
                {
                    return(new CacheStream(diskStream, true));
                }
            }

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

            onDownloadStarted?.Invoke(downloadInformation);

            var memoryStream = await DownloadStreamAndCacheAsync(url, filename, token, duration, cacheType).ConfigureAwait(false);

            return(new CacheStream(memoryStream, false));
        }