示例#1
0
        /// <summary>
        /// Removes an sql dependecy set on a HttpRuntime.Cache key. This method only removes
        /// the dependency, it does not purge the actual item from cache.
        /// </summary>
        /// <param name="key">The cache key.</param>
        public static void RemoveDependecy(string key)
        {
            SqlConnection     con = new SqlConnection(_ConnectionString);
            DataAccessManager dam = new DataAccessManager(ConnectionString);

            dam.AddInputParameter("@CacheKey", key);
            dam.AddInputParameter("@ApplicationId", ClusteredCacheController.ApplicationId);

            try { dam.ExecuteNonQuery(GetFormattedStoredProcedureName(SP_DELETE)); }
            catch { throw; }
            finally
            {
                switch (ClusteredCacheController.ClusteredCachingMode)
                {
                case ClusteredCachingMode.ServiceBroker:
                    #region service broker
                    string dependencyId = GetDependencyIDFromCacheKey(key);
                    if (!string.IsNullOrEmpty(dependencyId))
                    {
                        _ServiceBrokerDependencies[dependencyId].SqlDependency.OnChange -= OnChange;
                        _ServiceBrokerDependencies.Remove(dependencyId);
                    }
                    #endregion
                    break;

                case ClusteredCachingMode.CheckAtRequest:
                    CheckAtRequestDependencies.Remove(key);
                    break;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Gets an item with a specified key from the HttpRuntime.Cache.
        /// </summary>
        /// <param name="key">The cache key.</param>
        /// <param name="loadObj">The delegate returning the item to insert into cache when it doesn't exist.</param>
        /// <param name="priority">The priority.</param>
        /// <param name="autoInsertDependency">If set to true, a dependency will be automatically created when no cache
        /// dependency is currently set. If this value is false and you try to retrieve an item from cache that was not added
        /// first, this method will return null.</param>
        /// <returns>
        /// The cached item when present at HttpRuntime.Cache, the loadObj delegate (which re-adds the item
        /// at cache) when the item can't be found at cache, null if the cache item dependency doesn't exist and autoInsertDependency
        /// is set to false or the loadObj delegate returned null.
        /// </returns>
        public static object GetCacheItem(string key, ReloadObjectAtCache loadObj, CacheItemPriority priority, bool autoInsertDependency)
        {
            object fetchedItem = null;

            switch (ClusteredCacheController.ClusteredCachingMode)
            {
            case ClusteredCachingMode.ServiceBroker:
                #region service broker
                lock (_CacheLock)
                {
                    if (HttpRuntime.Cache[key] == null)
                    {
                        // check whether we have a dependency...
                        string dependencyId = GetDependencyIDFromCacheKey(key);
                        if (!string.IsNullOrEmpty(dependencyId) || autoInsertDependency)
                        {    // we actually have a dependency, so refresh the cache...
                            try { fetchedItem = loadObj(); }
                            catch (Exception err) { throw new CachingException("Error at loading object into cache. See the inner exception for further details.", err); }
                            if (fetchedItem == null)
                            {    // we can't insert null as an object, so simply return null.
                                return(null);
                            }
                            HttpRuntime.Cache.Insert(key, fetchedItem, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, priority, null);
                            return(fetchedItem);
                        }
                        else
                        {    // this key doesn't exist and there is no associated dependency, so return null...
                            return(null);
                        }
                    }

                    return(HttpRuntime.Cache[key]);
                }
                #endregion

            case ClusteredCachingMode.CheckAtRequest:
                #region Check at request
                long synchTicks = -1;
                lock (_CacheLock)
                {
                    ClusteredCachingSynchronizationStatus status = IsUpToDate(key, out synchTicks);
                    switch (status)
                    {
                    case ClusteredCachingSynchronizationStatus.UpToDate:
                        if (HttpRuntime.Cache[key] == null)
                        {
                            try { fetchedItem = loadObj(); }
                            catch (Exception err) { throw new CachingException("Error at loading object into cache. See the inner exception for further details.", err); }
                            if (fetchedItem == null)
                            {        // we can't insert null as an object, so simply return null.
                                return(null);
                            }
                            HttpRuntime.Cache.Insert(key, fetchedItem, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, priority, null);
                            return(fetchedItem);
                        }
                        return(HttpRuntime.Cache[key]);

                    case ClusteredCachingSynchronizationStatus.OutOfDate:
                        CheckAtRequestDependencies[key] = GetCheckAtRequestInfo(synchTicks);

                        HttpRuntime.Cache.Remove(key);

                        try { fetchedItem = loadObj(); }
                        catch (Exception err) { throw new CachingException("Error at loading object into cache. See the inner exception for further details.", err); }
                        if (fetchedItem == null)
                        {        // we can't insert null as an object, so simply return null.
                            return(null);
                        }

                        HttpRuntime.Cache.Insert(key, fetchedItem, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, priority, null);
                        return(fetchedItem);


                    case ClusteredCachingSynchronizationStatus.NotFound:

                        CheckAtRequestDependencies.Add(key, GetCheckAtRequestInfo(synchTicks));
                        HttpRuntime.Cache.Remove(key);

                        try { fetchedItem = loadObj(); }
                        catch (Exception err) { throw new CachingException("Error at loading object into cache. See the inner exception for further details.", err); }

                        if (fetchedItem == null)
                        {        // we can't insert null as an object, so simply return null.
                            return(null);
                        }

                        HttpRuntime.Cache.Insert(key, fetchedItem, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, priority, null);
                        return(fetchedItem);


                    case ClusteredCachingSynchronizationStatus.RemovedFromCollection:

                        if (autoInsertDependency)
                        {
                            try { fetchedItem = loadObj(); }
                            catch (Exception err) { throw new CachingException("Error at loading object into cache. See the inner exception for further details.", err); }

                            AddCacheItem(key, fetchedItem, priority);
                            return(fetchedItem);
                        }
                        else
                        {
                            HttpRuntime.Cache.Remove(key);
                            CheckAtRequestDependencies.Remove(key);
                            return(null);
                        }
                    }
                    break;
                }
                #endregion
            }

            return(null);
        }