System.Web.Caching.Cache.Insert C# (CSharp) Method

Insert() public method

public Insert ( string key, object value ) : void
key string
value object
return void
		public void Insert (string key, object value)
		{
			Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, null, true);
		}
		

Same methods

Cache::Insert ( string key, object value, System.Web.Caching.CacheDependency dependencies ) : void
Cache::Insert ( string key, object value, System.Web.Caching.CacheDependency dependencies, System.DateTime absoluteExpiration, System.TimeSpan slidingExpiration ) : void
Cache::Insert ( string key, object value, System.Web.Caching.CacheDependency dependencies, System.DateTime absoluteExpiration, System.TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback ) : void
Cache::Insert ( string key, object value, System.Web.Caching.CacheDependency dependencies, System.DateTime absoluteExpiration, System.TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, CacheItemUpdateCallback onUpdateCallback, bool doLock ) : void
Cache::Insert ( string key, object value, System.Web.Caching.CacheDependency dependencies, System.DateTime absoluteExpiration, System.TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback ) : void

Usage Example

        /// <summary>
        /// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
        /// </summary>
        /// <typeparam name="TT"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="priority"></param>
        /// <param name="refreshAction"></param>
        /// <param name="cacheDependency"></param>
        /// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
        /// <param name="getCacheItem"></param>
        /// <param name="syncLock"></param>
        /// <returns></returns>
        private TT GetCacheItem <TT>(string cacheKey,
                                     CacheItemPriority priority, CacheItemRemovedCallback refreshAction,
                                     CacheDependency cacheDependency, TimeSpan?timeout, Func <TT> getCacheItem, object syncLock)
        {
            var result = _cache.Get(cacheKey);

            if (result == null)
            {
                lock (syncLock)
                {
                    result = _cache.Get(cacheKey);
                    if (result == null)
                    {
                        result = getCacheItem();
                        if (result != null)
                        {
                            //we use Insert instead of add if for some crazy reason there is now a cache with the cache key in there, it will just overwrite it.
                            _cache.Insert(cacheKey, result, cacheDependency,
                                          timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value),
                                          TimeSpan.Zero, priority, refreshAction);
                        }
                    }
                }
            }
            return(result.TryConvertTo <TT>().Result);
        }
All Usage Examples Of System.Web.Caching.Cache::Insert