在写完面向服务架构~全局配置文件也面向服务了与面向服务架构~全局配置文件也面向服务了~续(对性能的优化)之后,觉得程序还是有可以优化的地方,在我上一篇文章中也提到了cache,所以今天这一篇主要把cache加入到项目中来,以便最大程度上提高程序的性能。
1 /// <summary> 2 /// 从文件中反序列化到实体 3 /// </summary> 4 /// <param name="fileName"></param> 5 /// <param name="type"></param> 6 private IConfiger LoadConfigFile(string fileName, Type type) 7 { 8 this.configType = type; 9 fileChangeTime[fileName] = File.GetLastWriteTime(fileName); 10 return ConfigSerialize.DeserializeInfo(fileName, this.configType); 11 } 12 /// <summary> 13 /// 加载配置文 14 /// </summary> 15 /// <param name="fileName">文件名</param> 16 /// <param name="type">ʵ实体类型</param> 17 /// <returns></returns> 18 internal IConfiger LoadConfig(string fileName, Type type) 19 { 20 return LoadConfig(fileName, type, true); 21 } 22 23 /// <summary> 24 /// 加载配置文件 25 /// </summary> 26 /// <param name="fileName">文件名</param> 27 /// <param name="type">实体类型</param> 28 /// <param name="isCache">是否要从缓存读取</param> 29 /// <returns></returns> 30 internal IConfiger LoadConfig(string fileName, Type type, bool isCache) 31 { 32 if (!isCache) 33 return LoadConfigFile(fileName, type); 34 lock (lockHelper) 35 { 36 if (DataCache.GetCache(fileName) == null) 37 DataCache.SetCache(fileName, LoadConfigFile(fileName, type)); 38 DateTime newfileChangeTime = File.GetLastWriteTime(fileName); 39 if (!newfileChangeTime.Equals(fileChangeTime[fileName])) 40 { 41 DataCache.SetCache(fileName, LoadConfigFile(fileName, type)); 42 return LoadConfigFile(fileName, type); 43 } 44 else 45 { 46 return DataCache.GetCache(fileName) as IConfiger; 47 } 48 } 49 }
实事上,使用cache使用的流程是:如果cache里没有文件的缓存,就向从文件缓存到cache,再判断是否文件被修改过,如果没有直接返回cache中的实体配置信息,如果文件已经被修改,就从文件中取回,并同时存入缓存。
以下是.net cache的操作功能类代码:
1 namespace ConfigCache 2 { 3 public class DataCache 4 { 5 /// <summary> 6 /// 得到cache键所对应的值 7 /// </summary> 8 /// <param name="CacheKey"></param> 9 /// <returns></returns> 10 public static object GetCache(string CacheKey) 11 { 12 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 13 return objCache[CacheKey]; 14 } 15 /// <summary> 16 /// 将指定值设置到cache键上 17 /// </summary> 18 /// <param name="CacheKey">键</param> 19 /// <param name="objObject">值</param> 20 public static void SetCache(string CacheKey, object objObject) 21 { 22 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 23 objCache.Insert(CacheKey, objObject); 24 } 25 /// <summary> 26 /// 将指定值设置到cache键上 27 /// </summary> 28 /// <param name="CacheKey">键</param> 29 /// <param name="objObject">值</param> 30 /// <param name="absoluteExpiration">绝对过期时间</param> 31 /// <param name="slidingExpiration">相对过期时间</param> 32 public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) 33 { 34 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 35 objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration); 36 } 37 38 /// <summary> 39 /// 移除指定cache键 40 /// </summary> 41 /// <param name="CacheKey"></param> 42 public static void RemoveCache(string CacheKey) 43 { 44 System.Web.Caching.Cache objCache = HttpRuntime.Cache; 45 objCache.Remove(CacheKey); 46 47 } 48 49 }
}
这下子,应该有的都有了,业务分别了,性能也上去了,咱们的服务型配置文件的实现也就讲完了,呵呵,各位晚安了!