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 新增类介绍(含类的源码及新版本配置工具源码)
前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...
随机推荐
- Linux 查看 添加 修改路由
最近搭建vpn, 使用 ssh 隧道一直在涉及路由相关问题,今天简单整理一下,方便下次使用: 查看路由: [jsi@localhost Desktop]$ route Kernel IP routin ...
- CodeForces - 1251E2 (思维+贪心)
题意 https://vjudge.net/problem/CodeForces-1251E2 一共有 n 个选民,你可以付出 pi 的代价让第 i 个选民为你投票,或者,在为你投票的人数达到 mi ...
- java中的字符串一
public class TestString2 { public static void main(String[] args) { //判断两字符串是否相等 String s1 = "H ...
- 详解C++ STL priority_queue 容器
详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...
- CF-weekly4 F. Kyoya and Colored Balls
https://codeforces.com/gym/253910/problem/F F. Kyoya and Colored Balls time limit per test 2 seconds ...
- Jupyter notebook中的.ipynb文件转换成python的.py文件
转自:https://blog.csdn.net/wyr_rise/article/details/82656555 Jupyter notebook中.py与.ipynb文件的import问题 ...
- Spring Security OAuth2学习
什么是 oAuth oAuth 协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是 oAuth 的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需 ...
- jenkins报错 Host key verification failed.
一.Host key verification failed 问题描述 在本地windows机器上安装了jenkins,在git bash命令行窗口可以使用git pull命令,但是在jenkins ...
- 解惑:在Ubuntu18.04.2的idea上运行Scala支持的spark程序遇到的问题
解惑:在Ubuntu18.04.2的idea上运行Scala支持的spark程序遇到的问题 一.前言 最近在做一点小的实验,用到了Scala,spark这些东西,于是在Linux平台上来完成,结果一个 ...
- [MFC]_在vs2019中使用MFC快速构建简单windows窗口程序
微软基础类库(英语: Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用程序框架,以减少应用程序开发 ...