本缓存依赖于 System.Web.Caching 命名空间。
应用程序数据缓存
1 // 增加命名空间
2 using System.Web.Caching;
3
4 /// <summary>
5 /// 根据索引键值从缓存中获取对象
6 /// </summary>
7 /// <param name="cacheKey">索引键值</param>
8 /// <returns>缓存的对象</returns>
9 private static object GetObjectFromCache(string cacheKey)
10 {
11 Cache cache = HttpRuntime.Cache;
12 return cache[cacheKey];
13 }
14
15 /// <summary>
16 /// 将对象以cacheKey为索引键值添加到缓存中
17 /// </summary>
18 /// <param name="cacheKey">索引键值</param>
19 /// <param name="obj">需要缓存的对象</param>
20 private static void AddObjectToCache(string cacheKey, object obj)
21 {
22 Cache cache = HttpRuntime.Cache;
23 cache.Insert(cacheKey, obj);
24 }
25
26 /// <summary>
27 /// 将对象以cacheKey为索引键值添加到缓存中
28 /// </summary>
29 /// <param name="cacheKey">索引键值</param>
30 /// <param name="obj">需要缓存的对象</param>
31 /// <param name="absoluteExpiration">绝对过期时间</param>
32 /// <param name="slidingExpiration">最后一次访问所插入对象时与该对象过期时之间的时间间隔</param>
33 private static void AddObjectToCache(string cacheKey, object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)
34 {
35 Cache cache = HttpRuntime.Cache;
36 cache.Insert(cacheKey, obj, null, absoluteExpiration, slidingExpiration);
37 }
38
39 protected void Page_Load(object sender, EventArgs e)
40 {
41 string cacheKey = "__test";
42
43 // 从缓存中获取
44 object obj = GetObjectFromCache(cacheKey);
45 // 缓存里没有
46 if (obj == null)
47 {
48 // 把当前时间进行缓存
49 obj = DateTime.Now;
50 if (obj != null)
51 {
52 // 缓存时间30秒
53 int cacheTime = 30;
54 // 添加到缓存中去
55 AddObjectToCache(cacheKey, obj, DateTime.Now.AddSeconds(cacheTime), TimeSpan.Zero);
56 }
57 }
58 Literal1.Text = obj.ToString();
59 }
2 using System.Web.Caching;
3
4 /// <summary>
5 /// 根据索引键值从缓存中获取对象
6 /// </summary>
7 /// <param name="cacheKey">索引键值</param>
8 /// <returns>缓存的对象</returns>
9 private static object GetObjectFromCache(string cacheKey)
10 {
11 Cache cache = HttpRuntime.Cache;
12 return cache[cacheKey];
13 }
14
15 /// <summary>
16 /// 将对象以cacheKey为索引键值添加到缓存中
17 /// </summary>
18 /// <param name="cacheKey">索引键值</param>
19 /// <param name="obj">需要缓存的对象</param>
20 private static void AddObjectToCache(string cacheKey, object obj)
21 {
22 Cache cache = HttpRuntime.Cache;
23 cache.Insert(cacheKey, obj);
24 }
25
26 /// <summary>
27 /// 将对象以cacheKey为索引键值添加到缓存中
28 /// </summary>
29 /// <param name="cacheKey">索引键值</param>
30 /// <param name="obj">需要缓存的对象</param>
31 /// <param name="absoluteExpiration">绝对过期时间</param>
32 /// <param name="slidingExpiration">最后一次访问所插入对象时与该对象过期时之间的时间间隔</param>
33 private static void AddObjectToCache(string cacheKey, object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)
34 {
35 Cache cache = HttpRuntime.Cache;
36 cache.Insert(cacheKey, obj, null, absoluteExpiration, slidingExpiration);
37 }
38
39 protected void Page_Load(object sender, EventArgs e)
40 {
41 string cacheKey = "__test";
42
43 // 从缓存中获取
44 object obj = GetObjectFromCache(cacheKey);
45 // 缓存里没有
46 if (obj == null)
47 {
48 // 把当前时间进行缓存
49 obj = DateTime.Now;
50 if (obj != null)
51 {
52 // 缓存时间30秒
53 int cacheTime = 30;
54 // 添加到缓存中去
55 AddObjectToCache(cacheKey, obj, DateTime.Now.AddSeconds(cacheTime), TimeSpan.Zero);
56 }
57 }
58 Literal1.Text = obj.ToString();
59 }