zoukankan      html  css  js  c++  java
  • AppSetting配置工具类

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <!--
     3   有关如何配置 ASP.NET 应用程序的详细信息,请访问
     4   http://go.microsoft.com/fwlink/?LinkId=169433
     5   -->
     6 <configuration>
     7   <appSettings>
     8     <add key="key1" value="value1"/>
     9   </appSettings>
    10 </configuration>
    web.config示例

    一、缓存类

     1 using System;
     2 using System.Collections;
     3 using System.Configuration;
     4 using System.Web;
     5 using System.Web.Caching;
     6 
     7 namespace Common
     8 {
     9     /// <summary>
    10     /// 缓存帮助类
    11     /// author:陈彦斌
    12     /// 时间:2019年7月14日14:25:30
    13     /// HttpRuntime.Cache
    14     /// </summary>
    15     public sealed class CacheHelper
    16     {
    17         /// <summary>
    18         /// 获取configuratio节点下appSettings中add的值
    19         /// </summary>
    20         /// <param name="key">AppSettings的键</param>
    21         /// <returns></returns>
    22         public static object GetAppSttings(string key)
    23         {
    24             return ConfigurationManager.AppSettings[key];
    25         }
    26         /// <summary>
    27         /// 获取当前应用程序指定CacheKey的值
    28         /// </summary>
    29         /// <param name="CacheKey">appSettings节点下add中的键</param>
    30         /// <returns></returns>
    31         public static object GetCache(string CacheKey)
    32         {
    33             Cache objCache = HttpRuntime.Cache;
    34             return objCache[CacheKey];
    35         }
    36         /// <summary>
    37         /// 设置数据缓存(慎用)
    38         /// </summary>
    39         /// <param name="CacheKey"></param>
    40         /// <param name="CacheValue"></param>
    41         public static void SetCache(string CacheKey,object CacheValue)
    42         {
    43             Cache objCache = HttpRuntime.Cache;
    44             objCache.Insert(CacheKey, CacheValue);
    45         }
    46         /// <summary>
    47         /// 设置数据缓存
    48         /// </summary>
    49         /// <param name="CacheKey"></param>
    50         /// <param name="CacheValue"></param>
    51         /// <param name="TimeOut">时间间隔</param>
    52         public static void SetCache(string CacheKey, object CacheValue, TimeSpan TimeOut)
    53         {
    54             Cache objCache = HttpRuntime.Cache;
    55             objCache.Insert(CacheKey, CacheValue, null, DateTime.MaxValue, TimeOut, CacheItemPriority.NotRemovable, null);
    56         }
    57         /// <summary>
    58         /// 设置数据缓存
    59         /// </summary>
    60         /// <param name="CacheKey"></param>
    61         /// <param name="CacheValue"></param>
    62         /// <param name="absoluteExpiration">绝对过期时间</param>
    63         /// <param name="slidingExpiration">时间间隔</param>
    64         public static void SetCache(string CacheKey, object CacheValue, DateTime absoluteExpiration, TimeSpan slidingExpiration)
    65         {
    66             Cache objCache = HttpRuntime.Cache;
    67             objCache.Insert(CacheKey, CacheValue, null, absoluteExpiration, slidingExpiration);
    68         }
    69         /// <summary>
    70         /// 移除全部缓存
    71         /// </summary>
    72         public static void RemovaAllCache()
    73         {
    74             Cache objCache = HttpRuntime.Cache;
    75             IDictionaryEnumerator CacheEnum = objCache.GetEnumerator();
    76             while (CacheEnum.MoveNext())
    77             {
    78                 objCache.Remove(CacheEnum.Key.ToString());
    79             }
    80         }
    81         /// <summary>
    82         /// 移除指定键的缓存
    83         /// </summary>
    84         /// <param name="CacheKey"></param>
    85         public static void RemovaAllCache(string CacheKey)
    86         {
    87             Cache objCache = HttpRuntime.Cache;
    88             objCache.Remove(CacheKey);
    89         }
    90     }
    91 }

    二、AppSetting配置类

     1 using System;
     2 
     3 namespace Common
     4 {
     5     /// <summary>
     6     /// web.config操作类
     7     /// 使用前需引用程序集:System.configuration
     8     /// </summary>
     9     public sealed class ConfigHelper
    10     {
    11         /// <summary>
    12         /// 获取AppSettings中配置String信息
    13         /// </summary>
    14         /// <param name="key"></param>
    15         /// <returns></returns>
    16         public static string GetConfigString(string key)
    17         {
    18             object objValue = CacheHelper.GetCache(key);
    19             if (objValue == null) //缓冲区没有值
    20             {
    21                 objValue = CacheHelper.GetAppSttings(key);
    22                 if (objValue != null)
    23                 {
    24                     CacheHelper.SetCache(key, objValue, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
    25                 }
    26             }
    27             return objValue.ToString();
    28         }
    29         /// <summary>
    30         /// 获取AppSettings中配置Bool信息
    31         /// </summary>
    32         /// <param name="key"></param>
    33         /// <returns></returns>
    34         public static bool GetConfigBool(string key)
    35         {
    36             object objValue= CacheHelper.GetAppSttings(key);
    37             if (StringUtil.isNullOrBlank(objValue))
    38             {
    39                 try
    40                 {
    41                     bool.Parse(objValue.ToString());
    42                     return true;
    43                 }
    44                 catch
    45                 {
    46                     return false;
    47                 }
    48             }
    49             return false;
    50         }
    51         /// <summary>
    52         /// 获取AppSettings中配置decimal信息
    53         /// </summary>
    54         /// <param name="key"></param>
    55         /// <returns></returns>
    56         public static decimal GetConfigDecimal(string key)
    57         {
    58             object objValue = CacheHelper.GetAppSttings(key);
    59             if (StringUtil.isNullOrBlank(objValue))
    60             {
    61                 try
    62                 {                    
    63                     return decimal.Parse(objValue.ToString());
    64                 }
    65                 catch
    66                 {
    67                     return 0;
    68                 }
    69             }
    70             return 0;
    71         }
    72         /// <summary>
    73         /// 获取AppSettings中配置DateTime信息,可空
    74         /// </summary>
    75         /// <param name="key"></param>
    76         /// <returns></returns>
    77         public static DateTime? GetConfigDateTime(string key)
    78         {
    79             DateTime? DateTimeNull = null;
    80             object objValue = CacheHelper.GetAppSttings(key);
    81             if (StringUtil.isNullOrBlank(objValue))
    82             {
    83                 try
    84                 {
    85                     return DateTime.Parse(objValue.ToString());
    86                 }
    87                 catch
    88                 {
    89                     return DateTimeNull;
    90                 }
    91             }
    92             return DateTimeNull;
    93         }
    94     }
    95 }
  • 相关阅读:
    【Leetcode】23. Merge k Sorted Lists
    【Leetcode】109. Convert Sorted List to Binary Search Tree
    【Leetcode】142.Linked List Cycle II
    【Leetcode】143. Reorder List
    【Leetcode】147. Insertion Sort List
    【Leetcode】86. Partition List
    jenkins 配置安全邮件
    python 发送安全邮件
    phpstorm 同步远程服务器代码
    phpUnit 断言
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/11184405.html
Copyright © 2011-2022 走看看