AppSetting配置工具类
<?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配置工具类的更多相关文章
- 关于TornadoFx和Android的全局配置工具类封装实现及思路解析
原文地址: 关于TornadoFx和Android的全局配置工具类封装实现及思路解析 - Stars-One的杂货小窝 目前个人开发软件存在设置页面,可以让用户自定义些设置,但我发现,存储数据的代码逻 ...
- Python配置工具类ConfigParser使用
ConfigParser模块定义了类ConfigParser,用于实现配置文件解释器.该模块ConfigParser在Python3中,已更名为configparser. 一,函数介绍 1.读取配置文 ...
- 适用于app.config与web.config的ConfigUtil读写工具类
之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...
- 适用于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通用)>,现在重新整理一 ...
- Python数据库工具类MySQLdb使用
MySQLdb模块用于连接mysql数据库. 基本操作 # -*- coding: utf-8 -*- #mysqldb import time, MySQLdb ...
- Hibernate 工具类
1.HibernateConfigUtil.java(HIbernate配置工具类) import org.hibernate.Session; import org.hibernate.Sessio ...
- Python 之configparser读取配置操作类
一.为什么要封装 我们为什么要封装,我相信你们在项目开发过程中深有体会,那么这个读取配置工具类,又是为了什么? 为了项目参数配置的灵活性,不要改动到源码 为了信息的安全(一定层面的),体现代码重用性 ...
- RedisUtil,Redis工具类
RedisUtil,Redis工具类 1.配置maven,增加依赖 2.配置工具类 1.配置maven,增加依赖 <dependency> <groupId>redis.cli ...
- CYQ.Data.Orm.DBFast 新增类介绍(含类的源码及新版本配置工具源码)
前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...
随机推荐
- 如何突破DNS报文的512字节限制
- DNS的512字节限制 根据协议标准,DNS协议同时占用UDP和TCP的53端口,这是为什么呢? 翻阅DNS资料,可以发现,DNS协议默认按UDP传输,为优化传输性能,DNS协议有一个512字节的 ...
- vscode使用formate格式化less遇到的坑
就是这个家伙 我的代码 @input-padding-y : 8px;@input-padding-x : 12px; @input-padding-y-lg : @input-padding-y + ...
- Web服务器—Apache
Apache配置文件:httpd.conf文件 # 指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入. ServerRoot "/www/ser ...
- Web服务器—Nginx
Nginx常用命令: 启动nginx服务 [root@localhost ~]# service nginx start [root@localhost ~]# systemctl start ngi ...
- 使用Socket下载图片
Socket下载一张图片 Socket下载一页图片 Socket下载一张图片 在百度搜索头像,挑一张 复制图片的路径打开,并保存这个url 把这个url的域名和路径分开 首先导入socket模块 第一 ...
- HTML&CSS基础-子元素和后代元素选择器
HTML&CSS基础-子元素和后代元素选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!DOCTYPE html> <html ...
- 第十七章 Metasploit Framework
渗透测试者的困扰▪ 需要掌握数百个工具软件,上千个命令参数,实在记不住▪ 新出现的漏洞PoC/EXP有不同的运行环境要求,准备工作繁琐▪ 大部分时间都在学习使用不同工具的使用习惯,如果能统一就好了▪ ...
- Linux 修改/etc/sudoers 可被任何修改,如何解决
今天不小心,修改了/etc/sudoers的权限 改成了 777的权限, 于是每次使用sudo都会弹出 sudo:sudo /etc/sudoers is world writable sudo:no ...
- office 小技巧
1. 数据匹配 VLOOKUP 例一:如图,将左表(sheet1)的e列关联到右表(sheet2),条件为: sheet2.字段名 = sheet1.D 公式:=VLOOKUP(sheet2!D2:D ...
- eclipse git pull 代码 failed 并且报DIRTY_WORKTREE.classpath
用eclipse git pull代码的时候出现如题错误. 解决办法就是reset reset命令有3种方式: 1.git reset –mixed:此为默认方式,不带任何参数的git reset,即 ...