Exemple #1
0
            public static void OnCacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
            {
                CacheItemUpdateReason updateReason;
                SentinelEntry         entry = value as SentinelEntry;

                switch (reason)
                {
                case CacheItemRemovedReason.Expired:
                    updateReason = CacheItemUpdateReason.Expired;
                    break;

                case CacheItemRemovedReason.DependencyChanged:
                    updateReason = CacheItemUpdateReason.DependencyChanged;
                    if (entry.ExpensiveObjectDependency.HasChanged)
                    {
                        // If the expensiveObject has been removed explicitly by Cache.Remove,
                        // return from the SentinelEntry removed callback
                        // thus effectively removing the SentinelEntry from the cache.
                        return;
                    }
                    break;

                case CacheItemRemovedReason.Underused:
                    Debug.Fail("Reason should never be CacheItemRemovedReason.Underused since the entry was inserted as NotRemovable.");
                    return;

                default:
                    // do nothing if reason is Removed
                    return;
                }

                CacheDependency         cacheDependency;
                DateTime                absoluteExpiration;
                TimeSpan                slidingExpiration;
                object                  expensiveObject;
                CacheItemUpdateCallback callback = entry.CacheItemUpdateCallback;

                // invoke update callback
                try {
                    callback(entry.Key, updateReason, out expensiveObject, out cacheDependency, out absoluteExpiration, out slidingExpiration);
                    // Dev10 861163 - Only update the "expensive" object if the user returns a new object and the
                    // cache dependency hasn't changed.  (Inserting with a cache dependency that has already changed will cause recursion.)
                    if (expensiveObject != null && (cacheDependency == null || !cacheDependency.HasChanged))
                    {
                        HttpRuntime.Cache.Insert(entry.Key, expensiveObject, cacheDependency, absoluteExpiration, slidingExpiration, entry.CacheItemUpdateCallback);
                    }
                    else
                    {
                        HttpRuntime.Cache.Remove(entry.Key);
                    }
                }
                catch (Exception e) {
                    HttpRuntime.Cache.Remove(entry.Key);
                    try {
                        WebBaseEvent.RaiseRuntimeError(e, value);
                    }
                    catch {
                    }
                }
            }