<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="key1" value="value1"/>
</appSettings>
</configuration>

web.config示例

一、缓存类

 using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Caching; namespace Common
{
/// <summary>
/// 缓存帮助类
/// author:陈彦斌
/// 时间:2019年7月14日14:25:30
/// HttpRuntime.Cache
/// </summary>
public sealed class CacheHelper
{
/// <summary>
/// 获取configuratio节点下appSettings中add的值
/// </summary>
/// <param name="key">AppSettings的键</param>
/// <returns></returns>
public static object GetAppSttings(string key)
{
return ConfigurationManager.AppSettings[key];
}
/// <summary>
/// 获取当前应用程序指定CacheKey的值
/// </summary>
/// <param name="CacheKey">appSettings节点下add中的键</param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置数据缓存(慎用)
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="CacheValue">值</param>
public static void SetCache(string CacheKey,object CacheValue)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, CacheValue);
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="CacheValue">值</param>
/// <param name="TimeOut">时间间隔</param>
public static void SetCache(string CacheKey, object CacheValue, TimeSpan TimeOut)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, CacheValue, null, DateTime.MaxValue, TimeOut, CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="CacheValue">值</param>
/// <param name="absoluteExpiration">绝对过期时间</param>
/// <param name="slidingExpiration">时间间隔</param>
public static void SetCache(string CacheKey, object CacheValue, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, CacheValue, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemovaAllCache()
{
Cache objCache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = objCache.GetEnumerator();
while (CacheEnum.MoveNext())
{
objCache.Remove(CacheEnum.Key.ToString());
}
}
/// <summary>
/// 移除指定键的缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static void RemovaAllCache(string CacheKey)
{
Cache objCache = HttpRuntime.Cache;
objCache.Remove(CacheKey);
}
}
}

二、AppSetting配置类

 using System;

 namespace Common
{
/// <summary>
/// web.config操作类
/// 使用前需引用程序集:System.configuration
/// </summary>
public sealed class ConfigHelper
{
/// <summary>
/// 获取AppSettings中配置String信息
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static string GetConfigString(string key)
{
object objValue = CacheHelper.GetCache(key);
if (objValue == null) //缓冲区没有值
{
objValue = CacheHelper.GetAppSttings(key);
if (objValue != null)
{
CacheHelper.SetCache(key, objValue, DateTime.Now.AddMinutes(), TimeSpan.Zero);
}
}
return objValue.ToString();
}
/// <summary>
/// 获取AppSettings中配置Bool信息
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static bool GetConfigBool(string key)
{
object objValue= CacheHelper.GetAppSttings(key);
if (StringUtil.isNullOrBlank(objValue))
{
try
{
bool.Parse(objValue.ToString());
return true;
}
catch
{
return false;
}
}
return false;
}
/// <summary>
/// 获取AppSettings中配置decimal信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static decimal GetConfigDecimal(string key)
{
object objValue = CacheHelper.GetAppSttings(key);
if (StringUtil.isNullOrBlank(objValue))
{
try
{
return decimal.Parse(objValue.ToString());
}
catch
{
return ;
}
}
return ;
}
/// <summary>
/// 获取AppSettings中配置DateTime信息,可空
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static DateTime? GetConfigDateTime(string key)
{
DateTime? DateTimeNull = null;
object objValue = CacheHelper.GetAppSttings(key);
if (StringUtil.isNullOrBlank(objValue))
{
try
{
return DateTime.Parse(objValue.ToString());
}
catch
{
return DateTimeNull;
}
}
return DateTimeNull;
}
}
}

AppSetting配置工具类的更多相关文章

  1. 关于TornadoFx和Android的全局配置工具类封装实现及思路解析

    原文地址: 关于TornadoFx和Android的全局配置工具类封装实现及思路解析 - Stars-One的杂货小窝 目前个人开发软件存在设置页面,可以让用户自定义些设置,但我发现,存储数据的代码逻 ...

  2. Python配置工具类ConfigParser使用

    ConfigParser模块定义了类ConfigParser,用于实现配置文件解释器.该模块ConfigParser在Python3中,已更名为configparser. 一,函数介绍 1.读取配置文 ...

  3. 适用于app.config与web.config的ConfigUtil读写工具类

    之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...

  4. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  5. Python数据库工具类MySQLdb使用

    MySQLdb模块用于连接mysql数据库. 基本操作 # -*- coding: utf-8 -*-       #mysqldb       import time, MySQLdb       ...

  6. Hibernate 工具类

    1.HibernateConfigUtil.java(HIbernate配置工具类) import org.hibernate.Session; import org.hibernate.Sessio ...

  7. Python 之configparser读取配置操作类

    一.为什么要封装 我们为什么要封装,我相信你们在项目开发过程中深有体会,那么这个读取配置工具类,又是为了什么? 为了项目参数配置的灵活性,不要改动到源码 为了信息的安全(一定层面的),体现代码重用性 ...

  8. RedisUtil,Redis工具类

    RedisUtil,Redis工具类 1.配置maven,增加依赖 2.配置工具类 1.配置maven,增加依赖 <dependency> <groupId>redis.cli ...

  9. CYQ.Data.Orm.DBFast 新增类介绍(含类的源码及新版本配置工具源码)

    前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...

随机推荐

  1. CSS元素显示模式

    CSS的元素显示模式 什么是元素显示模式 作用:网页的标签非常多,在不同的地方会用到不同类型的标签,了解他们的特点可以更好的布局我们的网页 元素的显示模式就是元素(标签)以什么样的方式进行显示,比如& ...

  2. Wireshark使用教程:不同报文颜色的含义

    - 设置 色彩规则有两个入口,一个在报文上方的工具栏内,如图: 那个鲜艳的图标就是色彩规则的入口. 另一个是view-->coloring rules菜单. 点击进去即可看见所有的色彩规则的设置 ...

  3. wpf 的dispatcher

    wpf项目中后台代码调用界面控件时,会提示进程调用的错误. private Thread JxThread = null;  //定义线程 private DataLoading.Loading nL ...

  4. 错误:shell 打开出现一大堆 错误 declare -x 之类的消息

    像图中这种情况:这是什么情况呢? 原因:可能是你最近修改了.bashrc 或者 bash_profile 之类的文件.其中export 命令,要求export 命令写在单独的一行上: 就像下面这样,如 ...

  5. liteos MMU(十八)

    1. 概述 1.1 基本概念 MMU全称"Memory Management Unit",顾名思义就是"内存管理单元". 1.2 运作机制 建立页表描述符号表, ...

  6. golang中的struct标签tag

    这个以前用beego时涉及过,时间久了,就忘了. 现在k8s里的controller,一样用了这个语法, 再拾起来吧. http://www.01happy.com/golang-struct-tag ...

  7. ACM-ICPC 2018 南京网络赛

    题目顺序:A C E G I J L A. An Olympian Math Problem 打表,找规律,发现答案为n-1 C. GDY 题意: m张卡片,标号1-13: n个玩家,标号1-n:每个 ...

  8. /usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child

    https://www.jb51.net/article/142787.htm gn gn  问题如何解决?????

  9. 无法添加符号: 归档没有索引;运行 ranlib 以添加一个

    这将告诉您对象文件的格式.如果对象文件是针对不同的平台编译的,则会导致无法为存档创建索引.要纠正这种情况,您需要重新编译这些文件.

  10. Python的运用基础3

    1. 简述执行Python程序的两种方式以及他们的优缺点? 交互式(jupyter) 优点:运行一句执行一句 缺点:关闭即消失 ==例如== win10系统cmd窗口 命令行式(Pycharm) 优点 ...