/// <summary>
        /// Helper method to more easily create an Absolute Expiration CacheItemPolicy directly from a Configuration
        /// Parameter name that has the TTL Seconds; with Default fallback if it does not exist.
        /// </summary>
        /// <param name="ttlSecondsConfigKey"></param>
        /// <param name="callbackWhenCacheEntryRemoved"></param>
        /// <returns></returns>
        public static CacheItemPolicy NewAbsoluteExpirationPolicy(string ttlSecondsConfigKey, Action <CacheEntryRemovedArguments> callbackWhenCacheEntryRemoved = null)
        {
            var timeSpanToLive = LazyCacheConfig.GetCacheTTLFromConfig(ttlSecondsConfigKey, LazyCacheConfig.NeverCacheTTL);

            if (timeSpanToLive.TotalMilliseconds > 0)
            {
                return(LazyCachePolicy.NewAbsoluteExpirationPolicy(timeSpanToLive, callbackWhenCacheEntryRemoved));
            }

            return(null);
        }
        /// <summary>
        /// Helper method to more easily create an Absolute Expiration CacheItemPolicy directly from a Configuration
        /// Parameter names that has the TTL Seconds; this will return the first identified valid configuration key.
        /// </summary>
        /// <param name="ttlConfigKeysToSearch"></param>
        /// <param name="callbackWhenCacheEntryRemoved"></param>
        /// <returns></returns>
        public static CacheItemPolicy NewAbsoluteExpirationPolicy(string[] ttlConfigKeysToSearch, Action <CacheEntryRemovedArguments> callbackWhenCacheEntryRemoved = null)
        {
            var timeSpanToLive = LazyCacheConfig.GetCacheTTLFromConfig(ttlConfigKeysToSearch, LazyCacheConfig.NeverCacheTTL);

            if (timeSpanToLive.TotalMilliseconds > 0)
            {
                return(LazyCachePolicy.NewAbsoluteExpirationPolicy(timeSpanToLive, callbackWhenCacheEntryRemoved));
            }

            //Finally return the cache policy for the first identified valid configuration key.
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the Cache TTL Seconds from Configuration in a fully ThreadSafe way by finding the configuration value
        /// of the first valid config key specified; searching in the order defined int the array.
        /// </summary>
        /// <param name="configKeysToSearch"></param>
        /// <param name="defaultMinimumTTL"></param>
        /// <returns></returns>
        public static TimeSpan GetCacheTTLFromConfig(string[] configKeysToSearch, TimeSpan defaultMinimumTTL)
        {
            foreach (var configKey in configKeysToSearch)
            {
                var timeSpanToLive = LazyCacheConfig.GetCacheTTLFromConfig(configKey, LazyCacheConfig.MissingCacheTTL);
                if (timeSpanToLive == NeverCacheTTL || timeSpanToLive.TotalMilliseconds > 0)
                {
                    return(timeSpanToLive);
                }
            }

            return(defaultMinimumTTL);
        }