コード例 #1
0
        public virtual async Task <ImageCacheItem> GetAsync(string key)
        {
            var item = await rootFolder.TryGetItemAsync(key);

            if (item == null || !item.IsOfType(StorageItemTypes.File))
            {
                return(null);
            }

            var file = (StorageFile)item;

            var cacheItem = new ImageCacheItem
            {
                Buffer = await FileIO.ReadBufferAsync(file)
            };

            try
            {
                // Use ImageProperties.DateTaken to get expiration date
                var imageProperties = await file.Properties.GetImagePropertiesAsync();

                cacheItem.Expires = imageProperties.DateTaken.UtcDateTime;
            }
            catch
            {
            }

            //Debug.WriteLine("Loaded cached image {0}", file.Path);
            return(cacheItem);
        }
コード例 #2
0
        public virtual async Task<ImageCacheItem> GetAsync(string key)
        {
            var item = await rootFolder.TryGetItemAsync(key);

            if (item == null || !item.IsOfType(StorageItemTypes.File))
            {
                return null;
            }

            var file = (StorageFile)item;

            var cacheItem = new ImageCacheItem
            {
                Buffer = await FileIO.ReadBufferAsync(file)
            };

            try
            {
                // Use ImageProperties.DateTaken to get expiration date
                var imageProperties = await file.Properties.GetImagePropertiesAsync();
                cacheItem.Expires = imageProperties.DateTaken.UtcDateTime;
            }
            catch
            {
            }

            //Debug.WriteLine("Loaded cached image {0}", file.Path);
            return cacheItem;
        }
コード例 #3
0
        public override object Get(string key, string regionName = null)
        {
            if (regionName != null)
            {
                throw new NotSupportedException("ImageFileCache does not support named regions.");
            }

            if (key == null)
            {
                throw new ArgumentNullException("The parameter key must not be null.");
            }

            var imageCacheItem = memoryCache.Get(key) as ImageCacheItem;

            if (imageCacheItem == null)
            {
                var path = FindFile(key);

                if (path != null)
                {
                    try
                    {
                        var buffer = File.ReadAllBytes(path);

                        if (buffer.Length > 16 && Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == "EXPIRES:")
                        {
                            var expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);

                            Array.Resize(ref buffer, buffer.Length - 16);

                            imageCacheItem = new ImageCacheItem
                            {
                                Buffer     = buffer,
                                Expiration = expiration
                            };

                            memoryCache.Set(key, imageCacheItem, new CacheItemPolicy {
                                AbsoluteExpiration = expiration
                            });

                            //Debug.WriteLine("ImageFileCache: Reading {0}, Expires {1}", path, imageCacheItem.Expiration.ToLocalTime());
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
                    }
                }
            }

            return(imageCacheItem);
        }
コード例 #4
0
        public override object Get(string key, string regionName = null)
        {
            if (regionName != null)
            {
                throw new NotSupportedException("ImageFileCache does not support named regions.");
            }

            if (key == null)
            {
                throw new ArgumentNullException("The parameter key must not be null.");
            }

            var imageCacheItem = memoryCache.Get(key) as ImageCacheItem;

            if (imageCacheItem == null)
            {
                var path = FindFile(key);

                if (path != null)
                {
                    try
                    {
                        var buffer     = File.ReadAllBytes(path);
                        var expiration = GetExpiration(ref buffer);

                        imageCacheItem = new ImageCacheItem
                        {
                            Buffer     = buffer,
                            Expiration = expiration
                        };

                        memoryCache.Set(key, imageCacheItem, new CacheItemPolicy {
                            AbsoluteExpiration = expiration
                        });

                        //Debug.WriteLine("ImageFileCache: Reading {0}, Expires {1}", path, imageCacheItem.Expiration.ToLocalTime());
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
                    }
                }
            }

            return(imageCacheItem);
        }