/// <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 Block Read(int id) { string cacheKey = Block.CacheKey(id); ObjectCache cache = MemoryCache.Default; Block block = cache[cacheKey] as Block; if (block != null) { return(block); } else { Rock.CMS.BlockService blockService = new CMS.BlockService(); Rock.CMS.Block blockModel = blockService.Get(id); if (blockModel != null) { block = new Block(); block.Id = blockModel.Id; block.Path = blockModel.Path; block.Name = blockModel.Name; block.Description = blockModel.Description; block.InstancePropertiesVerified = false; Rock.Attribute.Helper.LoadAttributes(blockModel); block.AttributeValues = blockModel.AttributeValues; if (blockModel.Attributes != null) { foreach (var category in blockModel.Attributes) { foreach (var attribute in category.Value) { block.AttributeIds.Add(attribute.Id); } } } // Block 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 // BlockInstancePropery attributes that may have been added or modified string physicalPath = System.Web.HttpContext.Current.Request.MapPath(block.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, block, cacheItemPolicy); return(block); } else { return(null); } } }
/// <summary> /// Saves the attribute values. /// </summary> /// <param name="personId">The person id.</param> public void SaveAttributeValues(int?personId) { Rock.CMS.BlockService blockService = new CMS.BlockService(); Rock.CMS.Block blockModel = blockService.Get(this.Id); if (blockModel != null) { Rock.Attribute.Helper.LoadAttributes(blockModel); foreach (var category in blockModel.Attributes) { foreach (var attribute in category.Value) { Rock.Attribute.Helper.SaveAttributeValues(blockModel, attribute, this.AttributeValues[attribute.Key].Value, personId); } } } }
/// <summary> /// Saves the attribute values. /// </summary> /// <param name="personId">The person id.</param> public void SaveAttributeValues(int? personId) { Rock.CMS.BlockService blockService = new CMS.BlockService(); Rock.CMS.Block blockModel = blockService.Get( this.Id ); if ( blockModel != null ) { Rock.Attribute.Helper.LoadAttributes( blockModel ); foreach ( var category in blockModel.Attributes ) foreach ( var attribute in category.Value ) Rock.Attribute.Helper.SaveAttributeValues( blockModel, attribute, this.AttributeValues[attribute.Key].Value, personId ); } }
/// <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 Block Read( int id ) { string cacheKey = Block.CacheKey( id ); ObjectCache cache = MemoryCache.Default; Block block = cache[cacheKey] as Block; if ( block != null ) return block; else { Rock.CMS.BlockService blockService = new CMS.BlockService(); Rock.CMS.Block blockModel = blockService.Get( id ); if ( blockModel != null ) { block = new Block(); block.Id = blockModel.Id; block.Path = blockModel.Path; block.Name = blockModel.Name; block.Description = blockModel.Description; block.InstancePropertiesVerified = false; Rock.Attribute.Helper.LoadAttributes( blockModel ); block.AttributeValues = blockModel.AttributeValues; if (blockModel.Attributes != null) foreach ( var category in blockModel.Attributes ) foreach ( var attribute in category.Value ) block.AttributeIds.Add( attribute.Id ); // Block 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 // BlockInstancePropery attributes that may have been added or modified string physicalPath = System.Web.HttpContext.Current.Request.MapPath( block.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, block, cacheItemPolicy ); return block; } else return null; } }