zoukankan      html  css  js  c++  java
  • 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));
                }
            }
        }
  • 相关阅读:
    数据切分——Atlas介绍
    HDU 5015 233Matrix (构造矩阵)
    Wincc操作数据库SQLSERVER
    UIWebView 设置背景为透明
    29个你必须知道的Linux命令
    【读书笔记】iOS-UIWindow-WindowLevel
    linux下uart应用编程
    Java Web HelloWorld!
    手把手图文教你eclipse下如何配置tomcat
    Tomcat安装及配置教程
  • 原文地址:https://www.cnblogs.com/xffy1028/p/4494378.html
Copyright © 2011-2022 走看看