예제 #1
0
        /// <summary>
        /// Reads the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public static BlockCache Read(Guid guid)
        {
            ObjectCache cache    = MemoryCache.Default;
            object      cacheObj = cache[guid.ToString()];

            if (cacheObj != null)
            {
                return(Read((int)cacheObj));
            }
            else
            {
                var blockService = new BlockService();
                var blockModel   = blockService.Get(guid);
                if (blockModel != null)
                {
                    blockModel.LoadAttributes();
                    var block = new BlockCache(blockModel);

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set(BlockCache.CacheKey(block.Id), block, cachePolicy);
                    cache.Set(block.Guid.ToString(), block.Id, cachePolicy);

                    return(block);
                }
                else
                {
                    return(null);
                }
            }
        }
예제 #2
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">The 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 BlockService();
                var blockModel   = blockService.Get(id);
                if (blockModel != null)
                {
                    blockModel.LoadAttributes();
                    block = new BlockCache(blockModel);

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set(cacheKey, block, cachePolicy);
                    cache.Set(block.Guid.ToString(), block.Id, cachePolicy);

                    return(block);
                }
                else
                {
                    return(null);
                }
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        /// <summary>
        /// Adds Block model to cache, and returns cached object
        /// </summary>
        /// <param name="blockModel">The block model.</param>
        /// <returns></returns>
        public static BlockCache Read(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 = new BlockCache(blockModel);

                var cachePolicy = new CacheItemPolicy();
                cache.Set(cacheKey, block, cachePolicy);
                cache.Set(block.Guid.ToString(), block.Id, cachePolicy);

                return(block);
            }
        }
예제 #5
0
 /// <summary>
 /// Removes block from cache
 /// </summary>
 /// <param name="id"></param>
 public static void Flush(int id)
 {
     FlushCache(BlockCache.CacheKey(id));
 }
예제 #6
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">The id.</param>
 /// <param name="rockContext">The rock context.</param>
 /// <returns></returns>
 public static BlockCache Read(int id, RockContext rockContext = null)
 {
     return(GetOrAddExisting(BlockCache.CacheKey(id),
                             () => LoadById(id, rockContext)));
 }
예제 #7
0
        /// <summary>
        /// Removes block from cache
        /// </summary>
        /// <param name="id"></param>
        public static void Flush(int id)
        {
            ObjectCache cache = MemoryCache.Default;

            cache.Remove(BlockCache.CacheKey(id));
        }
예제 #8
0
 /// <summary>
 /// Adds Block model to cache, and returns cached object
 /// </summary>
 /// <param name="blockModel">The block model.</param>
 /// <returns></returns>
 public static BlockCache Read(Block blockModel)
 {
     return(GetOrAddExisting(BlockCache.CacheKey(blockModel.Id),
                             () => LoadByModel(blockModel)));
 }