蝙蝠岛资源网 Design By www.hbtsch.com
本文以一个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
}
}
标签:
Asp.Net,Cache,类
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无Asp.Net中Cache操作类实例详解的评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2025年11月04日
2025年11月04日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]