Asp.Net中Cache操作類實(shí)例詳解
本文以一個(gè)Asp.Net的Cache操作類實(shí)例代碼來(lái)詳細(xì)描述了cache緩存的結(jié)構(gòu)及實(shí)現(xiàn)方法,完整代碼如下所示:
/// <head>
/// <function>
/// 存儲(chǔ)類(存儲(chǔ)UserInfo信息)
/// </function>
/// <description>
/// 用Cache存儲(chǔ)用戶信息
/// 在指定間隔(TimeOut)內(nèi)取,則可以從Cache中取,
/// 如果超出存儲(chǔ)時(shí)間,則從數(shù)據(jù)庫(kù)取用戶信息數(shù)據(jù)
/// 作為所有用戶信息的存儲(chǔ)類.
/// </description>
/// <author>
/// <name>ChengKing</name>
/// </author>
/// </head>
using System;
using System.Web;
using System.Web.Caching;
namespace Common
{
/// <summary>
/// 存儲(chǔ)類(存儲(chǔ)UserInfo信息)
/// </summary>
public class Storage
{
public Storage()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
#region 方法
//實(shí)現(xiàn)“一鍵一值”存儲(chǔ)方法,最普通的存儲(chǔ)方法
//(“一鍵一值”指一個(gè)Identify存儲(chǔ)一個(gè)值,下面還有一個(gè)“一鍵多值”方法,因?yàn)橛袝r(shí)候需要一個(gè)鍵存儲(chǔ)多個(gè)變量對(duì)象值)
public static bool InsertIdentify(string strIdentify,object Info)
{
if(strIdentify != null && strIdentify.Length != 0 && userInfo != null)
{
//建立回調(diào)委托的一個(gè)實(shí)例
CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
//以Identify為標(biāo)志,將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;
}
}
//判斷存儲(chǔ)的"一鍵一值"值是否還存在(有沒(méi)有過(guò)期失效或從來(lái)都未存儲(chǔ)過(guò))
public static bool ExistIdentify(string strIdentify)
{
if(HttpContext.Current.Cache[strIdentify] != null)
{
return true;
}
else
{
return false;
}
}
//插入"一鍵多值"方法
//***其中 StorageInfType是一個(gè)Enum,里面存有三種類型: UserInf SysInf PageInf
//這個(gè)枚舉如下:
/*
public enum StorageInfType
{
/// <summary>用戶信息</summary>
UserInf = 0,
/// <summary>頁(yè)面信息</summary>
PageInf = 1,
/// <summary>系統(tǒng)信息</summary>
SysInf = 2
}
//此枚舉是自己定義的.可根據(jù)需要定義不同的枚舉
//加個(gè)枚舉目的是實(shí)現(xiàn)“一鍵多值”存儲(chǔ)方法,事實(shí)上Cache中是存放了多個(gè)變量的,只不過(guò)被這個(gè)類封裝了,
//程序員感到就好像是“一鍵一值”. 這樣做目的是可以簡(jiǎn)化開(kāi)發(fā)操作,否則程序員要存儲(chǔ)幾個(gè)變量就得定義幾個(gè)Identify.
public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue)
{
if(strIdentify != null && strIdentify != "" && strIdentify.Length != 0 && objValue != null)
{
//RemoveCommonInf(strIdentify,enumInfType);
//建立回調(diào)委托的一個(gè)實(shí)例
CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
if(enumInfType == StorageInfType.UserInf)
{
//以用戶UserID+信息標(biāo)志(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+信息標(biāo)志(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+信息標(biāo)志(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;
}
}
//手動(dòng)移除“一鍵一值”對(duì)應(yīng)的值
public static bool RemoveIdentify(string strIdentify)
{
//取出值
if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
{
HttpContext.Current.Cache.Remove(strIdentify);
}
return true;
}
//此方法在值失效之前調(diào)用,可以用于在失效之前更新數(shù)據(jù)庫(kù),或從數(shù)據(jù)庫(kù)重新獲取數(shù)據(jù)
private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason)
{
}
#endregion
}
}
相關(guān)文章
排除JQuery通過(guò)HttpGet調(diào)用WebService返回Json時(shí)“parserror”錯(cuò)誤
排除JQuery通過(guò)HttpGet調(diào)用WebService返回Json時(shí)“parserror”錯(cuò)誤的解決方法。2011-10-10
ASP.NET Core快速入門(mén)之實(shí)戰(zhàn)篇
這篇文章主要介紹了ASP.NET Core快速入門(mén)之實(shí)戰(zhàn)篇,對(duì)跨平臺(tái)框架感興趣的同學(xué),可以參考下2021-04-04
asp.net驗(yàn)證一個(gè)字符串是否符合指定的正則表達(dá)式
asp.net檢驗(yàn)字符串是否滿足指定正則表達(dá)式2008-05-05
GridView分頁(yè)代碼簡(jiǎn)單萬(wàn)能實(shí)用
GridView在使用.net技術(shù)搭建的后臺(tái),在商品列表或者是信息列表經(jīng)常會(huì)出現(xiàn);它的作用在于有效的管理信息,增刪改查等等最主要的是還可以實(shí)現(xiàn)分頁(yè),這一點(diǎn)是無(wú)可比靡的,接下來(lái)介紹如何使用GridView實(shí)現(xiàn)分頁(yè),需要了解的朋友可以參考下2012-12-12
ASP.NET?MVC使用Knockout獲取數(shù)組元素索引的2種方法
這篇文章介紹了ASP.NET?MVC使用Knockout獲取數(shù)組元素索引的2種方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Asp.net "對(duì)路徑的訪問(wèn)被拒絕" 解決方法的分析
本篇文章是對(duì)Asp.net中"對(duì)路徑的訪問(wèn)被拒絕" 的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
基于.NET的FluentValidation數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)
這篇文章主要介紹了基于.NET的FluentValidation數(shù)據(jù)驗(yàn)證實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

