예제 #1
0
        /// <summary>
        /// 根据指定键获取或添加指定值。
        /// </summary>
        /// <typeparam name="TResult">缓存容器值的强类型表示。</typeparam>
        /// <param name="container">一个缓存容器接口的实例对象。</param>
        /// <param name="key">缓存项的键。</param>
        /// <param name="newValueFunc">获取失败后待添加的新值委托。</param>
        /// <returns>获取得到的缓存项的值。</returns>
        /// <exception cref="System.ArgumentNullException">container or key or
        /// newValueFunc</exception>
        public static TResult GetOrAddXWithFunc <TResult>(this ICacheContainer container, string key, Func <Tuple <TResult, Expiration> > newValueFunc)
        {
            if (ReferenceEquals(container, null))
            {
                throw new ArgumentNullException(string.Format("[ICacheContainerExtensions].[GetOrAddXWithFunc<{0}>].container", typeof(TResult).FullName));
            }
            if (key == null)
            {
                throw new ArgumentNullException(string.Format("[ICacheContainerExtensions].[GetOrAddXWithFunc<{0}>].key", typeof(TResult).FullName));
            }
            if (newValueFunc == null)
            {
                throw new ArgumentNullException(string.Format("[ICacheContainerExtensions].[GetOrAddXWithFunc<{0}>].newValueFunc", typeof(TResult).FullName));
            }

            object value = null;

            if (container.TryGetValue(key, out value))
            {
                return((TResult)value);
            }

            var newValue = newValueFunc();

            container.Update(key, newValue.Item1, newValue.Item2);
            return(newValue.Item1);
        }
예제 #2
0
        /// <summary>
        /// 根据指定键获取或添加指定值。
        /// </summary>
        /// <typeparam name="TResult">缓存容器值的强类型表示。</typeparam>
        /// <param name="container">一个缓存容器接口的实例对象。</param>
        /// <param name="key">缓存项的键。</param>
        /// <param name="newValue">获取失败后待添加的新值。</param>
        /// <param name="expiration">缓存项的过期设置信息:<c>null</c> 表示使用缺省的过期设置。</param>
        /// <returns>获取得到的缓存项的值。</returns>
        /// <exception cref="System.ArgumentNullException">container or key or newValue</exception>
        public static TResult GetOrAddX <TResult>(this ICacheContainer container, string key, TResult newValue, Expiration expiration = null)
        {
            if (ReferenceEquals(container, null))
            {
                throw new ArgumentNullException(string.Format("[ICacheContainerExtensions].[GetOrAddX<{0}>].container", typeof(TResult).FullName));
            }
            if (key == null)
            {
                throw new ArgumentNullException(string.Format("[ICacheContainerExtensions].[GetOrAddX<{0}>].key", typeof(TResult).FullName));
            }
            if (newValue == null)
            {
                throw new ArgumentNullException(string.Format("[ICacheContainerExtensions].[GetOrAddX<{0}>].newValue", typeof(TResult).FullName));
            }

            object value = null;

            if (container.TryGetValue(key, out value))
            {
                return((TResult)value);
            }

            container.Update(key, newValue, expiration);
            return(newValue);
        }
예제 #3
0
        /// <summary>
        /// 根据指定键获取或添加指定值。
        /// </summary>
        /// <param name="container">一个缓存容器接口的实例对象。</param>
        /// <param name="key">缓存项的键。</param>
        /// <param name="newValueFunc">获取失败后待添加的新值委托。</param>
        /// <returns>获取得到的缓存项的值。</returns>
        /// <exception cref="System.ArgumentNullException">container or key or
        /// newValueFunc</exception>
        public static object GetOrAddWithFunc(this ICacheContainer container, string key, Func <Tuple <object, Expiration> > newValueFunc)
        {
            if (ReferenceEquals(container, null))
            {
                throw new ArgumentNullException("[ICacheContainerExtensions].[GetOrAddWithFunc].container");
            }
            if (key == null)
            {
                throw new ArgumentNullException("[ICacheContainerExtensions].[GetOrAddWithFunc].key");
            }
            if (newValueFunc == null)
            {
                throw new ArgumentNullException("[ICacheContainerExtensions].[GetOrAddWithFunc].newValueFunc");
            }

            object value = null;

            if (container.TryGetValue(key, out value))
            {
                return(value);
            }

            var newValue = newValueFunc();

            container.Update(key, newValue.Item1, newValue.Item2);
            return(newValue.Item1);
        }
예제 #4
0
        /// <summary>
        /// 根据指定键获取或添加指定值。
        /// </summary>
        /// <param name="container">一个缓存容器接口的实例对象。</param>
        /// <param name="key">缓存项的键。</param>
        /// <param name="newValue">获取失败后待添加的新值。</param>
        /// <param name="expiration">缓存项的过期设置信息:<c>null</c> 表示使用缺省的过期设置。</param>
        /// <returns>获取得到的缓存项的值。</returns>
        /// <exception cref="System.ArgumentNullException">container or key or newValue</exception>
        public static object GetOrAdd(this ICacheContainer container, string key, object newValue, Expiration expiration = null)
        {
            if (ReferenceEquals(container, null))
            {
                throw new ArgumentNullException("[ICacheContainerExtensions].[GetOrAdd].container");
            }
            if (key == null)
            {
                throw new ArgumentNullException("[ICacheContainerExtensions].[GetOrAdd].key");
            }

            object value = null;

            if (container.TryGetValue(key, out value))
            {
                return(value);
            }

            container.Update(key, newValue, expiration);
            return(newValue);
        }