zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    09-2:跳台阶
    09:菲波那切数列
    08:旋转数组的最小值
    07:用两个栈实现队列
    06:重建二叉树
    05:从尾到头打印链表
    04:替换字符
    centos7安装Jenkins更改默认端口并配置Ldap服务器进行用户认证
    Jira配置openLdap服务器进行用户认证
    定时自动从FTP服务器取数据脚本
  • 原文地址:https://www.cnblogs.com/jiayc/p/8693739.html
Copyright © 2011-2022 走看看