Ejemplo n.º 1
0
        /// <summary>
        /// Reads the specified block type model.
        /// </summary>
        /// <param name="blockTypeModel">The block type model.</param>
        /// <returns></returns>
        public static BlockTypeCache Read(BlockType blockTypeModel)
        {
            string cacheKey = BlockTypeCache.CacheKey(blockTypeModel.Id);

            ObjectCache    cache     = MemoryCache.Default;
            BlockTypeCache blockType = cache[cacheKey] as BlockTypeCache;

            if (blockType != null)
            {
                return(blockType);
            }
            else
            {
                blockType = new BlockTypeCache(blockTypeModel);

                var cachePolicy = new CacheItemPolicy();
                AddChangeMonitor(cachePolicy, blockType.Path);
                cache.Set(cacheKey, blockType, cachePolicy);

                var guidCachePolicy = new CacheItemPolicy();
                AddChangeMonitor(guidCachePolicy, blockType.Path);
                cache.Set(blockType.Guid.ToString(), blockType.Id, guidCachePolicy);

                return(blockType);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public static BlockTypeCache Read(Guid guid)
        {
            ObjectCache cache    = MemoryCache.Default;
            object      cacheObj = cache[guid.ToString()];

            if (cacheObj != null)
            {
                return(Read((int)cacheObj));
            }
            else
            {
                var blockTypeService = new BlockTypeService();
                var blockTypeModel   = blockTypeService.Get(guid);
                if (blockTypeModel != null)
                {
                    blockTypeModel.LoadAttributes();
                    var blockType = new BlockTypeCache(blockTypeModel);

                    var cachePolicy = new CacheItemPolicy();
                    AddChangeMonitor(cachePolicy, blockType.Path);
                    cache.Set(BlockTypeCache.CacheKey(blockType.Id), blockType, cachePolicy);

                    var guidCachePolicy = new CacheItemPolicy();
                    AddChangeMonitor(guidCachePolicy, blockType.Path);
                    cache.Set(blockType.Guid.ToString(), blockType.Id, guidCachePolicy);

                    return(blockType);
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns Block Type 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 BlockTypeCache Read(int id)
        {
            string cacheKey = BlockTypeCache.CacheKey(id);

            ObjectCache    cache     = MemoryCache.Default;
            BlockTypeCache blockType = cache[cacheKey] as BlockTypeCache;

            if (blockType != null)
            {
                return(blockType);
            }
            else
            {
                Rock.Model.BlockTypeService blockTypeService = new Model.BlockTypeService();
                Rock.Model.BlockType        blockTypeModel   = blockTypeService.Get(id);
                if (blockTypeModel != null)
                {
                    blockType = new BlockTypeCache(blockTypeModel);

                    blockType.IsInstancePropertiesVerified = false;

                    blockTypeModel.LoadAttributes();

                    blockType.AttributeValues = blockTypeModel.AttributeValues;

                    if (blockTypeModel.Attributes != null)
                    {
                        foreach (var attribute in blockTypeModel.Attributes)
                        {
                            blockType.AttributeIds.Add(attribute.Value.Id);
                        }
                    }

                    // Block Type cache expiration monitors the actual block on the file system so that it is flushed from
                    // memory anytime the file contents change.  This is to force the cmsPage object to revalidate any
                    // BlockPropery attributes that may have been added or modified
                    string        physicalPath = System.Web.HttpContext.Current.Request.MapPath(blockType.Path);
                    List <string> filePaths    = new List <string>();
                    filePaths.Add(physicalPath);
                    filePaths.Add(physicalPath + ".cs");

                    CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
                    cacheItemPolicy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
                    cache.Set(cacheKey, blockType, cacheItemPolicy);

                    return(blockType);
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns Block Type object from cache.  If block does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static BlockTypeCache Read(int id, RockContext rockContext = null)
        {
            string cacheKey = BlockTypeCache.CacheKey(id);

            bool existing  = CacheContainsKey(cacheKey);
            var  blockType = GetOrAddExisting(cacheKey, () => LoadById(id, rockContext));

            if (blockType != null && !existing)
            {
                var cachePolicy = new CacheItemPolicy();
                AddChangeMonitor(cachePolicy, blockType.Path);

                SetCache(cacheKey, blockType, cachePolicy);
            }

            return(blockType);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns Block Type 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 BlockTypeCache Read(int id)
        {
            string cacheKey = BlockTypeCache.CacheKey(id);

            ObjectCache    cache     = MemoryCache.Default;
            BlockTypeCache blockType = cache[cacheKey] as BlockTypeCache;

            if (blockType != null)
            {
                return(blockType);
            }
            else
            {
                var blockTypeService = new BlockTypeService();
                var blockTypeModel   = blockTypeService.Get(id);
                if (blockTypeModel != null)
                {
                    blockTypeModel.LoadAttributes();
                    blockType = new BlockTypeCache(blockTypeModel);

                    var cachePolicy = new CacheItemPolicy();
                    AddChangeMonitor(cachePolicy, blockType.Path);
                    cache.Set(cacheKey, blockType, cachePolicy);

                    var guidCachePolicy = new CacheItemPolicy();
                    AddChangeMonitor(guidCachePolicy, blockType.Path);
                    cache.Set(blockType.Guid.ToString(), blockType.Id, guidCachePolicy);

                    return(blockType);
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Removes block type from cache
 /// </summary>
 /// <param name="id"></param>
 public static void Flush(int id)
 {
     FlushCache(BlockTypeCache.CacheKey(id));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Reads the specified block type model.
 /// </summary>
 /// <param name="blockTypeModel">The block type model.</param>
 /// <returns></returns>
 public static BlockTypeCache Read(BlockType blockTypeModel)
 {
     return(GetOrAddExisting(BlockTypeCache.CacheKey(blockTypeModel.Id),
                             () => LoadByModel(blockTypeModel)));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Removes block type from cache
        /// </summary>
        /// <param name="id"></param>
        public static void Flush(int id)
        {
            ObjectCache cache = MemoryCache.Default;

            cache.Remove(BlockTypeCache.CacheKey(id));
        }