Beispiel #1
0
        /// <summary>
        /// Updates any Cache Objects that are associated with this entity
        /// </summary>
        /// <param name="entityState">State of the entity.</param>
        /// <param name="dbContext">The database context.</param>
        public void UpdateCache(EntityState entityState, Rock.Data.DbContext dbContext)
        {
            AttributeCache.UpdateCachedEntity(this.Id, entityState);
            AttributeCache.UpdateCacheEntityAttributes(this, entityState);

            int?   entityTypeId;
            string entityTypeQualifierColumn;
            string entityTypeQualifierValue;

            if (entityState == EntityState.Deleted)
            {
                entityTypeId = originalEntityTypeId;
                entityTypeQualifierColumn = originalEntityTypeQualifierColumn;
                entityTypeQualifierValue  = originalEntityTypeQualifierValue;
            }
            else
            {
                entityTypeId = this.EntityTypeId;
                entityTypeQualifierColumn = this.EntityTypeQualifierColumn;
                entityTypeQualifierValue  = this.EntityTypeQualifierValue;
            }

            if ((!entityTypeId.HasValue || entityTypeId.Value == 0) && string.IsNullOrEmpty(entityTypeQualifierColumn) && string.IsNullOrEmpty(entityTypeQualifierValue))
            {
                GlobalAttributesCache.Remove();
            }

            if ((!entityTypeId.HasValue || entityTypeId.Value == 0) && entityTypeQualifierColumn == Attribute.SYSTEM_SETTING_QUALIFIER && string.IsNullOrEmpty(entityTypeQualifierValue))
            {
                Rock.Web.SystemSettings.Remove();
            }

            if (entityTypeId.HasValue)
            {
                if (entityTypeId == EntityTypeCache.GetId <Block>())
                {
                    // Update BlockTypes/Blocks that reference this attribute
                    if (entityTypeQualifierColumn.Equals("BlockTypeId", StringComparison.OrdinalIgnoreCase))
                    {
                        int?blockTypeId = entityTypeQualifierValue.AsIntegerOrNull();
                        if (blockTypeId.HasValue)
                        {
                            BlockTypeCache.FlushItem(blockTypeId.Value);

                            foreach (var blockId in new BlockService(dbContext as RockContext).GetByBlockTypeId(blockTypeId.Value).Select(a => a.Id).ToList())
                            {
                                BlockCache.FlushItem(blockId);
                            }
                        }
                    }
                }
                else if (entityTypeId == EntityTypeCache.GetId <DefinedValue>())
                {
                    // Update DefinedTypes/DefinedValues that reference this attribute
                    if (entityTypeQualifierColumn.Equals("DefinedTypeId", StringComparison.OrdinalIgnoreCase))
                    {
                        int?definedTypeId = entityTypeQualifierValue.AsIntegerOrNull();
                        if (definedTypeId.HasValue)
                        {
                            DefinedTypeCache.FlushItem(definedTypeId.Value);

                            foreach (var definedValueId in new DefinedValueService(dbContext as RockContext).GetByDefinedTypeId(definedTypeId.Value).Select(a => a.Id).ToList())
                            {
                                DefinedValueCache.FlushItem(definedValueId);
                            }
                        }
                    }
                }
                else if (entityTypeId == EntityTypeCache.GetId <WorkflowActivityType>())
                {
                    if (entityTypeQualifierColumn.Equals("ActivityTypeId", StringComparison.OrdinalIgnoreCase))
                    {
                        int?activityTypeId = entityTypeQualifierValue.AsIntegerOrNull();
                        if (activityTypeId.HasValue)
                        {
                            WorkflowActivityTypeCache.FlushItem(activityTypeId.Value);
                        }
                    }
                }
                else if (entityTypeId == EntityTypeCache.GetId <GroupType>())
                {
                    if (entityTypeQualifierColumn.Equals("Id", StringComparison.OrdinalIgnoreCase))
                    {
                        int?groupTypeId = entityTypeQualifierValue.AsIntegerOrNull();
                        if (groupTypeId.HasValue)
                        {
                            GroupTypeCache.FlushItem(groupTypeId.Value);
                        }
                    }
                    else if (entityTypeQualifierColumn.Equals("GroupTypePurposeValueId", StringComparison.OrdinalIgnoreCase))
                    {
                        int?groupTypePurposeValueId = entityTypeQualifierValue.AsIntegerOrNull();
                        if (groupTypePurposeValueId.HasValue)
                        {
                            foreach (var groupTypeId in GroupTypeCache.All().Where(a => a.GroupTypePurposeValueId == groupTypePurposeValueId.Value).Select(a => a.Id).ToList())
                            {
                                GroupTypeCache.FlushItem(groupTypeId);
                            }
                        }
                    }
                }
                else if (entityTypeId.HasValue)
                {
                    // some other EntityType. If it the EntityType has a CacheItem associated with it, clear out all the CachedItems of that type to ensure they have a clean read of the Attributes that were Added, Changed or Removed
                    EntityTypeCache entityType = EntityTypeCache.Get(entityTypeId.Value, dbContext as RockContext);

                    if (entityType?.HasEntityCache() == true)
                    {
                        entityType.ClearCachedItems();
                    }
                }
            }
        }