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 新增类介绍(含类的源码及新版本配置工具源码)
前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...
随机推荐
- 函数的防抖---js
执行规定一段时间后执行 <input type="text" id="inp" /> <script> var oInp = docum ...
- 安卓开发笔记(三十四):Material Design框架实现优美的左侧侧滑栏
首先我们先上图: 下面是主页面的代码,activity_main.xml: <?xml version="1.0" encoding="utf-8"?& ...
- Android native进程间通信实例-binder篇之——解决实际问题inputreader内建类清楚缓存
我在实际开发中,遇到一个问题,在电容屏驱动中没有发送input_sync 给上层,导致电容屏有的数据缓存在inputreader 中,会导致系统一系列奇怪问题发生, 至于为什么驱动不发送input_s ...
- Xamarin.Forms iOS 真机测试 打包
等着打包过程中记录一下如何打一个debug包到真机上测试的流程1. 需要在XCode中创建一个新的项目,选择iOS==>Single View App,点击Next 2. 在新的弹框中需要App ...
- [转]JVM系列二:GC策略&内存申请、对象衰老
原文地址:http://www.cnblogs.com/redcreen/archive/2011/05/04/2037056.html JVM里的GC(Garbage Collection)的算法有 ...
- 大数据分析的下一代架构--IOTA架构设计实践[下]
大数据分析的下一代架构--IOTA架构设计实践[下] 原创置顶 代立冬 发布于2018-12-31 20:59:53 阅读数 2151 收藏 展开 IOTA架构提出背景 大数据3.0时代以前,Lam ...
- 微软Cloud+AI本地化社区更新
有关微软Cloud+AI本地化方面的介绍请参见我之前的文章:<微软Cloud+AI本地化社区贡献指南>,本文将公布该社区最新的活动变更事宜. MLCP改进 我们想借此机会向您介绍我们在社区 ...
- 自定义MVC三
完成t_mvc_book表的增删改查1.通用分页的jar.自定义mvc框架.自定义标签 导入jar.导入之前写好的pageTag.自定义mvc.xml pageTag private static f ...
- ACM-单向链表插入排序算法(在原链表上操作)
/* 1.若链表只有一个节点或者为空,直接返回 2.将链表的前两个节点排序,并将排序之后的第二个节点的下一个节点赋空 3.此时整个链表分为了两个,将未排序的节点一一插入到已排序链表中: 3.1.第 ...
- day76_10_23自定义签发token,其他drf组件
一.签发token的原理 当认证类authentication_classes是JSONWebTokenAuthentication时,其父类JSONWebTokenAPIView只有post 方法, ...