Ejemplo n.º 1
0
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        public void Set(object key, object data)
        {
            ArgumentAssertion.IsNotNull(key, "key");
            var cacheKey = key.ToString();

            this.Set(cacheKey, data);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get <T>(object key)
        {
            ArgumentAssertion.IsNotNull(key, "oKey");
            var cacheKey = key.ToString();
            var val      = this.Get(cacheKey);

            if (val == null)
            {
                return(default(T));
            }
            else
            {
                return((T)val);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 从缓存获取数据,如缓存中没有指定数据,则通过loadEntity(key)获取数据,并将数据缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="loadEntity"></param>
        /// <returns></returns>
        public static T LoadEntityAndCache <T>(this ICache cache, string key, Func <T> loadEntity)
        {
            ArgumentAssertion.IsNotNull(cache, "cache");
            ArgumentAssertion.IsNotNull(loadEntity, "loadEntity");

            var entity = cache.Get <T>(key);

            if (null == entity)
            {
                entity = loadEntity();
                if (null != entity)
                {
                    cache.Set(key, entity);
                }
            }
            return(entity);
        }