Exemple #1
0
        /// <summary>
        /// 在Cache中读取Cache项,如果不存在,则调用action
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="action">不存在时的回调</param>
        /// <returns>Cache项的值</returns>
        public TValue GetOrAddNewValue(TKey key, CacheItemNotExistsAction action)
        {
            TValue result = default(TValue);

            if (TryGetValue(key, out result) == false)
            {
                lock (this.syncRoot)
                {
                    if (TryGetValue(key, out result) == false)
                    {
                        result = action(this, key);
                    }
                }
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// 在Cache中读取Cache项,如果不存在,则调用action
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="action">不存在时的回调</param>
        /// <returns>Cache项的值</returns>
        public TValue GetOrAddNewValue(TKey key, CacheItemNotExistsAction action)
        {
            key = ConvertCacheKey(key);

            TValue result;

            if (this.InnerTryGetValue(key, out result) == false)
            {
                lock (this.syncRoot)
                {
                    if (this.InnerTryGetValue(key, out result) == false)
                    {
                        result = action(this, key);
                    }
                }
            }

            return(result);
        }