コード例 #1
0
        private async Task <IRandomAccessStream> LoadImageStreamFromCacheInternal(Uri imageUri)
        {
            var imageUrl = imageUri.AbsoluteUri;

            //获取不到内存缓存
            if (ImageConfig.Default.CacheMode == CacheMode.OnlyStorageCache)
            {
                //网络uri且缓存可用
                if (imageUri.IsWebScheme() && await ImageConfig.Default.StorageCacheImpl.IsCacheExistsAndAlive(imageUrl))
                {
                    ImageLog.Log("[storage] " + imageUrl);
                    var storageStream = await ImageConfig.Default.StorageCacheImpl.LoadCacheStreamAsync(imageUrl);

                    return(storageStream);
                }
            }
            return(null);
        }
コード例 #2
0
        private async Task <IRandomAccessStream> LoadImageStreamFromCacheInternal(Uri imageUri)
        {
            var imageUrl = imageUri.AbsoluteUri;

            if (_ImageConfig.CacheMode == CacheMode.MemoryAndStorageCache ||
                _ImageConfig.CacheMode == CacheMode.OnlyMemoryCache)
            {
                IRandomAccessStream memoryStream;
                //尝试获取内存缓存
                if (_ImageConfig.MemoryCacheImpl.TryGetValue(imageUrl, out memoryStream))
                {
                    ImageLog.Log("[memory] " + imageUrl);

                    //SaveStream(memoryStream, imageUri.LocalPath);
                    return(memoryStream);
                }
            }

            //获取不到内存缓存
            if (_ImageConfig.CacheMode == CacheMode.MemoryAndStorageCache ||
                _ImageConfig.CacheMode == CacheMode.OnlyStorageCache)
            {
                //网络uri且缓存可用
                if (imageUri.IsWebScheme() && await _ImageConfig.StorageCacheImpl.IsCacheExistsAndAlive(imageUrl))
                {
                    ImageLog.Log("[storage] " + imageUrl);
                    var storageStream = await _ImageConfig.StorageCacheImpl.LoadCacheStreamAsync(imageUrl);

                    // Moving cache to the memory
                    if (_ImageConfig.CacheMode == CacheMode.MemoryAndStorageCache &&
                        storageStream != null)
                    {
                        _ImageConfig.MemoryCacheImpl.Put(imageUrl, storageStream);
                    }

                    //SaveStream(storageStream, imageUri.LocalPath);
                    return(storageStream);
                }
            }
            return(null);
        }
コード例 #3
0
        /// <summary>
        /// Async loading image stream from cache or network
        /// </summary>
        /// <param name="imageUri">Uri of the image to load</param>
        /// <returns>Stream of the image if load was successfull, null otherwise</returns>
        public virtual async Task <IRandomAccessStream> LoadImageStream(Uri imageUri,
                                                                        CancellationTokenSource cancellationTokenSource)
        {
            CheckConfig();
            if (imageUri == null)
            {
                return(null);
            }
            var imageUrl = imageUri.AbsoluteUri;

            //有Cache情况,先加载Cache
            if (_ImageConfig.CacheMode != CacheMode.NoCache)
            {
                //加载Cache

                var resultFromCache = await this.LoadImageStreamFromCache(imageUri);

                if (resultFromCache != null)
                {
                    return(resultFromCache);
                }
            }
            try
            {
                ImageLog.Log("[network] loading " + imageUrl);
                IRandomAccessStream randStream = null;
                //如果有自定义UriParser,使用自定义,反之使用默认方式.
                if (_ImageConfig.UriParser != null)
                {
                    randStream = await _ImageConfig.
                                 UriParser.GetStreamFromUri(imageUri, cancellationTokenSource.Token);
                }
                else
                {
                    randStream = await imageUri.GetStreamFromUri(cancellationTokenSource.Token);
                }
                if (randStream == null)
                {
                    ImageLog.Log("[error] failed to download: " + imageUrl);
                    return(null);
                }
                var inMemoryStream = new InMemoryRandomAccessStream();
                using (randStream)
                {
                    var copyAction = RandomAccessStream.CopyAndCloseAsync(
                        randStream.GetInputStreamAt(0L),
                        inMemoryStream.GetOutputStreamAt(0L));
                    await copyAction.AsTask(cancellationTokenSource.Token);
                }
                randStream = inMemoryStream;
                ImageLog.Log("[network] loaded " + imageUrl);
                if (_ImageConfig.CacheMode != CacheMode.NoCache)
                {
                    if (_ImageConfig.CacheMode == CacheMode.MemoryAndStorageCache ||
                        _ImageConfig.CacheMode == CacheMode.OnlyMemoryCache)
                    {
                        if (randStream != null)
                        {
                            _ImageConfig.MemoryCacheImpl.Put(imageUrl, randStream);
                        }
                    }

                    if (_ImageConfig.CacheMode == CacheMode.MemoryAndStorageCache ||
                        _ImageConfig.CacheMode == CacheMode.OnlyStorageCache)
                    {
                        //是http or https 才加入本地缓存
                        if (imageUri.IsWebScheme())
                        {
                            await Task.Factory.StartNew(() =>
                            {
                                ImageLog.Log(string.Format("{0} in task t-{1}", imageUri, Task.CurrentId));
                                // Async saving to the storage cache without await
                                var saveAsync = _ImageConfig.StorageCacheImpl.SaveAsync(imageUrl, randStream)
                                                .ContinueWith(task =>
                                {
                                    ImageLog.Log(string.Format("{0} in task t1-{1}", imageUri, Task.CurrentId));
                                    if (task.IsFaulted || !task.Result)
                                    {
                                        ImageLog.Log("[error] failed to save in storage: " + imageUri);
                                    }
                                }
                                                              );
                            }, default(CancellationToken), TaskCreationOptions.AttachedToParent, this._sequentialScheduler);
                        }
                    }
                }

                return(randStream);
            }
            catch (Exception ex)
            {
                ImageLog.Log("[error] failed to save loaded image: " + imageUrl);
            }

            //var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            // May be another thread has saved image to the cache
            // It is real working case
            if (_ImageConfig.CacheMode != CacheMode.NoCache)
            {
                var resultFromCache = await this.LoadImageStreamFromCache(imageUri);

                if (resultFromCache != null)
                {
                    return(resultFromCache);
                }
            }

            ImageLog.Log("[error] failed to load image stream from cache and network: " + imageUrl);
            return(null);
        }