/// <summary> /// Returns Global Attributes from cache. If they are not already in cache, they /// will be read and added to cache /// </summary> /// <returns></returns> public static GlobalAttributes Read() { string cacheKey = GlobalAttributes.CacheKey(); ObjectCache cache = MemoryCache.Default; GlobalAttributes globalAttributes = cache[cacheKey] as GlobalAttributes; if (globalAttributes != null) { return(globalAttributes); } else { globalAttributes = new GlobalAttributes(); globalAttributes.AttributeValues = new Dictionary <string, KeyValuePair <string, string> >(); var attributeService = new Rock.Core.AttributeService(); var attributeValueService = new Rock.Core.AttributeValueService(); foreach (Rock.Core.Attribute attribute in attributeService.Queryable(). Where(a => a.Entity == "" && (a.EntityQualifierColumn ?? string.Empty) == "" && (a.EntityQualifierValue ?? string.Empty) == "")) { // TODO: Need to add support for multiple values var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, null).FirstOrDefault(); globalAttributes.AttributeValues.Add(attribute.Key, new KeyValuePair <string, string>(attribute.Name, (attributeValue != null && !string.IsNullOrEmpty(attributeValue.Value)) ? attributeValue.Value : attribute.DefaultValue)); } cache.Set(cacheKey, globalAttributes, new CacheItemPolicy()); return(globalAttributes); } }
/// <summary> /// Returns Global Attributes from cache. If they are not already in cache, they /// will be read and added to cache /// </summary> /// <returns></returns> public static GlobalAttributes Read() { string cacheKey = GlobalAttributes.CacheKey(); ObjectCache cache = MemoryCache.Default; GlobalAttributes globalAttributes = cache[cacheKey] as GlobalAttributes; if ( globalAttributes != null ) return globalAttributes; else { globalAttributes = new GlobalAttributes(); globalAttributes.AttributeValues = new Dictionary<string, KeyValuePair<string, string>>(); var attributeService = new Rock.Core.AttributeService(); var attributeValueService = new Rock.Core.AttributeValueService(); foreach ( Rock.Core.Attribute attribute in attributeService.Queryable(). Where( a => a.Entity == "" && ( a.EntityQualifierColumn ?? string.Empty ) == "" && ( a.EntityQualifierValue ?? string.Empty ) == "" ) ) { // TODO: Need to add support for multiple values var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, null ).FirstOrDefault(); globalAttributes.AttributeValues.Add( attribute.Key, new KeyValuePair<string, string>( attribute.Name, (attributeValue != null && !string.IsNullOrEmpty(attributeValue.Value)) ? attributeValue.Value : attribute.DefaultValue ) ); } cache.Set( cacheKey, globalAttributes, new CacheItemPolicy() ); return globalAttributes; } }
/// <summary> /// Removes Global Attributes from cache /// </summary> public static void Flush() { ObjectCache cache = MemoryCache.Default; cache.Remove(GlobalAttributes.CacheKey()); }
/// <summary> /// Gets the Global Attribute values for the specified key. /// </summary> /// <param name="key">The key.</param> /// <returns></returns> public static string Value(string key) { GlobalAttributes globalAttributes = Read(); return(globalAttributes.AttributeValue(key)); }