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));
            }
        }
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。