asp.net中缓存类DataCache(依赖文件缓存和时间缓存,或两者)

来源:互联网 发布:淘宝在哪里看已买宝贝 编辑:程序博客网 时间:2024/05/20 06:05
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Web;  
  5. using System.Web.Caching;  
  6. using System.IO;  
  7.   
  8. namespace Tools.Web  
  9. {  
  10.     /// <summary>  
  11.     /// 网页中的缓存类,使用示例:  
  12.     /// object obj = DataCache.GetCache("file1",depfile);  
  13.     ///if (obj == null)  
  14.     ///{  
  15.     ///   string txt = "缓存内容";//从数据库或文件读取到的内容  
  16.     ///   DataCache.SetCacheDepFile("file1", txt, depfile);  
  17.     /// }  
  18.     /// else  
  19.     /// {  
  20.     ///     string txt=obj.ToString();  
  21.     /// }  
  22.     /// </summary>  
  23.     public partial class DataCache  
  24.     {  
  25.         #region 文件路径web.config  
  26.         private static string _webconfigfile = string.Empty;  
  27.         /// <summary>  
  28.         /// 文件路径web.config  
  29.         /// </summary>  
  30.         public static string webconfigfile  
  31.         {  
  32.             get  
  33.             {  
  34.                 if (string.IsNullOrEmpty(_webconfigfile)) _webconfigfile = HttpContext.Current.Server.MapPath("/web.config");  
  35.                 return _webconfigfile;  
  36.             }  
  37.         }  
  38.         #endregion  
  39.  
  40.         #region 文件路径App_Data/ShopConfig.config  
  41.         private static string _shopconfigfile = string.Empty;  
  42.         /// <summary>  
  43.         /// 文件路径App_Data/ShopConfig.config  
  44.         /// </summary>  
  45.         public static string shopconfigfile  
  46.         {  
  47.             get  
  48.             {  
  49.                 if (string.IsNullOrEmpty(_shopconfigfile)) _shopconfigfile = HttpContext.Current.Server.MapPath("/App_Data/ShopConfig.config");  
  50.                 return _shopconfigfile;  
  51.             }  
  52.         }  
  53.         #endregion  
  54.  
  55.         #region 文件路径App_Data/SiteConfig.config  
  56.         private static string _siteconfigfile = string.Empty;  
  57.         /// <summary>  
  58.         /// 文件路径App_Data/SiteConfig.config  
  59.         /// </summary>  
  60.         public static string siteconfigfile  
  61.         {  
  62.             get  
  63.             {  
  64.                 if (string.IsNullOrEmpty(_siteconfigfile)) _siteconfigfile = HttpContext.Current.Server.MapPath("/App_Data/SiteConfig.config");  
  65.                 return _siteconfigfile;  
  66.             }  
  67.         }  
  68.         #endregion  
  69.  
  70.         #region 文件路径App_Data/Template.config  
  71.         private static string _templateconfigfile = string.Empty;  
  72.         /// <summary>  
  73.         /// 文件路径App_Data/Template.config  
  74.         /// </summary>  
  75.         public static string templateconfigfile  
  76.         {  
  77.             get  
  78.             {  
  79.                 if (string.IsNullOrEmpty(_templateconfigfile)) _templateconfigfile = HttpContext.Current.Server.MapPath("/App_Data/Template.config");  
  80.                 return _templateconfigfile;  
  81.             }  
  82.         }  
  83.         #endregion  
  84.  
  85.         #region 删除缓存  
  86.         /// <summary>  
  87.         /// 删除缓存  
  88.         /// </summary>  
  89.         /// <param name="CacheKey">键</param>  
  90.         public static void DeleteCache(string CacheKey)  
  91.         {  
  92.             HttpRuntime.Cache.Remove(CacheKey);  
  93.         }  
  94.         #endregion  
  95.  
  96.         #region 获取缓存,依赖时间  
  97.         /// <summary>  
  98.         /// 获取缓存,依赖时间  
  99.         /// </summary>  
  100.         /// <param name="CacheKey">键</param>  
  101.         /// <returns></returns>  
  102.         public static object GetCache(string CacheKey)  
  103.         {  
  104.             object obj_time=HttpRuntime.Cache[CacheKey + "_time"];  
  105.             object obj_cache=HttpRuntime.Cache[CacheKey];  
  106.             if (obj_time != null && obj_cache!=null)  
  107.             {  
  108.                 if (Convert.ToDateTime(obj_time) < DateTime.Now)  
  109.                 {  
  110.                     DeleteCache(CacheKey);  
  111.                     DeleteCache(CacheKey + "_time");  
  112.                     return null;  
  113.                 }  
  114.                 else return obj_cache;  
  115.             }  
  116.             else  
  117.             {  
  118.                 DeleteCache(CacheKey);  
  119.                 DeleteCache(CacheKey+"_time");  
  120.                 return null;  
  121.             }  
  122.         }  
  123.         #endregion  
  124.  
  125.         #region 获取缓存,依赖文件  
  126.         /// <summary>  
  127.         /// 获取缓存,依赖文件  
  128.         /// </summary>  
  129.         /// <param name="CacheKey">键</param>  
  130.         /// <param name="depFile">依赖的文件</param>  
  131.         /// <returns></returns>  
  132.         public static object GetCache(string CacheKey, string depFile)  
  133.         {  
  134.             object obj_time = HttpRuntime.Cache[CacheKey + "_time"];  
  135.             object obj_cache = HttpRuntime.Cache[CacheKey];  
  136.             if (File.Exists(depFile))  
  137.             {  
  138.                 FileInfo fi = new FileInfo(depFile);  
  139.   
  140.                 if (obj_time != null && obj_cache != null)  
  141.                 {  
  142.                     if (Convert.ToDateTime(obj_time) != fi.LastWriteTime)  
  143.                     {  
  144.                         DeleteCache(CacheKey);  
  145.                         DeleteCache(CacheKey + "_time");  
  146.                         return null;  
  147.                     }  
  148.                     else return obj_cache;  
  149.                 }  
  150.                 else  
  151.                 {  
  152.                     DeleteCache(CacheKey);  
  153.                     DeleteCache(CacheKey + "_time");  
  154.                     return null;  
  155.                 }  
  156.             }  
  157.             else  
  158.             {  
  159.                 throw new Exception("文件(" + depFile + ")不存在!");  
  160.             }  
  161.         }   
  162.         #endregion  
  163.  
  164.         #region 简单的插入缓存  
  165.         /// <summary>  
  166.         /// 简单的插入缓存  
  167.         /// </summary>  
  168.         /// <param name="CacheKey">键</param>  
  169.         /// <param name="objObject">数据</param>  
  170.         public static void SetCache(string CacheKey, object objObject)  
  171.         {  
  172.             HttpRuntime.Cache.Insert(CacheKey, objObject);  
  173.         }  
  174.         #endregion  
  175.  
  176.         #region 有过期时间的插入缓存数据  
  177.         /// <summary>  
  178.         /// 有过期时间的插入缓存数据  
  179.         /// </summary>  
  180.         /// <param name="CacheKey">键</param>  
  181.         /// <param name="objObject">数据</param>  
  182.         /// <param name="absoluteExpiration">过期时间</param>  
  183.         /// <param name="slidingExpiration">可调度参数,传null就是禁用可调度</param>  
  184.         public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)  
  185.         {  
  186.             if (slidingExpiration == null) slidingExpiration = Cache.NoSlidingExpiration;  
  187.             HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);  
  188.             HttpRuntime.Cache.Insert(CacheKey + "_time", absoluteExpiration, null, absoluteExpiration, slidingExpiration);//存储过期时间  
  189.         }  
  190.         #endregion  
  191.  
  192.         #region 插入缓存数据,指定缓存多少秒  
  193.         /// <summary>  
  194.         /// 插入缓存数据,指定缓存多少秒  
  195.         /// </summary>  
  196.         /// <param name="CacheKey">缓存的键</param>  
  197.         /// <param name="objObject">缓存的数据</param>  
  198.         /// <param name="seconds">缓存秒数</param>  
  199.         /// <param name="slidingExpiration">传null就是禁用可调度过期</param>  
  200.         public static void SetCacheSecond(string CacheKey, object objObject, int seconds, TimeSpan slidingExpiration)  
  201.         {  
  202.             DateTime absoluteExpiration = DateTime.Now.AddSeconds(seconds);  
  203.             if (slidingExpiration == null) slidingExpiration = Cache.NoSlidingExpiration;  
  204.             HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);  
  205.             HttpRuntime.Cache.Insert(CacheKey + "_time", absoluteExpiration, null, absoluteExpiration, slidingExpiration);//存储过期时间  
  206.         }  
  207.         #endregion  
  208.  
  209.         #region 依赖文件的缓存,文件没改不会过期  
  210.         /// <summary>  
  211.         /// 依赖文件的缓存,文件没改不会过期  
  212.         /// </summary>  
  213.         /// <param name="CacheKey">键</param>  
  214.         /// <param name="objObject">数据</param>  
  215.         /// <param name="depfilename">依赖文件,可调用 DataCache 里的变量</param>  
  216.         public static void SetCacheDepFile(string CacheKey, object objObject, string depfilename)  
  217.         {  
  218.             //缓存依赖对象  
  219.             System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(depfilename);  
  220.             DateTime absoluteExpiration = System.Web.Caching.Cache.NoAbsoluteExpiration;  
  221.             TimeSpan slidingExpiration=System.Web.Caching.Cache.NoSlidingExpiration;  
  222.             System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  223.             objCache.Insert(  
  224.                 CacheKey,  
  225.                 objObject,  
  226.                 dep,  
  227.                 System.Web.Caching.Cache.NoAbsoluteExpiration, //从不过期  
  228.                 slidingExpiration, //禁用可调过期  
  229.                 System.Web.Caching.CacheItemPriority.Default,  
  230.                 null);  
  231.             if (File.Exists(depfilename))  
  232.             {  
  233.                 FileInfo fi = new FileInfo(depfilename);  
  234.                 DateTime lastWriteTime = fi.LastWriteTime;  
  235.                 HttpRuntime.Cache.Insert(CacheKey + "_time", lastWriteTime, null, absoluteExpiration, slidingExpiration);//存储文件最后修改时间  
  236.             }  
  237.   
  238.         }  
  239.         #endregion  
  240.     }  
  241. }  
0 0
原创粉丝点击