Custom implementation of MemoryCache https://github.com/ironyx/sharpmemorycache
Наследование: System.Runtime.Caching.MemoryCache
Пример #1
0
        /// <summary>
        /// Flushes the memory cache.
        /// </summary>
        private static void FlushMemoryCache()
        {
            lock (RockMemoryCache.s_initLock)
            {
                if (RockMemoryCache.s_defaultCache != null)
                {
                    RockMemoryCache.s_defaultCache.Dispose();
                }

                RockMemoryCache.s_defaultCache = new RockMemoryCache();
            }
        }
Пример #2
0
        /// <summary>
        /// Copies from model.
        /// </summary>
        /// <param name="model">The model.</param>
        public virtual void CopyFromModel(Rock.Data.IEntity model)
        {
            this.Id          = model.Id;
            this.Guid        = model.Guid;
            this.ForeignId   = model.ForeignId;
            this.ForeignGuid = model.ForeignGuid;
            this.ForeignKey  = model.ForeignKey;

            RockMemoryCache cache = RockMemoryCache.Default;

            cache.Set(model.Guid.ToString(), model.Id, new CacheItemPolicy());
        }
Пример #3
0
        /// <summary>
        /// Removes Global Attributes from cache
        /// </summary>
        public static void Flush()
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            cache.Remove(GlobalAttributesCache.CacheKey());

            if (HttpContext.Current != null)
            {
                var appSettings = HttpContext.Current.Application;
                appSettings[ORG_LOC_GUID]    = null;
                appSettings[ORG_LOC_STATE]   = null;
                appSettings[ORG_LOC_COUNTRY] = null;
            }
        }
Пример #4
0
        /// <summary>
        /// Flushes all the pages that use a specific layout.
        /// </summary>
        public static void FlushLayout(int layoutId)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            foreach (var item in cache)
            {
                if (item.Key.StartsWith("Rock:Page:"))
                {
                    var page = cache[item.Key] as PageCache;
                    if (page != null && page.LayoutId == layoutId)
                    {
                        cache.Remove(item.Key);
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Gets the or add existing.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="valueFactory">The value factory.</param>
        /// <returns></returns>
        public static int GetOrAddExisting(string key, Func <int> valueFactory)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            object cacheValue = cache.Get(key);

            if (cacheValue != null)
            {
                return((int)cacheValue);
            }

            int value = valueFactory();

            cache.Set(key, value, new CacheItemPolicy());
            return(value);
        }
Пример #6
0
        /// <summary>
        /// Flushes the block instances for all the pages that use a specific site.
        /// </summary>
        public static void FlushSiteBlocks(int siteId)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            foreach (var item in cache)
            {
                if (item.Key.StartsWith("Rock:Page:"))
                {
                    var page = cache[item.Key] as PageCache;
                    if (page != null && page.SiteId == siteId)
                    {
                        page.FlushBlocks();
                    }
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Gets the or add existing.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="valueFactory">The value factory.</param>
        /// <returns></returns>
        public static int GetOrAddExisting(string key, Func <int> valueFactory)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            var newValue = new Lazy <int>(valueFactory);
            var oldValue = cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy <int>;

            try
            {
                return((oldValue ?? newValue).Value);
            }
            catch
            {
                cache.Remove(key);
                throw;
            }
        }
Пример #8
0
        /// <summary>
        /// Gets the or add all.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="valueFactory">The value factory.</param>
        /// <returns></returns>
        public static List <int> GetOrAddAll(string key, Func <List <int> > valueFactory)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            var value = cache.Get(key) as List <int>;

            if (value != null)
            {
                return(value);
            }

            value = valueFactory();
            if (value != null)
            {
                cache.Set(key, value, new CacheItemPolicy());
            }
            return(value);
        }
Пример #9
0
        /// <summary>
        /// Gets the existing or a new item from cache
        /// </summary>
        /// <typeparam name="TT">The type of the t.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="valueFactory">The value factory.</param>
        /// <returns></returns>
        public static TT GetOrAddExisting <TT>(string key, Func <TT> valueFactory)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            object cacheValue = cache.Get(key);

            if (cacheValue != null)
            {
                return((TT)cacheValue);
            }

            TT value = valueFactory();

            if (value != null)
            {
                cache.Set(key, value, new CacheItemPolicy());
            }
            return(value);
        }
Пример #10
0
        /// <summary>
        /// Gets the or add existing.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="valueFactory">The value factory.</param>
        /// <returns></returns>
        public static LavaTemplateCache GetOrAddExisting(string key, Func <LavaTemplateCache> valueFactory)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            object cacheValue = cache.Get(key);

            if (cacheValue != null)
            {
                return((LavaTemplateCache)cacheValue);
            }

            LavaTemplateCache value = valueFactory();

            var cacheItemPolicy = new CacheItemPolicy();

            cacheItemPolicy.SlidingExpiration = new TimeSpan(0, 10, 0);
            cache.Set(key, value, cacheItemPolicy);

            return(value);
        }
Пример #11
0
        /// <summary>
        /// Flushes the cache.
        /// </summary>
        /// <param name="key">The key.</param>
        public static void FlushCache(string key)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            cache.Remove(key);
        }
Пример #12
0
        /// <summary>
        /// Caches the contains key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static bool CacheContainsKey(string key)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            return(cache.Contains(key));
        }
Пример #13
0
        /// <summary>
        /// Sets the cache.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="item">The item.</param>
        /// <param name="policy">The policy.</param>
        public static void SetCache(string key, object item, CacheItemPolicy policy)
        {
            RockMemoryCache cache = RockMemoryCache.Default;

            cache.Set(key, item, policy);
        }
Пример #14
0
        /// <summary>
        /// Flushes the memory cache.
        /// </summary>
        private static void FlushMemoryCache()
        {
            lock ( RockMemoryCache.s_initLock )
            {
                if ( RockMemoryCache.s_defaultCache != null )
                {
                    RockMemoryCache.s_defaultCache.Dispose();
                }

                RockMemoryCache.s_defaultCache = new RockMemoryCache();
            }
        }
Пример #15
0
 /// <summary>
 /// Clears all items from cache.
 /// </summary>
 public static void Clear()
 {
     lock ( RockMemoryCache.s_initLock )
     {
         if ( RockMemoryCache.s_defaultCache != null )
         {
             RockMemoryCache.s_defaultCache.Dispose();
             RockMemoryCache.s_defaultCache = null;
         }
     }
 }