示例#1
0
        /// <summary>
        /// Determines whether [is up to date] [the specified key].
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="synchTicks">The synch ticks.</param>
        /// <returns></returns>
        public static ClusteredCachingSynchronizationStatus IsUpToDate(string key, out long synchTicks)
        {
            #region check whether we can delay the synchronization check
            if (ClusteredCacheController.CheckAtRequestIsUpToDateDelayInMilliseconds > 0 &&
                CheckAtRequestDependencies.ContainsKey(key) &&
                CheckAtRequestDependencies[key].LastChecked.HasValue &&
                CheckAtRequestDependencies[key].LastChecked.Value.AddMilliseconds(ClusteredCacheController.CheckAtRequestIsUpToDateDelayInMilliseconds) > DateTime.UtcNow)
            {
                synchTicks = CheckAtRequestDependencies[key].Ticks;
                return(ClusteredCachingSynchronizationStatus.UpToDate);
            }
            #endregion

            DataAccessManager dam = new DataAccessManager(ConnectionString);
            dam.AddInputParameter("@CacheKey", key);
            dam.AddInputParameter("@ApplicationId", ClusteredCacheController.ApplicationId);
            synchTicks = dam.ExecuteScalar <long>(GetFormattedStoredProcedureName(SP_SELECT));

            if (synchTicks <= 0)
            {
                return(ClusteredCachingSynchronizationStatus.RemovedFromCollection);
            }
            else
            {
                if (CheckAtRequestDependencies.ContainsKey(key))
                {
                    if (CheckAtRequestDependencies[key].Ticks.Equals(synchTicks))
                    {
                        if (ClusteredCacheController.CheckAtRequestIsUpToDateDelayInMilliseconds > 0)
                        {
                            CheckAtRequestDependencies[key] = GetCheckAtRequestInfo(synchTicks);
                        }

                        return(ClusteredCachingSynchronizationStatus.UpToDate);
                    }
                    else
                    {
                        return(ClusteredCachingSynchronizationStatus.OutOfDate);
                    }
                }
                else
                {
                    return(ClusteredCachingSynchronizationStatus.NotFound);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Adds an object to the HttpRuntime.Cache collection and sets an SQL Dependency on it's
        /// key.
        /// </summary>
        /// <param name="key">The cache key.</param>
        /// <param name="item">The item to add to the cache.</param>
        /// <param name="priority">The priority.</param>
        public static void AddCacheItem(string key, object item, CacheItemPriority priority)
        {
            if (ClusteredCacheController.ClusteredCachingMode == ClusteredCachingMode.ServiceBroker)
            {
                if (_ServiceBrokerDependencies.ContainsKey(key))
                {
                    _ServiceBrokerDependencies.Remove(key);
                }
            }

            #region insert/update at database
            long utcNow = DateTime.UtcNow.Ticks;

            DataAccessManager dam = new DataAccessManager(ConnectionString);
            dam.AddInputParameter("@CacheKey", key);
            dam.AddInputParameter("@LastUpdate", utcNow);
            dam.AddInputParameter("@ApplicationId", ClusteredCacheController.ApplicationId);
            dam.ExecuteNonQuery(GetFormattedStoredProcedureName(SP_INSERT_UPDATE));
            #endregion

            #region add to cache collection
            HttpRuntime.Cache.Remove(key);
            HttpRuntime.Cache.Insert(key, item, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, priority, null);
            #endregion

            #region add dependency

            switch (ClusteredCacheController.ClusteredCachingMode)
            {
            case ClusteredCachingMode.ServiceBroker:
                #region service broker
                SqlConnection con = new SqlConnection(_ConnectionString);
                SqlCommand    cmd = new SqlCommand(GetFormattedStoredProcedureName(SP_SELECT), con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CacheKey", SqlDbType.NVarChar, 256);
                cmd.Parameters[0].Value = key;

                // Clear any existing notifications
                cmd.Notification = null;

                // Create the dependency for this command
                SqlDependency dependency = new SqlDependency(cmd);

                // Add the event handler
                dependency.OnChange += new OnChangeEventHandler(OnChange);

                #region execute reader
                try
                {
                    con.Open();
                    // Execute the command.
                    cmd.ExecuteReader();
                    // Process the DataReader
                }
                catch (SqlException err)
                {
                    DataAccessManager.ThrowDataAccessManagerException(err, GetFormattedStoredProcedureName(SP_SELECT));
                }
                finally
                {
                    con.Close();
                }
                #endregion

                _ServiceBrokerDependencies.Add(dependency.Id, new CacheKeySqlDependency()
                {
                    CacheKey = key, SqlDependency = dependency
                });
                #endregion
                break;

            case ClusteredCachingMode.CheckAtRequest:
                if (CheckAtRequestDependencies.ContainsKey(key))
                {
                    CheckAtRequestDependencies[key] = GetCheckAtRequestInfo(utcNow);
                }
                else
                {
                    CheckAtRequestDependencies.Add(key, GetCheckAtRequestInfo(utcNow));
                }
                break;
            }
            #endregion
        }