Asp.Net中Cache操作類

來源:互聯網
上載者:User
/// <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
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.