AddItem() public static method

Adds an item to the cache.
public static AddItem ( string key, object value, System.Runtime.Caching.CacheItemPolicy policy = null, string regionName = null ) : bool
key string /// A unique identifier for the cache entry. ///
value object /// The object to insert. ///
policy System.Runtime.Caching.CacheItemPolicy /// Optional. An object that contains eviction details for the cache entry. This object /// provides more options for eviction than a simple absolute expiration. The default value for the optional parameter /// is null. ///
regionName string /// Optional. A named region in the cache to which the cache entry can be added, /// if regions are implemented. The default value for the optional parameter /// is null. ///
return bool
示例#1
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);
        }
示例#2
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);
        }
示例#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 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);
        }