CacheHelper02

  1. using System;  
  2. using System.Web;  
  3. using System.Collections;  
  4.   
  5. namespace MSCL  
  6. {  
  7.     /// <summary>  
  8.     /// Cache辅助类  
  9.     /// </summary>  
  10.     public class CacheHelper  
  11.     {  
  12.         /// <summary>  
  13.         /// 获取数据缓存  
  14.         /// </summary>  
  15.         /// <param name="CacheKey">键</param>  
  16.         public static object GetCache(string CacheKey)  
  17.         {  
  18.             System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  19.             return objCache[CacheKey];  
  20.         }  
  21.   
  22.         /// <summary>  
  23.         /// 设置数据缓存  
  24.         /// </summary>  
  25.         public static void SetCache(string CacheKey, object objObject)  
  26.         {  
  27.             System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  28.             objCache.Insert(CacheKey, objObject);  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 设置数据缓存  
  33.         /// </summary>  
  34.         public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)  
  35.         {  
  36.             System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  37.             objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);  
  38.         }  
  39.   
  40.         /// <summary>  
  41.         /// 设置数据缓存  
  42.         /// </summary>  
  43.         public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)  
  44.         {  
  45.             System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  46.             objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);  
  47.         }  
  48.   
  49.         /// <summary>  
  50.         /// 移除指定数据缓存  
  51.         /// </summary>  
  52.         public static void RemoveAllCache(string CacheKey)  
  53.         {  
  54.             System.Web.Caching.Cache _cache = HttpRuntime.Cache;  
  55.             _cache.Remove(CacheKey);  
  56.         }  
  57.   
  58.         /// <summary>  
  59.         /// 移除全部缓存  
  60.         /// </summary>  
  61.         public static void RemoveAllCache()  
  62.         {  
  63.             System.Web.Caching.Cache _cache = HttpRuntime.Cache;  
  64.             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();  
  65.             while (CacheEnum.MoveNext())  
  66.             {  
  67.                 _cache.Remove(CacheEnum.Key.ToString());  
  68.             }  
  69.         }  
  70.     }  
  71. }  
 
 
  1. var cache = MSCL.CacheHelper.GetCache("mydata");  
  2. List<TestTable> list = new List<TestTable>();  
  3. if (cache == null)  
  4. {  
  5.     int recordCount = 0;  
  6.     int totalPage = 0;  
  7.     list = RBAC.Dal.DataRootBase.QueryPagingMssql<TestTable>("TestTable", "*", "D_Id asc", 2, 10, "", out recordCount, out totalPage);  
  8.     //插入cache 缓存30秒  
  9.     MSCL.CacheHelper.SetCache("mydata", list, DateTime.Now.AddSeconds(30), TimeSpan.Zero);  
  10. }  
  11. else  
  12. {  
  13.     list = (List<TestTable>)cache;  
  14. }  
  15.   
  16.   
  17. foreach (var item in list)  
  18. {  
  19.     str += string.Format("用户名{0}密码{1}真实姓名{2}--{3}<br>", item.D_Id, item.D_Password, item.D_Name, DateTime.Now);  
  20. }  
posted @ 2018-04-02 15:25  称虚圆  阅读(84)  评论(0编辑  收藏  举报