Пример #1
0
        /// <summary>
        /// Returns Block object from cache.  If block does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static BlockCache Read(int id)
        {
            string cacheKey = BlockCache.CacheKey(id);

            ObjectCache cache = MemoryCache.Default;
            BlockCache  block = cache[cacheKey] as BlockCache;

            if (block != null)
            {
                return(block);
            }
            else
            {
                var blockService = new Model.BlockService();
                var blockModel   = blockService.Get(id);
                if (blockModel != null)
                {
                    blockModel.LoadAttributes();

                    block = BlockCache.CopyModel(blockModel);

                    cache.Set(cacheKey, block, new CacheItemPolicy());

                    return(block);
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Adds Block model to cache, and returns cached object
        /// </summary>
        /// <param name="blockModel"></param>
        /// <returns></returns>
        public static BlockCache Read(Rock.Model.Block blockModel)
        {
            string cacheKey = BlockCache.CacheKey(blockModel.Id);

            ObjectCache cache = MemoryCache.Default;
            BlockCache  block = cache[cacheKey] as BlockCache;

            if (block != null)
            {
                return(block);
            }
            else
            {
                block = BlockCache.CopyModel(blockModel);
                cache.Set(cacheKey, block, new CacheItemPolicy());

                return(block);
            }
        }