欲善其事,必利其器 - Librame.Caching 缓存功能
Librame.Caching 缓存主要功能介绍
一、Librame.Caching.Cache(缓存类)
Cache 与 System.Web.Caching.Cache 的使用方式一致;相较于后者,引入了线程锁、缓存项队列等机制,同时进行了优化。
二、Librame.Caching.CachePools(缓存池类)
提供一组静态操作缓存的方法,相当于 Dictionary<string, Cache> 键缓存对集合。
三、Librame.Caching.ICacher(缓存器接口)与 Librame.Caching.CacherBase(缓存器基类)
提供封装的单个缓存对象的操作方法,下面以 InstanceCacher 实例缓存器为例介绍如何自定义使用缓存。
View Code
1 namespace Librame.Caching
2 {
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6
7 /// <summary>
8 /// 实例缓存器
9 /// </summary>
10 public class InstanceCacher : CacherBase, ICacher
11 {
12 /// <summary>
13 /// 构造函数
14 /// </summary>
15 public InstanceCacher()
16 : this("InstanceCacher")
17 {
18 }
19 /// <summary>
20 /// 构造函数
21 /// </summary>
22 /// <param name="cachePoolKey"></param>
23 public InstanceCacher(string cachePoolKey)
24 : base(cachePoolKey)
25 {
26 }
27
28 /// <summary>
29 /// 当前缓存键(需重载)
30 /// </summary>
31 public virtual string CurrentKey
32 {
33 get { throw new Exception("This attribute needs to be overridden."); }
34 }
35
36 /// <summary>
37 /// 是否包含当前缓存实例
38 /// </summary>
39 /// <returns></returns>
40 public bool Contains()
41 {
42 return base.Contains(this.CurrentKey);
43 }
44
45 /// <summary>
46 /// 获取当前缓存实例
47 /// </summary>
48 /// <returns></returns>
49 public object Get()
50 {
51 return base.Get(this.CurrentKey);
52 }
53
54 /// <summary>
55 /// 添加或设置当前缓存实例
56 /// </summary>
57 /// <param name="value"></param>
58 public void AddOrSet(object value)
59 {
60 base.AddOrSet(this.CurrentKey, value);
61 }
62
63 /// <summary>
64 /// 添加当前缓存实例
65 /// </summary>
66 /// <param name="value"></param>
67 /// <returns></returns>
68 public object Add(object value)
69 {
70 return base.Add(this.CurrentKey, value);
71 }
72
73 /// <summary>
74 /// 设置当前缓存实例
75 /// </summary>
76 /// <param name="value"></param>
77 public void Set(object value)
78 {
79 base.Set(this.CurrentKey, value);
80 }
81
82 /// <summary>
83 /// 移除当前缓存实例
84 /// </summary>
85 /// <returns></returns>
86 public object Remove()
87 {
88 return base.Remove(this.CurrentKey);
89 }
90
91 /// <summary>
92 /// 清空当前缓存
93 /// </summary>
94 public void Clear()
95 {
96 base.Clear(this.CurrentKey);
97 }
98 }
99 }
2 {
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6
7 /// <summary>
8 /// 实例缓存器
9 /// </summary>
10 public class InstanceCacher : CacherBase, ICacher
11 {
12 /// <summary>
13 /// 构造函数
14 /// </summary>
15 public InstanceCacher()
16 : this("InstanceCacher")
17 {
18 }
19 /// <summary>
20 /// 构造函数
21 /// </summary>
22 /// <param name="cachePoolKey"></param>
23 public InstanceCacher(string cachePoolKey)
24 : base(cachePoolKey)
25 {
26 }
27
28 /// <summary>
29 /// 当前缓存键(需重载)
30 /// </summary>
31 public virtual string CurrentKey
32 {
33 get { throw new Exception("This attribute needs to be overridden."); }
34 }
35
36 /// <summary>
37 /// 是否包含当前缓存实例
38 /// </summary>
39 /// <returns></returns>
40 public bool Contains()
41 {
42 return base.Contains(this.CurrentKey);
43 }
44
45 /// <summary>
46 /// 获取当前缓存实例
47 /// </summary>
48 /// <returns></returns>
49 public object Get()
50 {
51 return base.Get(this.CurrentKey);
52 }
53
54 /// <summary>
55 /// 添加或设置当前缓存实例
56 /// </summary>
57 /// <param name="value"></param>
58 public void AddOrSet(object value)
59 {
60 base.AddOrSet(this.CurrentKey, value);
61 }
62
63 /// <summary>
64 /// 添加当前缓存实例
65 /// </summary>
66 /// <param name="value"></param>
67 /// <returns></returns>
68 public object Add(object value)
69 {
70 return base.Add(this.CurrentKey, value);
71 }
72
73 /// <summary>
74 /// 设置当前缓存实例
75 /// </summary>
76 /// <param name="value"></param>
77 public void Set(object value)
78 {
79 base.Set(this.CurrentKey, value);
80 }
81
82 /// <summary>
83 /// 移除当前缓存实例
84 /// </summary>
85 /// <returns></returns>
86 public object Remove()
87 {
88 return base.Remove(this.CurrentKey);
89 }
90
91 /// <summary>
92 /// 清空当前缓存
93 /// </summary>
94 public void Clear()
95 {
96 base.Clear(this.CurrentKey);
97 }
98 }
99 }
四、Librame.Caching.FileInstanceCacher(文件实例缓存器)
继承自 Librame.Caching.InstanceCacher,对所有文件类实例提供支持。样例见 Librame.Settings 配置功能。