国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.Net中Cache操作類實例詳解

2024-07-10 12:43:14
字體:
來源:轉載
供稿:網友

本文以一個Asp.Net的Cache操作類實例代碼來詳細描述了cache緩存的結構及實現方法,完整代碼如下所示:

/// <head>/// <function>///  存儲類(存儲UserInfo信息)/// </function>/// <description>///  用Cache存儲用戶信息///  在指定間隔(TimeOut)內取,則可以從Cache中取,///  如果超出存儲時間,則從數據庫取用戶信息數據///  作為所有用戶信息的存儲類./// </description>/// <author>///  <name>ChengKing</name>  /// </author>/// </head>using System;using System.Web;using System.Web.Caching;namespace Common{    /// <summary> /// 存儲類(存儲UserInfo信息) /// </summary> public class Storage { public Storage() {  //  // TODO: 在此處添加構造函數邏輯  // } #region 方法 //實現“一鍵一值”存儲方法,最普通的存儲方法          //(“一鍵一值”指一個Identify存儲一個值,下面還有一個“一鍵多值”方法,因為有時候需要一個鍵存儲多個變量對象值)        public static bool InsertIdentify(string strIdentify,object Info) {    if(strIdentify != null && strIdentify.Length != 0 && userInfo != null)  {  //建立回調委托的一個實例    CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);  //以Identify為標志,將userInfo存入Cache  HttpContext.Current.Cache.Insert(strIdentify,userInfo,null,         System.DateTime.Now.AddSeconds(300),        System.Web.Caching.Cache.NoSlidingExpiration,         System.Web.Caching.CacheItemPriority.Default,        callBack);  return true;  }    else  {  return false;  } } //判斷存儲的"一鍵一值"值是否還存在(有沒有過期失效或從來都未存儲過)      public static bool ExistIdentify(string strIdentify) {  if(HttpContext.Current.Cache[strIdentify] != null)  {  return true;  }  else  {  return false;  } } //插入"一鍵多值"方法 //***其中 StorageInfType是一個Enum,里面存有三種類型: UserInf SysInf PageInf  //這個枚舉如下: /*      public enum StorageInfType      {    /// <summary>用戶信息</summary>      UserInf = 0,    /// <summary>頁面信息</summary>    PageInf = 1,     /// <summary>系統信息</summary>        SysInf = 2       }        //此枚舉是自己定義的.可根據需要定義不同的枚舉         //加個枚舉目的是實現“一鍵多值”存儲方法,事實上Cache中是存放了多個變量的,只不過被這個類封裝了,        //程序員感到就好像是“一鍵一值”.  這樣做目的是可以簡化開發操作,否則程序員要存儲幾個變量就得定義幾個Identify. public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue) {    if(strIdentify != null && strIdentify != "" && strIdentify.Length != 0 && objValue != null)  {  //RemoveCommonInf(strIdentify,enumInfType);   //建立回調委托的一個實例        CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);  if(enumInfType == StorageInfType.UserInf)  {      //以用戶UserID+信息標志(StorageInfType枚舉),將userInfo存入Cache    HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.UserInf.ToString(),objValue,null,          System.DateTime.Now.AddSeconds(18000),    //單位秒         System.Web.Caching.Cache.NoSlidingExpiration,          System.Web.Caching.CacheItemPriority.Default,         callBack);   }  if(enumInfType == StorageInfType.PageInf)  {   //以用戶UserID+信息標志(StorageInfType枚舉),將PageInfo存入Cache     HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.PageInf.ToString(),objValue,null,           System.DateTime.Now.AddSeconds(18000),          System.Web.Caching.Cache.NoSlidingExpiration,           System.Web.Caching.CacheItemPriority.Default,          callBack);   }  if(enumInfType == StorageInfType.SysInf)  {   //以用戶UserID+信息標志(StorageInfType枚舉),將SysInfo存入Cache   HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.SysInf.ToString(),objValue,null,           System.DateTime.Now.AddSeconds(18000),           System.Web.Caching.Cache.NoSlidingExpiration,           System.Web.Caching.CacheItemPriority.Default,          callBack);   }  return true;  }  return false; }        //讀取“一鍵多值”Identify的值        public static bool ReadIdentify(string strIdentify,out UserInfo userInfo) {  //取出值  if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)  {  userInfo = (UserInfo)HttpContext.Current.Cache[strIdentify];  if(userInfo == null)  {   return false;  }  return true;  }    else  {  userInfo = null;  return false;  }   } //手動移除“一鍵一值”對應的值        public static bool RemoveIdentify(string strIdentify) {  //取出值  if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)  {  HttpContext.Current.Cache.Remove(strIdentify);      }    return true;  }        //此方法在值失效之前調用,可以用于在失效之前更新數據庫,或從數據庫重新獲取數據 private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason) { } #endregion } }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 观塘区| 绥江县| 尼玛县| 彝良县| 永济市| 闵行区| 淅川县| 瑞金市| 商城县| 沭阳县| 巍山| 固原市| 鄂伦春自治旗| 云梦县| 屏山县| 饶河县| 若尔盖县| 松原市| 临西县| 宁陵县| 苍梧县| 西乌| 肃南| 岗巴县| 清涧县| 芮城县| 哈密市| 陆河县| 张家界市| 新源县| 喜德县| 横峰县| 安吉县| 宣汉县| 长白| 泽州县| 紫云| 湟中县| 库尔勒市| 永丰县| 上虞市|