Asp.Net中Cache操作类

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

/// <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

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航