/// <summary> /// Gets the or add existing. /// </summary> /// <remarks> /// Because ComponentCacheDuration is determined by the channel, this class needs it's own GetOrAddExisting /// method. /// </remarks> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> private static InteractionComponentCache GetOrAddExisting(string key, Func <InteractionComponentCache> valueFactory) { RockMemoryCache cache = RockMemoryCache.Default; InteractionComponentCache cacheValue = cache.Get(key) as InteractionComponentCache; if (cacheValue != null) { return(cacheValue); } InteractionComponentCache value = valueFactory(); if (value != null) { // Because the cache policy for interaction components is defined on the channel, get the channel. int?cacheDuration = null; var channel = InteractionChannelCache.Read(value.ChannelId); if (channel != null) { cacheDuration = channel.ComponentCacheDuration; } if (!cacheDuration.HasValue || cacheDuration.Value > 0) { var cacheItemPolicy = new CacheItemPolicy(); if (cacheDuration.HasValue) { cacheItemPolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(cacheDuration.Value); } cache.Set(key, value, cacheItemPolicy); } } return(value); }
/// <summary> /// Removes interactionChannel from cache /// </summary> /// <param name="id"></param> public static void Flush(int id) { FlushCache(InteractionChannelCache.CacheKey(id)); }
/// <summary> /// Reads the specified model. /// </summary> /// <param name="interactionChannelModel">The model.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> public static InteractionChannelCache Read(InteractionChannel interactionChannelModel, RockContext rockContext = null) { return(GetOrAddExisting(InteractionChannelCache.CacheKey(interactionChannelModel.Id), () => LoadByModel(interactionChannelModel))); }
/// <summary> /// Returns InteractionChannel object from cache. If interactionChannel 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 InteractionChannelCache Read(int id, RockContext rockContext = null) { return(GetOrAddExisting(InteractionChannelCache.CacheKey(id), () => LoadById(id, rockContext))); }