/// <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> /// Copies from model. /// </summary> /// <param name="model">The model.</param> public virtual void CopyFromModel(Rock.Data.IEntity model) { this.Id = model.Id; this.Guid = model.Guid; RockMemoryCache cache = RockMemoryCache.Default; cache.Set(model.Guid.ToString(), new Lazy <int>(() => AsLazy(model.Id)), new CacheItemPolicy()); }
/// <summary> /// Copies from model. /// </summary> /// <param name="model">The model.</param> public virtual void CopyFromModel(Rock.Data.IEntity model) { this.Id = model.Id; this.Guid = model.Guid; this.ForeignId = model.ForeignId; this.ForeignGuid = model.ForeignGuid; this.ForeignKey = model.ForeignKey; RockMemoryCache cache = RockMemoryCache.Default; cache.Set(model.Guid.ToString(), model.Id, new CacheItemPolicy()); }
/// <summary> /// Gets the or add existing. /// </summary> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> public static int GetOrAddExisting(string key, Func <int> valueFactory) { RockMemoryCache cache = RockMemoryCache.Default; object cacheValue = cache.Get(key); if (cacheValue != null) { return((int)cacheValue); } int value = valueFactory(); cache.Set(key, value, new CacheItemPolicy()); return(value); }
/// <summary> /// Gets the existing or a new item from cache /// </summary> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> public static GlobalAttributesCache GetOrAddExisting(string key, Func <GlobalAttributesCache> valueFactory) { var value = _cache.Get(key) as GlobalAttributesCache; if (value != null) { return(value); } value = valueFactory(); if (value != null) { _cache.Set(key, value, new CacheItemPolicy()); } return(value); }
/// <summary> /// Gets the or add all. /// </summary> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> public static List <int> GetOrAddAll(string key, Func <List <int> > valueFactory) { RockMemoryCache cache = RockMemoryCache.Default; var value = cache.Get(key) as List <int>; if (value != null) { return(value); } value = valueFactory(); if (value != null) { cache.Set(key, value, new CacheItemPolicy()); } return(value); }
/// <summary> /// Gets the existing or a new item from cache /// </summary> /// <typeparam name="TT">The type of the t.</typeparam> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> public static TT GetOrAddExisting <TT>(string key, Func <TT> valueFactory) { RockMemoryCache cache = RockMemoryCache.Default; object cacheValue = cache.Get(key); if (cacheValue != null) { return((TT)cacheValue); } TT value = valueFactory(); if (value != null) { cache.Set(key, value, new CacheItemPolicy()); } return(value); }
/// <summary> /// Gets the or add existing. /// </summary> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> public static LavaTemplateCache GetOrAddExisting(string key, Func <LavaTemplateCache> valueFactory) { RockMemoryCache cache = RockMemoryCache.Default; object cacheValue = cache.Get(key); if (cacheValue != null) { return((LavaTemplateCache)cacheValue); } LavaTemplateCache value = valueFactory(); var cacheItemPolicy = new CacheItemPolicy(); cacheItemPolicy.SlidingExpiration = new TimeSpan(0, 10, 0); cache.Set(key, value, cacheItemPolicy); return(value); }
/// <summary> /// Sets the cache. /// </summary> /// <param name="key">The key.</param> /// <param name="item">The item.</param> /// <param name="policy">The policy.</param> public static void SetCache(string key, object item, CacheItemPolicy policy) { RockMemoryCache cache = RockMemoryCache.Default; cache.Set(key, item, policy); }