예제 #1
0
 public static void Add(string key, object cachingData, object cacheTimer,
                        bool isNoSlidingExpiration = true,
                        bool useSitecoreCache      = true,
                        bool globalCache           = false,
                        bool removeOnPublish       = true,
                        string siteName            = "",
                        string databaseName        = "",
                        System.Web.Caching.CacheDependency cacheDep          = null,
                        System.Web.Caching.CacheItemPriority priority        = System.Web.Caching.CacheItemPriority.Normal,
                        System.Web.Caching.CacheItemRemovedCallback callback = null)
 {
     CachingContext.Current.Add(key, cachingData, cacheTimer, isNoSlidingExpiration, useSitecoreCache, globalCache, removeOnPublish, siteName, databaseName, cacheDep, priority, callback);
 }
        /// <inheritdoc />
        public void Add(string key, object cachingData, object cacheTimer,
                        bool isNoSlidingExpiration = true,
                        bool useSitecoreCache      = true,
                        bool globalCache           = false,
                        bool removeOnPublish       = true,
                        string siteName            = "",
                        string databaseName        = "",
                        System.Web.Caching.CacheDependency cacheDep          = null,
                        System.Web.Caching.CacheItemPriority priority        = System.Web.Caching.CacheItemPriority.Normal,
                        System.Web.Caching.CacheItemRemovedCallback callback = null)
        {
            // make sure we have data
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (cachingData == null)
            {
                throw new ArgumentNullException(nameof(cachingData));
            }

            if (!removeOnPublish)
            {
                key = KeyAgent.AddKeepAfterPublish(key);
            }

            // setup defaults for caching types
            TimeSpan slidingCache  = System.Web.Caching.Cache.NoSlidingExpiration;
            DateTime absoluteCache = System.Web.Caching.Cache.NoAbsoluteExpiration;

            // set the cache type
            if (isNoSlidingExpiration)
            {
                // make sure it's right
                if (cacheTimer is DateTime)
                {
                    absoluteCache = (DateTime)cacheTimer;
                }
                else
                {
                    // we have an issue fix up
                    var timeSpanCheck = (TimeSpan)cacheTimer;
                    absoluteCache = DateTime.Now.Add(timeSpanCheck);
                }
            }
            else
            {
                // make sure it's right
                if (cacheTimer is TimeSpan)
                {
                    slidingCache = (TimeSpan)cacheTimer;
                }
                else
                {
                    // we have an issue fix up
                    var dateCheck = (DateTime)cacheTimer;
                    slidingCache = dateCheck.Subtract(DateTime.Now);
                }
            }

            // what type of cache are we using
            if (useSitecoreCache)
            {
                ICache cache = SitecoreCacheManager.GetCache(globalCache, siteName, databaseName);

                if (cache.ContainsKey(key))
                {
                    cache.Remove(key);
                }

                cache.Add(key, cachingData, slidingCache, absoluteCache);
            }
            else
            {
                var cacheStartKey = KeyAgent.GetBaseKey(globalCache, siteName, databaseName) + key;

                System.Web.HttpRuntime.Cache.Add(cacheStartKey, cachingData, cacheDep, absoluteCache, slidingCache, priority, callback);
            }
        }
예제 #3
0
 public object Add(string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback onRemoveCallback)
 {
     return(Cache.Add(RegionKey(key), value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback));
 }
 public Stream Add(string key, Stream value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback onRemoveCallback)
 {
     throw new NotImplementedException();
 }
        public static void Insert(string key, object value, TimeSpan cacheDuration, CacheExpirationType ExType, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback callback)
        {
            if (!EnableCaching)
            {
                context.Trace.Warn("Caching is disabled.");
            }
            else
            {
                context = System.Web.HttpContext.Current;
                switch (ExType)
                {
                case CacheExpirationType.Absolute:
                    context.Cache.Insert(key, value, null, DateTime.Now.Add(cacheDuration), System.Web.Caching.Cache.NoSlidingExpiration, priority, callback);
                    break;

                case CacheExpirationType.Sliding:
                    context.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, cacheDuration, priority, callback);
                    break;
                }

                context.Trace.Write(key + " cached for a duration of " + cacheDuration + ".");
            }
        }
예제 #6
0
        public void Add(string key, object data, DateTime absoluteTimeout, TimeSpan slidingTimeout, string category = null, System.Web.Caching.CacheItemPriority priority = System.Web.Caching.CacheItemPriority.Default)
        {
            var expiryTime = DateTime.UtcNow.AddMinutes(1);

            Cache.Add(key, data, expiryTime, category);
        }
예제 #7
0
 public void Insert(string keyString, object value, int cacheDurationInSeconds, System.Web.Caching.CacheItemPriority priority)
 {
 }
 public static void Insert(string key, object value, CacheExpirationType ExType, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback callback)
 {
     Insert(key, value, GetCacheDuration(key), ExType, priority, callback);
 }
예제 #9
0
        private void StoreObjectIntoCache(object cacheObject, string cacheKey, System.Web.HttpContext context, int MinutesToLive, CacheModes cacheMode, System.Web.Caching.CacheItemPriority priority)
        {
            // Only add to the cache when the object is not null
            if (cacheObject != null)
            {
                loggingService.Debug("Storing {0} object {1} into cache", cacheMode.ToString(), cacheKey);

                // Different insert for different cache mode
                switch (cacheMode)
                {
                case CacheModes.Absolute:
                    context.Cache.Insert(cacheKey,
                                         cacheObject,
                                         null,
                                         DateTime.Now.AddMinutes(MinutesToLive),
                                         System.Web.Caching.Cache.NoSlidingExpiration,
                                         priority,
                                         null);
                    break;

                case CacheModes.Sliding:
                    context.Cache.Insert(cacheKey,
                                         cacheObject,
                                         null,
                                         System.Web.Caching.Cache.NoAbsoluteExpiration,
                                         new TimeSpan(0, MinutesToLive, 0),
                                         priority,
                                         null);
                    break;
                }
            }
        }
예제 #10
0
        public T GetOrStoreObjectFromCache <T>(string cacheKey, System.Web.HttpContext context, Core.Caching.CacheModes cacheMode, int minutesToLive, System.Web.Caching.CacheItemPriority priority, Func <T> function)
        {
            object cacheItem = context.Cache[cacheKey];

            if (cacheItem == null)
            {
                // Store the object
                T cacheObject = function.Invoke();
                StoreObjectIntoCache(cacheObject,
                                     cacheKey,
                                     context,
                                     minutesToLive,
                                     cacheMode,
                                     priority);

                return(cacheObject);
            }
            else
            {
                loggingService.Debug("Getting object {0} from context cache", cacheKey);

                return((T)cacheItem);
            }
        }
        /// <summary>
        /// Saves the list to the cache
        /// </summary>
        /// <typeparam name="T">The type of parameter that will be saved</typeparam>
        /// <param name="cacheKey">The unique key to save</param>
        /// <param name="cachingData">The data to cache</param>
        /// <param name="cacheTimer">The time we want to cache this data</param>
        /// <param name="isNoSlidingExpiration">Is the cacheTimer an Absolute Expiration (default) or a sliding expiration</param>
        /// <param name="useSitecoreCache">Do you want to use Sitecore cache or the HttpRuntime cache object</param>
        /// <param name="cacheSize">The size of the cache, this will fetch the size dynamically if not provided</param>
        /// <param name="globalCache">Is the data to be stored in the global cache or site specific cache</param>
        /// <param name="removeOnPublish">Remove the contents on a publish, this is defaulted as true</param>
        /// <param name="siteName">Force set the site name, if this is run from a scheduled task this should be set</param>
        /// <param name="databaseName">Force the database if this is run from a scheduled tasks, this should be set</param>
        /// <param name="cacheDep">Any caching dependencies for the cache. NOTE: Not valid for Sitecore Caching</param>
        /// <param name="priority">The priority of the cache. NOTE: Not valid for Sitecore Caching</param>
        /// <param name="callback">The call-back function of the cache. NOTE: Not valid for Sitecore Caching</param>
        public void SaveCachedItem <T>(string cacheKey, object cachingData, object cacheTimer,
                                       bool isNoSlidingExpiration = true,
                                       bool useSitecoreCache      = true,
                                       long cacheSize             = 0,
                                       bool globalCache           = false,
                                       bool removeOnPublish       = true,
                                       string siteName            = "default",
                                       string databaseName        = "master",
                                       System.Web.Caching.CacheDependency cacheDep          = null,
                                       System.Web.Caching.CacheItemPriority priority        = System.Web.Caching.CacheItemPriority.Normal,
                                       System.Web.Caching.CacheItemRemovedCallback callback = null)
        {
            // make sure we have data
            if (!string.IsNullOrEmpty(cacheKey) &&
                cachingData != null)
            {
                // set the key so we can override it
                string key = cacheKey.ToLower();

                if (!removeOnPublish)
                {
                    key = string.Format(NonPublishKey, key).ToLower();
                }

                // setup defaults for caching types
                TimeSpan slidingCache  = System.Web.Caching.Cache.NoSlidingExpiration;
                DateTime absoluteCache = System.Web.Caching.Cache.NoAbsoluteExpiration;

                // set the cache type
                if (isNoSlidingExpiration)
                {
                    // make sure it's right
                    if (cacheTimer.GetType().Equals(typeof(DateTime)))
                    {
                        absoluteCache = (DateTime)cacheTimer;
                    }
                    else
                    {
                        // we have an issue fix up
                        TimeSpan timeSpanCheck = (TimeSpan)cacheTimer;
                        absoluteCache = DateTime.UtcNow.Add(timeSpanCheck);
                    }
                }
                else
                {
                    // make sure it's right
                    if (cacheTimer.GetType().Equals(typeof(TimeSpan)))
                    {
                        slidingCache = (TimeSpan)cacheTimer;
                    }
                    else
                    {
                        // we have an issue fix up
                        DateTime dateCheck = (DateTime)cacheTimer;
                        slidingCache = dateCheck.Subtract(DateTime.UtcNow);
                    }
                }

                // what type of cache are we using
                if (useSitecoreCache)
                {
                    #region Sitecore Cache
                    Sitecore.Caching.Cache cache = SitecoreCache(globalCache, siteName, databaseName);

                    if (cache.ContainsKey(key))
                    {
                        // remove the key
                        cache.Remove(key);
                    }

                    // do we have to work out the cache size
                    if (cacheSize == 0)
                    {
                        // we need to do a binary serialization to get the objects size
                        try
                        {
                            System.IO.MemoryStream mem = new System.IO.MemoryStream();
                            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                            binFormatter.Serialize(mem, cachingData);
                            cacheSize = mem.Length + 500; // increase just in case
                        }
                        catch (System.Exception ex)
                        {
                            // log and setup defaults
                            cacheSize = 1500; // default size we have made it bigger than normal just in case

                            // do we display the serialization error
                            if (_errorLogsEnabled)
                            {
                                Sitecore.Diagnostics.Log.Warn(string.Format("Cache - Size Serialize: '{0}'", key), ex, typeof(CacheHandler));
                            }

                            // is the type an IEnumerable
                            // get the cached item type
                            Type tType = cachingData.GetType();

                            // the type is a collection
                            if (typeof(System.Collections.ICollection).IsAssignableFrom(tType) ||
                                typeof(ICollection <>).IsAssignableFrom(tType))
                            {
                                // we want to try and see if the item is a collection
                                try
                                {
                                    // set the data as ICollection so we can get the data
                                    System.Collections.ICollection iEnum = (cachingData as System.Collections.ICollection);

                                    // make sure it casts correctly
                                    if (iEnum != null)
                                    {
                                        // we need to set this as it will cause issues
                                        long fakeCacheSize = cacheSize;
                                        cacheSize = iEnum.Count * fakeCacheSize;
                                    }
                                }
                                catch (System.Exception exer)
                                {
                                    cacheSize = 5000; // at least set it bigger just in case

                                    // do we display the logs
                                    if (_errorLogsEnabled)
                                    {
                                        Sitecore.Diagnostics.Log.Warn(string.Format("Cache - Collection Count: '{0}'", key), exer, typeof(CacheHandler));
                                    }
                                }
                            }
                        }
                    }

                    // use the sitecore cache
                    cache.Add(key.ToLower(), cachingData, cacheSize, slidingCache, absoluteCache);
                    #endregion
                }
                else
                {
                    #region HttpRuntime Cache
                    // set the cache key
                    string cacheStartKey = GetBaseKey(globalCache, siteName, databaseName) + key;

                    // confirm the cache sliding
                    System.Web.HttpRuntime.Cache.Add(cacheStartKey.ToLower(), cachingData, cacheDep, absoluteCache, slidingCache, priority, callback);
                    #endregion
                }
            }
        }
예제 #12
0
 public void Insert <T>(string key, T value, System.Web.Caching.CacheDependency dependencies, System.DateTime absoluteExpiration, System.TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback onRemoveCallback) where T : class
 {
     Cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
 }
예제 #13
0
        public object Add(string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, HostCacheItemRemovedCallback onRemoveCallback)
        {
            if (!IsHostKey(key))
            {
                key = GetHostKey(key);
            }

            if (onRemoveCallback != null)
            {
                Callbacks[key] = onRemoveCallback;
                return(System.Web.HttpRuntime.Cache.Add(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemoved)));
            }
            else
            {
                return(System.Web.HttpRuntime.Cache.Add(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, null));
            }
        }
예제 #14
0
파일: CacheEntity.cs 프로젝트: jjg0519/OA
        /// <summary>
        /// 缓存数据
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="entity">缓存对象</param>
        /// <param name="cachedp">缓存依赖项</param>
        /// <param name="priority">缓存清理优先级</param>
        public void Cache(TKey key, TValue entity, System.Web.Caching.CacheDependency cachedp, System.Web.Caching.CacheItemPriority priority)
        {
            if (entity == null)
            {
                return;
            }
            Dictionary <TKey, CacheObject <TValue> > cachedEntities = GetCachedEntities();

            lock (cachedEntities)
            {
                CacheObject <TValue> cachedEntity = new CacheObject <TValue>(entity);
                if (cachedEntities.ContainsKey(key))
                {
                    cachedEntities[key] = cachedEntity;
                }
                else
                {
                    cachedEntities.Add(key, cachedEntity);
                }
                CacheOperate.Insert(cacheKey, cachedEntities, cachedp, CacheMinutes, priority);
            }
        }
예제 #15
0
 public void Insert(string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback onRemoveCallback)
 {
 }