/// <summary> /// Gets an item from cache, and if not found, executes the itemFactory to create item and add to cache with an expiration timespan. /// </summary> /// <param name="key">The key.</param> /// <param name="region">The region.</param> /// <param name="itemFactory">The item factory.</param> /// <param name="expiration">The expiration.</param> /// <returns></returns> public static object GetOrAddExisting(string key, string region, Func <object> itemFactory, TimeSpan expiration) { var args = new RockCacheGetOrAddExistingArgs { Key = key, Region = region, ItemFactory = itemFactory, Expiration = expiration, AllowCacheBypass = false }; return(GetOrAddExisting(args)); }
/// <summary> /// Gets an item from cache using the specified key. If allowCacheBypass is true the CACHE_CONTROL_COOKIE will be /// inspected to see if cached value should be ignored. /// </summary> /// <param name="key">The key.</param> /// <param name="allowCacheBypass">if set to <c>true</c> the cache can be ignored based on the cache control cookie.</param> /// <returns></returns> public static object Get(string key, bool allowCacheBypass) { var args = new RockCacheGetOrAddExistingArgs { Key = key, Region = null, ItemFactory = null, Expiration = TimeSpan.MaxValue, AllowCacheBypass = allowCacheBypass }; return(GetOrAddExisting(args)); }
/// <summary> /// Gets or adds an item from cache using the specified args. If allowCacheBypass is true the CACHE_CONTROL_COOKIE will be /// inspected to see if cached value should be ignored. /// </summary> /// <param name="args">The arguments.</param> /// <returns></returns> public static object GetOrAddExisting(RockCacheGetOrAddExistingArgs args) { if (args.AllowCacheBypass && System.Web.HttpContext.Current != null) { var isCachedEnabled = System.Web.HttpContext.Current.Request.Cookies.Get(CACHE_CONTROL_COOKIE); if (isCachedEnabled != null && !isCachedEnabled.Value.AsBoolean()) { return(null); } } var value = args.Region.IsNotNullOrWhiteSpace() ? RockCacheManager <object> .Instance.Get(args.Key, args.Region) : RockCacheManager <object> .Instance.Get(args.Key); if (value != null) { return(value); } if (args.ItemFactory == null) { return(null); } value = args.ItemFactory(); if (value == null) { return(null); } if (args.Region.IsNotNullOrWhiteSpace()) { RockCacheManager <object> .Instance.AddOrUpdate(args.Key, args.Region, value, args.Expiration); } else { RockCacheManager <object> .Instance.AddOrUpdate(args.Key, value, args.Expiration); } return(value); }