LocalCache

 public static class LocalCacheHelper

    {

        private const int TimeOut = 5; //5分钟过期



        public static T GetCache<T>(string cacheKey)

        {

            var cache = GetCache(cacheKey);

            if (cache == null)

                return default(T);

            return (T)cache;

        }



        /// <summary>

        /// 获取当前应用程序指定CacheKey的Cache对象值

        /// </summary>

        /// <param name="cacheKey">索引键值</param>

        /// <returns>返回缓存对象</returns>

        private static object GetCache(string cacheKey)

        {

            var objCache = HttpRuntime.Cache;

            return objCache == null ? null : objCache[cacheKey];

        }



        /// <summary>

        /// 设置缓存数据

        /// </summary>

        /// <param name="cacheKey">索引键值</param>

        /// <param name="objObject">缓存对象</param>

        public static void SetCache(string cacheKey, object objObject)

        {

            try

            {

                var objCache = HttpRuntime.Cache;

                objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddMinutes(TimeOut), TimeSpan.Zero);

            }

            catch (Exception ex)

            {

                string message = string.Format("种本地缓存出错,有效信息为:cacheKey={0},objObject={1}", cacheKey, objObject.ToJson());

                CenteralLogManager.WriteException("种本地缓存出错", new Exception(message, ex));

            }

        }



        /// <summary>

        /// 设置缓存数据

        /// </summary>

        /// <param name="cacheKey">索引键值</param>

        /// <param name="objObject">缓存对象</param>

        /// <param name="minutes">缓存时间,单位:分钟</param>

        public static void SetCache(string cacheKey, object objObject, int minutes)

        {

            try

            {

                var objCache = HttpRuntime.Cache;

                objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);

            }

            catch (Exception ex)

            {

                string message = string.Format("种本地缓存出错,有效信息为:cacheKey={0},objObject={1}", cacheKey, objObject.ToJson());

                CenteralLogManager.WriteException("种本地缓存出错", new Exception(message, ex));

            }

        }

    }

 

你可能感兴趣的:(cache)