public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
 {
     if (((dependencies == null) && (absoluteExpiration == NoAbsoluteExpiration)) && (slidingExpiration == NoSlidingExpiration))
     {
         throw new ArgumentException(System.Web.SR.GetString("Invalid_Parameters_To_Insert"));
     }
     if (onUpdateCallback == null)
     {
         throw new ArgumentNullException("onUpdateCallback");
     }
     DateTime utcAbsoluteExpiration = DateTimeUtil.ConvertToUniversalTime(absoluteExpiration);
     this._cacheInternal.DoInsert(true, key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.NotRemovable, null, true);
     string[] cachekeys = new string[] { key };
     CacheDependency expensiveObjectDependency = new CacheDependency(null, cachekeys);
     if (dependencies == null)
     {
         dependencies = expensiveObjectDependency;
     }
     else
     {
         AggregateCacheDependency dependency2 = new AggregateCacheDependency();
         dependency2.Add(new CacheDependency[] { dependencies, expensiveObjectDependency });
         dependencies = dependency2;
     }
     this._cacheInternal.DoInsert(false, "w" + key, new SentinelEntry(key, expensiveObjectDependency, onUpdateCallback), dependencies, utcAbsoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, s_sentinelRemovedCallback, true);
 }
Example #2
0
        /// <summary>
        /// Inserts an object into the cache together with dependencies, expiration policies, and a delegate that 
        /// you can use to notify the application before the item is removed from the cache.
        /// </summary>
        /// <param name="key">The cache key that is used to reference the object.</param>
        /// <param name="value">The object to insert into the cache.</param>
        /// <param name="dependencies">The file or cache key dependencies for the item. When any dependency changes, 
        /// the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter 
        /// contains null.</param>
        /// <param name="absoluteExpiration">The time at which the inserted object expires and is removed from the 
        /// cache. To avoid possible issues with local time such as changes from standard time to daylight saving 
        /// time, use <see cref="DateTime.UtcNow"/> instead of <see cref="DateTime.Now"/> for this parameter value. 
        /// If you are using absolute expiration, the slidingExpiration parameter must be set to 
        /// <see cref="Cache.NoSlidingExpiration"/>.</param>
        /// <param name="slidingExpiration">The interval between the time that the cached object was last accessed 
        /// and the time at which that object expires. If this value is the equivalent of 20 minutes, the object 
        /// will expire and be removed from the cache 20 minutes after it was last accessed. If you are using 
        /// sliding expiration, the absoluteExpiration parameter must be set to <see cref="Cache.NoAbsoluteExpiration"/>.</param>
        /// <param name="callback">A delegate that will be called before the object is removed from the cache. 
        /// You can use this to update the cached item and ensure that it is not removed from the cache.</param>
        public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback callback)
        {
            if (cache != null)
            {
                cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, callback);
                return;
            }

            this.InsertToDictionary(key, value);
        }
Example #3
0
		void SetItemTimeout (CacheItem ci, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemRemovedCallback onRemoveCallback,
				     CacheItemUpdateCallback onUpdateCallback, string key, bool doLock)
		{
			bool disableExpiration = DisableExpiration;

			if (!disableExpiration) {
				ci.SlidingExpiration = slidingExpiration;
				if (slidingExpiration != NoSlidingExpiration)
					ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
				else
					ci.AbsoluteExpiration = absoluteExpiration;			
			}
			
			ci.OnRemoveCallback = onRemoveCallback;
			ci.OnUpdateCallback = onUpdateCallback;
			
			try {
				if (doLock)
					cacheLock.EnterWriteLock ();
				
				if (ci.Timer != null) {
					ci.Timer.Dispose ();
					ci.Timer = null;
				}

				if (key != null)
					cache [key] = ci;
				
				ci.LastChange = DateTime.Now;
				if (!disableExpiration && ci.AbsoluteExpiration != NoAbsoluteExpiration)
					EnqueueTimedItem (ci);
			} finally {
				if (doLock) {
					// See comment at the top of the file, above cacheLock declaration
					cacheLock.ExitWriteLock ();
				}
			}
		}
        /// <summary>
        /// Adds the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="val">The val.</param>
        /// <param name="slidingExpiration">The sliding expiration.</param>
        /// <param name="callback">The callback.</param>
        public void Add(string key, object val, TimeSpan slidingExpiration, CacheItemUpdateCallback callback)
        {

        }
Example #5
0
        // DevDiv Bugs 162763: 
        // Add a an event that fires *before* an item is evicted from the ASP.NET Cache
        public void Insert(
                string key,
                object value,
                CacheDependency dependencies,
                DateTime absoluteExpiration,
                TimeSpan slidingExpiration,
                CacheItemUpdateCallback onUpdateCallback) {

            if (dependencies == null && absoluteExpiration == Cache.NoAbsoluteExpiration && slidingExpiration == Cache.NoSlidingExpiration) {
                throw new ArgumentException(SR.GetString(SR.Invalid_Parameters_To_Insert));
            }
            if (onUpdateCallback == null) {
                throw new ArgumentNullException("onUpdateCallback");
            }
            DateTime utcAbsoluteExpiration = DateTimeUtil.ConvertToUniversalTime(absoluteExpiration);
            // Insert updatable cache entry
            _cacheInternal.DoInsert (
                        true,
                        key,
                        value,
                        null,
                        Cache.NoAbsoluteExpiration,
                        Cache.NoSlidingExpiration,
                        CacheItemPriority.NotRemovable,
                        null,
                        true);
            // Ensure the sentinel depends on its updatable entry
            string[] cacheKeys = { key };
            CacheDependency expensiveObjectDep = new CacheDependency(null, cacheKeys);
            if (dependencies == null) {
                dependencies = expensiveObjectDep;
            }
            else {
                AggregateCacheDependency deps = new AggregateCacheDependency();
                deps.Add(dependencies, expensiveObjectDep);
                dependencies = deps;
            }
            // Insert sentinel entry for the updatable cache entry 
            _cacheInternal.DoInsert(
                        false,
                        CacheInternal.PrefixValidationSentinel + key,
                        new SentinelEntry(key, expensiveObjectDep, onUpdateCallback),
                        dependencies,
                        utcAbsoluteExpiration,
                        slidingExpiration,
                        CacheItemPriority.NotRemovable,
                        Cache.s_sentinelRemovedCallback,
                        true);
        }
Example #6
0
		void SetItemTimeout (CacheItem ci, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemRemovedCallback onRemoveCallback,
				     CacheItemUpdateCallback onUpdateCallback, string key, bool doLock)
		{
			bool disableExpiration = DisableExpiration;

			if (!disableExpiration) {
				ci.SlidingExpiration = slidingExpiration;
				if (slidingExpiration != NoSlidingExpiration)
					ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
				else
					ci.AbsoluteExpiration = absoluteExpiration;			
			}
			
			ci.OnRemoveCallback = onRemoveCallback;
			ci.OnUpdateCallback = onUpdateCallback;
			
			try {
				if (doLock)
					cacheLock.EnterWriteLock ();

				if (key != null) {
					cache [key] = ci;
					cache.EvictIfNecessary ();
				}
				
				ci.LastChange = DateTime.Now;
				if (!disableExpiration && ci.AbsoluteExpiration != NoAbsoluteExpiration) {
					bool enqueue;
					if (ci.IsTimedItem) {
						enqueue = UpdateTimedItem (ci);
						if (!enqueue)
							UpdateTimerPeriod (ci);
					} else
						enqueue = true;

					if (enqueue) {
						ci.IsTimedItem = true;
						EnqueueTimedItem (ci);
					}
					
				}
			} finally {
				if (doLock) {
					// See comment at the top of the file, above cacheLock declaration
					cacheLock.ExitWriteLock ();
				}
			}
		}
Example #7
0
		public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
				    CacheItemUpdateCallback onUpdateCallback)
		{
			Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null, onUpdateCallback, true);
		}
 /// <summary>
 /// Add to Cache
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="val">The val.</param>
 /// <param name="slidingExpiration">The sliding expiration.</param>
 /// <param name="callback">The callback.</param>
 public void Add(string key, object val, TimeSpan slidingExpiration, CacheItemUpdateCallback callback)
 {
     HttpContext.Current.Cache.Insert(key, val, null, DateTime.MaxValue, slidingExpiration);
 }
Example #9
0
 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
 {
     Remove(key);
     _cache.Add(key, value);
 }
 public WebCacheManager(Func <string, T> loader, CacheSettings settings) : base(loader, settings)
 {
     _updateCallback = new CacheItemUpdateCallback(UpdateCallback);
 }
Example #11
0
 /// <summary>
 /// 插入缓存
 /// </summary>
 /// <param name="key">用于引用该对象的缓存密钥</param>
 /// <param name="value">要插入到缓存的对象</param>
 /// <param name="dependencies">文件或缓存关键的依存关系所插入对象。 当任何依赖关系更改时,该对象将变为无效,并从缓存中删除。 如果没有依赖关系,此参数包含 null</param>
 /// <param name="absoluteExpiration">从该处插入的对象过期并从缓存中删除的时间。 若要避免的本地时间,如从标准时间到夏时制的更改可能存在的问题,请使用 System.DateTime.UtcNow,而不是 System.DateTime.Now 为此参数值。 如果您使用的绝对过期 slidingExpiration 参数必须是 System.Web.Caching.Cache.NoSlidingExpiration</param>
 /// <param name="slidingExpiration">对象的到期时间和上次访问所插入的对象的时间之间的间隔。 如果此值为 20 分钟的等效项,该对象会过期,可从缓存中删除上次访问后的 20 分钟。 如果您使用可调到期,absoluteExpiration 参数必须是 System.Web.Caching.Cache.NoAbsoluteExpiration</param>
 /// <param name="onUpdateCallback">从缓存中删除该对象之前将调用一个委托。 您可以用于更新缓存的项目,并确保它不删除从缓存</param>
 public static void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
 {
     _cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, onUpdateCallback);
 }
Example #12
0
        void SetItemTimeout(CacheItem ci, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemRemovedCallback onRemoveCallback,
                            CacheItemUpdateCallback onUpdateCallback, string key, bool doLock)
        {
            bool disableExpiration = DisableExpiration;

            if (!disableExpiration)
            {
                ci.SlidingExpiration = slidingExpiration;
                if (slidingExpiration != NoSlidingExpiration)
                {
                    ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
                }
                else
                {
                    ci.AbsoluteExpiration = absoluteExpiration;
                }
            }

            ci.OnRemoveCallback = onRemoveCallback;
            ci.OnUpdateCallback = onUpdateCallback;

            try {
                if (doLock)
                {
                    cacheLock.EnterWriteLock();
                }

                if (key != null)
                {
                    cache [key] = ci;
                    cache.EvictIfNecessary();
                }

                ci.LastChange = DateTime.Now;
                if (!disableExpiration && ci.AbsoluteExpiration != NoAbsoluteExpiration)
                {
                    bool enqueue;
                    if (ci.IsTimedItem)
                    {
                        enqueue = UpdateTimedItem(ci);
                        if (!enqueue)
                        {
                            UpdateTimerPeriod(ci);
                        }
                    }
                    else
                    {
                        enqueue = true;
                    }

                    if (enqueue)
                    {
                        ci.IsTimedItem = true;
                        EnqueueTimedItem(ci);
                    }
                }
            } finally {
                if (doLock)
                {
                    // See comment at the top of the file, above cacheLock declaration
                    cacheLock.ExitWriteLock();
                }
            }
        }
Example #13
0
 /// <summary>
 /// 设置 当前应用程序指定CacheKey的Cache值 超过指定时间没有访问就自动释放
 /// </summary>
 /// <param name="CacheKey">唯一的缓存健值</param>
 /// <param name="objObject">将要存储的值</param>
 /// <param name="dependencies">缓存依赖,如果依赖有变化,将自动移除该缓存</param>
 /// <param name="slidingExpiration">相对超期时间,超过指定时间没有访问就自动释放</param>
 /// <param name="onUpdateCallback">更新缓存的时候回调的函数</param>
 public static void SetCache(string CacheKey, object objObject, CacheDependency dependencies, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
 {
     Cache objCache = HttpRuntime.Cache;
     objCache.Insert(CacheKey, objObject, dependencies,Cache.NoAbsoluteExpiration, slidingExpiration, onUpdateCallback);
 }
        /// <summary>
        /// Adds the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="val">The val.</param>
        /// <param name="absoluteExpiration">The absolute expiration.</param>
        /// <param name="callback">The callback.</param>
        public void Add(string key, object val, DateTime absoluteExpiration, CacheItemUpdateCallback callback)
        {

        }
        public void Callback() {
            var key = Guid.NewGuid().ToString();
            var val = Guid.NewGuid();

            HttpRuntimeCacheProvider cacheProvider = new HttpRuntimeCacheProvider();
            var expireCallback = new CacheItemUpdateCallback(Callback);
            cacheProvider.Overwrite(key, val, DateTime.Now.AddSeconds(4D), expireCallback);
            Thread.Sleep(5000);
        }
Example #16
0
 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Inserts a cache item with a given sliding expiration, priority and update callback
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="key">The key.</param>
 /// <param name="duration">The duration.</param>
 /// <param name="onUpdateCallback">The on update callback.</param>
 public static void PutSliding(object item, string key, TimeSpan duration, CacheItemUpdateCallback onUpdateCallback)
 {
     HttpRuntime.Cache.Insert(key, item, null, Cache.NoAbsoluteExpiration, duration, onUpdateCallback);
 }
 /// <summary>
 /// Adds the specified key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="val">The val.</param>
 /// <param name="absoluteExpiration">The absolute expiration.</param>
 /// <param name="callback">The callback.</param>
 public void Add(string key, object val, DateTime absoluteExpiration, CacheItemUpdateCallback callback)
 {
     HttpContext.Current.Cache.Insert(key, val, null, absoluteExpiration, TimeSpan.Zero, callback);
 }
 /// <summary>
 /// Inserts a cache item with a given absolute expiration, priority and update callback
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="key">The key.</param>
 /// <param name="duration">The duration.</param>
 /// <param name="onUpdateCallback">The on update callback.</param>
 public static void Put(object item, string key, TimeSpan duration, CacheItemUpdateCallback onUpdateCallback)
 {
     HttpRuntime.Cache.Insert(key, item, null, DateTime.Now.Add(duration), Cache.NoSlidingExpiration, onUpdateCallback);
 }
Example #20
0
		void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
			     CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, CacheItemUpdateCallback onUpdateCallback, bool doLock)
		{
			if (key == null)
				throw new ArgumentNullException ("key");
			if (value == null)
				throw new ArgumentNullException ("value");
			if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
				throw new ArgumentNullException ("slidingExpiration");
			if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
				throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
				
			CacheItem ci = new CacheItem ();
			ci.Value = value;
			ci.Key = key;
			
			if (dependencies != null) {
				ci.Dependency = dependencies;
				dependencies.DependencyChanged += new EventHandler (OnDependencyChanged);
				dependencies.SetCache (DependencyCache);
			}

			ci.Priority = priority;
			SetItemTimeout (ci, absoluteExpiration, slidingExpiration, onRemoveCallback, onUpdateCallback, key, doLock);
		}
Example #21
0
 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
                    CacheItemUpdateCallback onUpdateCallback)
 {
     Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null, onUpdateCallback, true);
 }
Example #22
0
 public SentinelEntry(string key, CacheDependency expensiveObjectDependency, CacheItemUpdateCallback callback) {
     _key = key;
     _expensiveObjectDependency = expensiveObjectDependency;
     _cacheItemUpdateCallback = callback;
 }
Example #23
0
        void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
                    CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, CacheItemUpdateCallback onUpdateCallback, bool doLock)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays(365))
            {
                throw new ArgumentNullException("slidingExpiration");
            }
            if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
            {
                throw new ArgumentException("Both absoluteExpiration and slidingExpiration are specified");
            }

            CacheItem ci = new CacheItem();

            ci.Value = value;
            ci.Key   = key;

            if (dependencies != null)
            {
                ci.Dependency = dependencies;
                dependencies.DependencyChanged += new EventHandler(OnDependencyChanged);
                dependencies.SetCache(DependencyCache);
            }

            ci.Priority = priority;
            SetItemTimeout(ci, absoluteExpiration, slidingExpiration, onRemoveCallback, onUpdateCallback, key, doLock);
        }
Example #24
0
 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
 {
     cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, onUpdateCallback);
 }
Example #25
0
        public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback)
        {
            if (((dependencies == null) && (absoluteExpiration == NoAbsoluteExpiration)) && (slidingExpiration == NoSlidingExpiration))
            {
                throw new ArgumentException(System.Web.SR.GetString("Invalid_Parameters_To_Insert"));
            }
            if (onUpdateCallback == null)
            {
                throw new ArgumentNullException("onUpdateCallback");
            }
            DateTime utcAbsoluteExpiration = DateTimeUtil.ConvertToUniversalTime(absoluteExpiration);

            this._cacheInternal.DoInsert(true, key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.NotRemovable, null, true);
            string[]        cachekeys = new string[] { key };
            CacheDependency expensiveObjectDependency = new CacheDependency(null, cachekeys);

            if (dependencies == null)
            {
                dependencies = expensiveObjectDependency;
            }
            else
            {
                AggregateCacheDependency dependency2 = new AggregateCacheDependency();
                dependency2.Add(new CacheDependency[] { dependencies, expensiveObjectDependency });
                dependencies = dependency2;
            }
            this._cacheInternal.DoInsert(false, "w" + key, new SentinelEntry(key, expensiveObjectDependency, onUpdateCallback), dependencies, utcAbsoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, s_sentinelRemovedCallback, true);
        }