本文以一个Asp.Net的Cache操作类实例代码来详细描述了cache缓存的结构及实现方法,完整代码如下所示:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/// <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
}
}
|
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 104
-
2025-05-27 35
-
2025-05-27 15
-
2025-05-29 21
-
2025-05-27 27
热门评论

