更新:2013-12-29
经过不断的修改和运行测试,在实际项目中使用得很好的了。。
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.Caching; using System.IO; namespace Tools.Web { /// <summary> /// 网页中的缓存类,使用示例: /// object obj = DataCache.GetCache("file1",depfile); ///if (obj == null) ///{ /// string txt = "缓存内容";//从数据库或文件读取到的内容 /// DataCache.SetCacheDepFile("file1", txt, depfile); /// } /// else /// { /// string txt=obj.ToString(); /// } /// </summary> public partial class DataCache { #region 文件路径web.config private static string _webconfigfile = string.Empty; /// <summary> /// 文件路径web.config /// </summary> public static string webconfigfile { get { if (string.IsNullOrEmpty(_webconfigfile)) _webconfigfile = HttpContext.Current.Server.MapPath("/web.config"); return _webconfigfile; } } #endregion #region 文件路径App_Data/ShopConfig.config private static string _shopconfigfile = string.Empty; /// <summary> /// 文件路径App_Data/ShopConfig.config /// </summary> public static string shopconfigfile { get { if (string.IsNullOrEmpty(_shopconfigfile)) _shopconfigfile = HttpContext.Current.Server.MapPath("/App_Data/ShopConfig.config"); return _shopconfigfile; } } #endregion #region 文件路径App_Data/SiteConfig.config private static string _siteconfigfile = string.Empty; /// <summary> /// 文件路径App_Data/SiteConfig.config /// </summary> public static string siteconfigfile { get { if (string.IsNullOrEmpty(_siteconfigfile)) _siteconfigfile = HttpContext.Current.Server.MapPath("/App_Data/SiteConfig.config"); return _siteconfigfile; } } #endregion #region 文件路径App_Data/Template.config private static string _templateconfigfile = string.Empty; /// <summary> /// 文件路径App_Data/Template.config /// </summary> public static string templateconfigfile { get { if (string.IsNullOrEmpty(_templateconfigfile)) _templateconfigfile = HttpContext.Current.Server.MapPath("/App_Data/Template.config"); return _templateconfigfile; } } #endregion #region 删除缓存 /// <summary> /// 删除缓存 /// </summary> /// <param name="CacheKey">键</param> public static void DeleteCache(string CacheKey) { HttpRuntime.Cache.Remove(CacheKey); } #endregion #region 获取缓存,依赖时间 /// <summary> /// 获取缓存,依赖时间 /// </summary> /// <param name="CacheKey">键</param> /// <returns></returns> public static object GetCache(string CacheKey) { object obj_time=HttpRuntime.Cache[CacheKey + "_time"]; object obj_cache=HttpRuntime.Cache[CacheKey]; if (obj_time != null && obj_cache!=null) { if (Convert.ToDateTime(obj_time) < DateTime.Now) { DeleteCache(CacheKey); DeleteCache(CacheKey + "_time"); return null; } else return obj_cache; } else { DeleteCache(CacheKey); DeleteCache(CacheKey+"_time"); return null; } } #endregion #region 获取缓存,依赖文件 /// <summary> /// 获取缓存,依赖文件 /// </summary> /// <param name="CacheKey">键</param> /// <param name="depFile">依赖的文件</param> /// <returns></returns> public static object GetCache(string CacheKey, string depFile) { object obj_time = HttpRuntime.Cache[CacheKey + "_time"]; object obj_cache = HttpRuntime.Cache[CacheKey]; if (File.Exists(depFile)) { FileInfo fi = new FileInfo(depFile); if (obj_time != null && obj_cache != null) { if (Convert.ToDateTime(obj_time) != fi.LastWriteTime) { DeleteCache(CacheKey); DeleteCache(CacheKey + "_time"); return null; } else return obj_cache; } else { DeleteCache(CacheKey); DeleteCache(CacheKey + "_time"); return null; } } else { throw new Exception("文件(" + depFile + ")不存在!"); } } #endregion #region 简单的插入缓存 /// <summary> /// 简单的插入缓存 /// </summary> /// <param name="CacheKey">键</param> /// <param name="objObject">数据</param> public static void SetCache(string CacheKey, object objObject) { HttpRuntime.Cache.Insert(CacheKey, objObject); } &n