• 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));
                }
            }
        }
  • 相关阅读:
    Html.ActionLink 几种重载方式说明及例子
    2 python 文本特征提取 CountVectorizer, TfidfVectorizer
    模型调参---GridSearchCV
    1 NLP学习大纲
    pandas.dropna/isnull/fillna/astype的用法
    激活函数总结
    LeetCode--53 最大连续子序列(总结)
    LeetCode--44 通配符匹配
    LeetCode--Two_Sum
    css 背景图片自适应元素大小
  • 原文地址:https://www.cnblogs.com/xffy1028/p/4494378.html
Copyright © 2020-2023  润新知