Encapsulates methods that allow the caching and retrieval of objects from the in memory cache.
示例#1
0
        /// <summary>
        /// Gets the <see cref="CachedImage"/> associated with the specified key.
        /// </summary>
        /// <param name="cachedPath">
        /// The cached path of the value to get.
        /// </param>
        /// <returns>
        /// The <see cref="CachedImage"/> matching the given key if the <see cref="CacheIndexer"/> contains an element with
        /// the specified key; otherwise, null.
        /// </returns>
        public static CachedImage GetValue(string cachedPath)
        {
            string      key         = Path.GetFileNameWithoutExtension(cachedPath);
            CachedImage cachedImage = (CachedImage)MemCache.GetItem(key);

            if (cachedImage == null)
            {
                // FileInfo is thread safe.
                FileInfo fileInfo = new FileInfo(cachedPath);

                if (!fileInfo.Exists)
                {
                    return(null);
                }

                // Pull the latest info.
                fileInfo.Refresh();

                cachedImage = new CachedImage
                {
                    Key             = Path.GetFileNameWithoutExtension(cachedPath),
                    Path            = cachedPath,
                    CreationTimeUtc = fileInfo.CreationTimeUtc
                };

                Add(cachedImage);
            }

            return(cachedImage);
        }
示例#2
0
        /// <summary>
        /// Gets the <see cref="CachedImage"/> associated with the specified key.
        /// </summary>
        /// <param name="cachedPath">
        /// The cached path of the value to get.
        /// </param>
        /// <returns>
        /// The <see cref="CachedImage"/> matching the given key if the <see cref="CacheIndexer"/> contains an element with
        /// the specified key; otherwise, null.
        /// </returns>
        public static CachedImage Get(string cachedPath)
        {
            string      key         = Path.GetFileNameWithoutExtension(cachedPath);
            CachedImage cachedImage = (CachedImage)MemCache.GetItem(key);

            return(cachedImage);
        }
示例#3
0
        /// <summary>
        /// Adds the specified key and value to the dictionary or returns the value if it exists.
        /// </summary>
        /// <param name="cachedImage">
        /// The cached image to add.
        /// </param>
        /// <returns>
        /// The value of the item to add or get.
        /// </returns>
        public static CachedImage Add(CachedImage cachedImage)
        {
            // Add the CachedImage.
            CacheItemPolicy policy = new CacheItemPolicy();

            policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> {
                cachedImage.Path
            }));

            MemCache.AddItem(cachedImage.Key, cachedImage, policy);
            return(cachedImage);
        }
示例#4
0
        /// <summary>
        /// Gets the <see cref="CachedImage"/> associated with the specified key.
        /// </summary>
        /// <param name="cachedPath">
        /// The cached path of the value to get.
        /// </param>
        /// <returns>
        /// The <see cref="CachedImage"/> matching the given key if the <see cref="CacheIndexer"/> contains an element with
        /// the specified key; otherwise, null.
        /// </returns>
        public static async Task <CachedImage> GetValueAsync(string cachedPath)
        {
            string      key         = Path.GetFileNameWithoutExtension(cachedPath);
            CachedImage cachedImage = (CachedImage)MemCache.GetItem(key);

            if (cachedImage == null)
            {
                cachedImage = await TaskHelpers.Run(() => GetCachedImage(cachedPath));

                if (cachedImage != null)
                {
                    Add(cachedImage);
                }
            }

            return(cachedImage);
        }
示例#5
0
        /// <summary>
        /// Adds a <see cref="CachedImage"/> to the cache.
        /// </summary>
        /// <param name="cachedImage">
        /// The cached image to add.
        /// </param>
        /// <param name="expiration">
        /// A <see cref="TimeSpan"/> defining the sliding expiration duration, defaults to zero
        /// </param>
        /// <returns>
        /// The value of the item to add or get.
        /// </returns>
        public static CachedImage Add(CachedImage cachedImage, TimeSpan expiration = default(TimeSpan))
        {
            if (expiration == default(TimeSpan) || expiration == TimeSpan.Zero)
            {
                expiration = new TimeSpan(0, 1, 0);
            }

            // Add the CachedImage with a sliding expiration of `expiry` minutes.
            var policy = new CacheItemPolicy {
                SlidingExpiration = expiration
            };

            if (new Uri(cachedImage.Path).IsFile)
            {
                if (ImageProcessorConfiguration.Instance.UseFileChangeMonitors)
                {
                    // When adding a file to monitor this increases the number of files that ASP.Net will actively monitoring
                    // which directly relates to FCN in ASP.Net. If there are too many monitors then the FCN buffer could overflow
                    // resulting in ASP.Net app domain restarts.

                    // If change monitoring is enabled, we should only monitor the folder, not every individual file. This will
                    // reduce the amount of file monitors, however since there are still a lot of folders generated by IP this number could
                    // still be rather large.

                    // Further to this is that by default ASP.Net will actively monitor these paths anyways so by creating a file change monitor
                    // here and the IP cache is within the web root, there will most likely be duplicate file change monitors created.

                    // If we want to add a monitor per file:
                    policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> {
                        cachedImage.Path
                    }));

                    // If we want to add a monitor per folder, we'd need to create our own implementation of `FileChangeMonitor` (i.e. DirectoryChangeMonitor)
                    // to pass in a folder to be watched and override the correct members
                    // policy.ChangeMonitors.Add(new DirectoryChangeMonitor(new List<string> { Path.GetDirectoryName(cachedImage.Path) }));
                }

                MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy);
            }
            else
            {
                MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy);
            }

            return(cachedImage);
        }
示例#6
0
        /// <summary>
        /// Adds the specified key and value to the dictionary or returns the value if it exists.
        /// </summary>
        /// <param name="cachedImage">
        /// The cached image to add.
        /// </param>
        /// <returns>
        /// The value of the item to add or get.
        /// </returns>
        public static CachedImage Add(CachedImage cachedImage)
        {
            // Add the CachedImage with a sliding expiration of 10 minutes.
            CacheItemPolicy policy = new CacheItemPolicy {
                SlidingExpiration = new TimeSpan(0, 10, 0)
            };

            if (new Uri(cachedImage.Path).IsFile)
            {
                policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> {
                    cachedImage.Path
                }));

                MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy);
            }
            else
            {
                MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy);
            }

            return(cachedImage);
        }
示例#7
0
        /// <summary>
        /// Removes the value associated with the specified key.
        /// </summary>
        /// <param name="cachedPath">
        /// The key of the item to remove.
        /// </param>
        /// <returns>
        /// true if the <see cref="CacheIndexer"/> removes an element with
        /// the specified key; otherwise, false.
        /// </returns>
        public static bool Remove(string cachedPath)
        {
            string key = Path.GetFileNameWithoutExtension(cachedPath);

            return(MemCache.RemoveItem(key));
        }