我们经常会遇到这样的场景:
今天来了个业务,需要加一个字段,但是考虑的以后可能有变动,需要配成“活”的。
一般最初的做法就是加一个配置到Web.Config文件的AppSettings中去。但是这样有一个问题,那就是改一下配置节点,AppDomain就需要重启,很是不爽。
变通一点的会搞出一个xml文件,利用序列化去动态的读取。但是,哥!每次都读文件不觉得太耗IO吗?尤其是使用频率高话?
下面上代码吧,懒的废话了,关键地方都注释了,也不是什么高深的技术:
先来配置文件(注意Config路径要自己建,代码没有处理)和对应的配置文件代码:
class="brush:html;gutter:true;"><?xml version="1.0" encoding="utf-8"?> <SimpleBizConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ID>12</ID> <Key>MyKey</Key> <ListSimple> <string>简单</string> <string>list</string> <string>集合</string> </ListSimple> </SimpleBizConfig>
using System.Text; using Glutton.Web.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebTest.Models { public class SimpleBizConfig : ISimpleConfig { /// <summary> /// 默认配置文件路径 /// </summary> public string GetPath() { return "~/Config/SimpleBizConfig.cfg"; } public string GetCacheKey() { return "~/MyConfig_SimpleBizConfig"; } public SimpleBizConfig() { this.ID = 1; this.Key = "MyKey"; this.ListSimple = new List<string>(); } public int ID { get; set; } public string Key { get; set; } public List<string> ListSimple { get; set; } internal string Desc() { StringBuilder sb = new StringBuilder(); sb.Append("类型:SimpleBizConfig").Append("<br/>"); sb.Append("ID = " + this.ID.ToString()).Append("<br/>"); sb.Append("Key = " + this.Key).Append("<br/>"); sb.Append("list").Append("<br/>"); for (int i = 0; i < this.ListSimple.Count; i++) { sb.Append("index:" + i.ToString() + ",value:" + ListSimple[i]).Append("<br/>"); } return sb.ToString(); } } }
再来管理配置文件的类:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Caching; using System.Xml.Serialization; namespace Glutton.Web.Configuration { public interface ISimpleConfig { string GetPath(); string GetCacheKey(); } public class ConfigManager { public static T GetConfig<T>() where T : class ,ISimpleConfig, new() { T tmpT = new T(); string cacheKey = tmpT.GetCacheKey(); //先尝试从cache中取数据 T t = GetFromCache<T>(cacheKey);//很郁闷,没有静态泛型接口 if (t != null) { return t; } //cache没有数据,直接读配置文件 XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); string configFilePath = HttpContext.Current.Server.MapPath(tmpT.GetPath()); if (!File.Exists(configFilePath)) { //文件不存在,初始化,这里需要配置文件类实现默认的初始化动作 using (TextWriter writer = new StreamWriter(configFilePath)) { t = new T(); xmlSerializer.Serialize(writer, t); } } else { using (FileStream fs = new FileStream(configFilePath, FileMode.Open)) { t = xmlSerializer.Deserialize(fs) as T; } } //存到缓存里面去,依赖web缓存的文件依赖功能实现监控配置文件修改 SetToCache<T>(cacheKey, configFilePath, t); return t; } private static void SetToCache<T>(string cacheKey, string configFilePath, T t) where T : class ,new() { HttpRuntime.Cache.Insert(cacheKey, t, new CacheDependency(configFilePath), //文件依赖过期 Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); } private static T GetFromCache<T>(string cacheKey) where T : class ,new() { return HttpRuntime.Cache[cacheKey] as T; } } }
看看调用的方法,HomeController里面加了一个测试方法:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Glutton.Web.Configuration; using WebTest.Models; namespace WebTest.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } public string TestCfg() { return ConfigManager.GetConfig<SimpleBizConfig>().Desc(); } } }
看看效果,:-D: