Get() public méthode

Returns an entry from the cache.
public Get ( string key, string regionName = null ) : object
key string A unique identifier for the cache entry to get.
regionName string A named region in the cache to which a cache entry was added. Do not pass a value for this parameter. This parameter is null by default, because the class does not implement regions.
Résultat object
Exemple #1
0
        /// <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);
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        /// <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);
        }
Exemple #5
0
        /// <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);
        }
Exemple #6
0
        /// <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);
        }