Example #1
0
        static public async Task <Tuple <BitmapImage, bool> > TryRetrieveOnlineCacheAsync(string cacheName)
        {
            try
            {
                var cachePath = Path.Combine(localCache.Path, cacheName);

                if (_bitmapDiskCache.TryGetValue(cachePath, out BitmapImage img))
                {
                    // Memory cache hit
                    return(new Tuple <BitmapImage, bool>(img, true));
                }
                else
                {
                    var file = await localCache.TryGetItemAsync(cacheName) as StorageFile;

                    if (file == null)
                    {
                        return(CacheMiss);
                    }

                    using (var fs = await file.OpenReadAsync())
                    {
                        if (fs.Size == 0)
                        {
                            _bitmapDiskCache.TryAdd(cachePath, null);
                            return(CacheDisabled); // Empty file stub
                        }

                        img = await CreateBitmapFromStreamAsync(fs);

                        if (img != null)
                        {
                            _bitmapDiskCache.AddOrUpdate(cachePath, img);
                            return(new Tuple <BitmapImage, bool>(img, true)); // Cache found
                        }
                    }
                }
            }
            catch { }
            return(CacheDisabled); // Decode error
        }
Example #2
0
        public static async Task <(BitmapImage, bool)> InternalGetAsync(string cacheName, int requiredSize)
        {
            using (await _sync.LockAsync())
            {
                if (_bitmapDiskCache.TryGetValue(cacheName, out var cachedValue))
                {
                    if (cachedValue == null || cachedValue.DecodePixelWidth == 0 || cachedValue.DecodePixelWidth >= requiredSize)
                    {
                        return(cachedValue, true);
                    }
                    else
                    {
                        _bitmapDiskCache.TryRemove(cacheName);
                    }
                }
                var folder = await EnsureThumbnailFolderCreatedAsync();

                var item = await folder.TryGetItemAsync(cacheName);

                if (item == null || !(item is StorageFile))
                {
                    return(null, false);
                }

                var file = (StorageFile)item;

                using (var stream = await file.OpenReadAsync())
                {
                    if (stream.Size == 0)
                    {
                        _bitmapDiskCache.Add(cacheName, null);
                        return(null, true);
                    }

                    var img = await TryDecodeAsync(stream, requiredSize);

                    _bitmapDiskCache.Add(cacheName, img);
                    return(img, true);
                }
            }
        }