AddOrGetExisting() 공개 메소드

Inserts a cache entry into the cache using the specified key and value and the specified details for how it is to be evicted.
public AddOrGetExisting ( string key, object value, System.Runtime.Caching.CacheItemPolicy policy, string regionName = null ) : object
key string A unique identifier for the cache entry to add or get.
value object The data for the cache entry.
policy System.Runtime.Caching.CacheItemPolicy An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.
regionName string A named region in the cache to which a cache entry can be added. Do not pass a value for this parameter. By default, this parameter is null, because the class does not implement regions.
리턴 object
예제 #1
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 newValue = new Lazy <GlobalAttributesCache>(valueFactory);
            var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy <GlobalAttributesCache>;

            try
            {
                return((oldValue ?? newValue).Value);
            }
            catch
            {
                _cache.Remove(key);
                throw;
            }
        }
예제 #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;

            var newValue = new Lazy <int>(valueFactory);
            var oldValue = cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy <int>;

            try
            {
                return((oldValue ?? newValue).Value);
            }
            catch
            {
                cache.Remove(key);
                throw;
            }
        }