In. NET Framework, it is called system. runtime. caching. This is not only a cache library, but also a framework. You can develop your own library on it. Objectcache defines general operations to be implemented for all caches. It works with a memory cache implementation called memorycache. The structure of the cache system is as follows:
Can you see the corresponding products?
The following is a code example for implementing such an architecture. The core of the Code is objectcache:
Define an abstract provider interface:
1: Public interface icachebuilder
2 :{
3: objectcache getinstance ();
4: String defaultregionname {Get ;}
5 :}
The implementation of the in-memory provider uses memorycache:
1: Public class memorycachebuilder: icachebuilder
2 :{
3: Public memorycachebuilder (){}
4:
5: Public objectcache getinstance ()
6 :{
7: Return memorycache. default;
8 :}
9:
10: Public String defaultregionname
11 :{
12: Get {return NULL ;}
13 :}
14 :}
Distributed cache provider memcached:
1: Public class memcachedcache: objectcache, icachebuilder
2 :{
3: Private long _ ldefaultexpiretime = 3600; // default expire time
4: Private memcachedclient _ client = NULL;
5: # region icache members
6:
7: Public memcachedcache ()
8 :{
9: This. _ client = memcachedclientservice. instance. client;
10 :}
11:
12: Public override void set (string key, object value, system. datetimeoffset absoluteexpiration, string regionname = NULL)
13 :{
14: Enforce. notnull (key, "key ");
15: cacheitem item = new cacheitem (Key, value, regionname );
16: cacheitempolicy policy = new cacheitempolicy ();
17: Policy. absoluteexpiration = absoluteexpiration;
18:
19: Set (item, policy );
20 :}
21:
22: Public override void set (cacheitem item, cacheitempolicy Policy)
23 :{
24: If (item = NULL | item. value = NULL)
25: return;
26:
27: item. Key = item. Key. tolower ();
28:
29: If (policy! = NULL & Policy. changemonitors! = NULL & Policy. changemonitors. Count> 0)
30: Throw new notsupportedexception ("Change monitors are not supported ");
31:
32: // Max timeout in scaleout = 65535
33: timespan expire = (policy. absoluteexpiration. Equals (null ))?
34: Policy. slidingexpiration:
35: (policy. absoluteexpiration-datetimeoffset. Now );
36:
37: Double timeout = expire. totalminutes;
38: If (timeout> 65535)
39: timeout = 65535;
40: else if (timeout> 0 & timeout <1)
41: timeout = 1;
42:
43: This. _ client. Store (enyim. caching. memcached. storemode. Set, item. Key. tostring (), item. value );
44:
45 :}
46:
47: Public override object this [String key]
48 :{
49: Get
50 :{
51: return get (key );
52 :}
53: Set
54 :{
55: Set (Key, value, null );
56 :}
57 :}
58:
59: Public override object addorgetexisting (string key, object value, cacheitempolicy policy, string regionname = NULL)
60 :{
61: cacheitem item = getcacheitem (Key, regionname );
62: If (item = NULL)
63 :{
64: Set (New cacheitem (Key, value, regionname), policy );
65: return value;
66 :}
67:
68: Return item. value;
69 :}
70:
71: Public override cacheitem addorgetexisting (cacheitem value, cacheitempolicy Policy)
72 :{
73: cacheitem item = getcacheitem (value. Key, value. regionname );
74: If (item = NULL)
75 :{
76: Set (value, policy );
77: return value;
78 :}
79:
80: Return item;
81 :}
82:
83: Public override object addorgetexisting (string key, object value, system. datetimeoffset absoluteexpiration, string regionname = NULL)
84 :{
85: cacheitem item = new cacheitem (Key, value, regionname );
86: cacheitempolicy policy = new cacheitempolicy ();
87: Policy. absoluteexpiration = absoluteexpiration;
88:
89: Return addorgetexisting (item, policy );
90 :}
91:
92: Public override bool contains (string key, string regionname = NULL)
93 :{
94: Return false;
95 :}
96:
97: Public override cacheentrychangemonitor createcacheentrychangemonitor (system. Collections. Generic. ienumerable <string> keys, string regionname = NULL)
98 :{
99: Throw new system. notimplementedexception ();
100 :}
101:
102: Public override defaultcachecapabilities
103 :{
104: Get
105 :{
106: Return
107: defaultcachecapabilities. outofprocessprovider |
108: defaultcachecapabilities. absoluteexpirations |
109: defaultcachecapabilities. slidingexpirations |
110: defaultcachecapabilities. cacheregions;
111 :}
112 :}
113:
114: Public override object get (string key, string regionname = NULL)
115 :{
116: Key = key. tolower ();
117:
118: return this. _ client. Get (key );
119 :}
120:
121: Public override cacheitem getcacheitem (string key, string regionname = NULL)
122 :{
123: object value = get (Key, regionname );
124: If (value! = NULL)
125: return New cacheitem (Key, value, regionname );
126:
127: return NULL;
128 :}
129:
130: Public override long getcount (string regionname = NULL)
131 :{
132: Return-1;
133 :}
134:
135: protected override system. Collections. Generic. ienumerator <system. Collections. Generic. keyvaluepair <string, Object> getenumerator ()
136 :{
137: Throw new system. notimplementedexception ();
138 :}
139:
140: Public override system. Collections. Generic. idictionary <string, Object> getvalues (system. Collections. Generic. ienumerable <string> keys, string regionname = NULL)
141 :{
142: Throw new system. notimplementedexception ();
143 :}
144:
145: Public override string name
146 :{
147: Get {return "memcachedprovider ";}
148 :}
149:
150: Public override object remove (string key, string regionname = NULL)
151 :{
152: Key = key. tolower ();
153: return this. _ client. Remove (key );
154 :}
155:
156: Public override void set (string key, object value, cacheitempolicy policy, string regionname = NULL)
157 :{
158: Set (New cacheitem (Key, value, regionname), policy );
159 :}
160:
161: # endregion
162:
163: # region icachebuilder members
164:
165: Public objectcache getinstance ()
166 :{
167: return this;
168 :}
169:
170: Public String defaultregionname
171 :{
172: Get {Throw new notimplementedexception ();}
173 :}
174:
175: # endregion
176 :}
Distributed cache provider Windows Server appfabric Caching:
1: Public class appfabriccacheprovider: objectcache, icachebuilder
2 :{
3: Public static datacache factory = NULL;
4: Public static object syncobj = new object ();
5:
6: Public override object addorgetexisting (string key, object value, cacheitempolicy policy, string regionname = NULL)
7 :{
8: cacheitem item = getcacheitem (Key, regionname );
9: If (item = NULL)
10 :{
11: Set (New cacheitem (Key, value, regionname), policy );
12: return value;
13 :}
14:
15: Return item. value;
16 :}
17:
18: Public override cacheitem addorgetexisting (cacheitem value, cacheitempolicy Policy)
19 :{
20: cacheitem item = getcacheitem (value. Key, value. regionname );
21: If (item = NULL)
22 :{
23: Set (value, policy );
24: return value;
25 :}
26:
27: Return item;
28 :}
29:
30: Public override object addorgetexisting (string key, object value, system. datetimeoffset absoluteexpiration, string regionname = NULL)
31 :{
32: cacheitem item = new cacheitem (Key, value, regionname );
33: cacheitempolicy policy = new cacheitempolicy ();
34: Policy. absoluteexpiration = absoluteexpiration;
35:
36: Return addorgetexisting (item, policy );
37 :}
38:
39: Public override bool contains (string key, string regionname = NULL)
40 :{
41: return get (Key, regionname )! = NULL;
42 :}
43:
44: Public override cacheentrychangemonitor createcacheentrychangemonitor (system. Collections. Generic. ienumerable <string> keys, string regionname = NULL)
45 :{
46: Throw new notimplementedexception ();
47 :}
48:
49: Public override defaultcachecapabilities
50 :{
51: Get
52 :{
53: Return
54: defaultcachecapabilities. outofprocessprovider |
55: defaultcachecapabilities. absoluteexpirations |
56: defaultcachecapabilities. slidingexpirations |
57: defaultcachecapabilities. cacheregions;
58 :}
59 :}
60:
61: Public override object get (string key, string regionname = NULL)
62 :{
63: Key = key. tolower ();
64: createregionifneeded ();
65:
66: Return (regionname = NULL )?
67: cachefactory. Get (key ):
68: cachefactory. Get (Key, regionname );
69 :}
70:
71: Public override cacheitem getcacheitem (string key, string regionname = NULL)
72 :{
73: object value = get (Key, regionname );
74: If (value! = NULL)
75: return New cacheitem (Key, value, regionname );
76:
77: return NULL;
78 :}
79:
80: Public override long getcount (string regionname = NULL)
81 :{
82: If (string. isnullorempty (regionname ))
83: Throw new notsupportedexception ();
84:
85: Return cachefactory. getobjectsinregion (regionname). longcount ();
86 :}
87:
88: protected override system. Collections. Generic. ienumerator <system. Collections. Generic. keyvaluepair <string, Object> getenumerator ()
89 :{
90: Throw new notsupportedexception ();
91 :}
92:
93: Public override system. Collections. Generic. idictionary <string, Object> getvalues (system. Collections. Generic. ienumerable <string> keys, string regionname = NULL)
94 :{
95: If (string. isnullorempty (regionname ))
96: Throw new notsupportedexception ();
97:
98: Return cachefactory. getobjectsinregion (regionname). todictionary (x => X. Key, x => X. value );
99 :}
100:
101: Public override string name
102 :{
103: Get {return "appfabric ";}
104 :}
105:
106: Public override object remove (string key, string regionname = NULL)
107 :{
108: Key = key. tolower ();
109: createregionifneeded ();
110:
111: Return (regionname = NULL )?
112: cachefactory. Remove (key ):
113: cachefactory. Remove (Key, regionname );
114 :}
115:
116: Public override void set (string key, object value, cacheitempolicy policy, string regionname = NULL)
117 :{
118: Set (New cacheitem (Key, value, regionname), policy );
119 :}
120:
121: Public override void set (cacheitem item, cacheitempolicy Policy)
122 :{
123: If (item = NULL | item. value = NULL)
124: return;
125:
126: If (policy! = NULL & Policy. changemonitors! = NULL & Policy. changemonitors. Count> 0)
127: Throw new notsupportedexception ("Change monitors are not supported ");
128:
129: item. Key = item. Key. tolower ();
130: createregionifneeded ();
131:
132: timespan expire = (policy. absoluteexpiration. Equals (null ))?
133: Policy. slidingexpiration:
134: (policy. absoluteexpiration-datetimeoffset. Now );
135:
136: If (string. isnullorempty (item. regionname ))
137: cachefactory. Put (item. Key, item. Value, expire );
138: else
139: cachefactory. Put (item. Key, item. Value, expire, item. regionname );
140 :}
141:
142: Private Static datacache cachefactory
143 :{
144: Get
145 :{
146: If (factory = NULL)
147 :{
148: Lock (syncobj)
149 :{
150: If (factory = NULL)
151 :{
152: datachefactory cachefactory = new datacachefactory ();
153: factory = cachefactory. getdefaultcache ();
154 :}
155 :}
156 :}
157:
158: Return factory;
159 :}
160 :}
161:
162: Private void createregionifneeded ()
163 :{
164: Try
165 :{
166: cachefactory. createregion (defaultregionname );
167 :}
168: Catch (datacacheexception ex)
169 :{
170: If (! Ex. errorcode. Equals (datacacheerrorcode. regionalreadyexists ))
171: Throw ex;
172 :}
173 :}
174:
175: Public override void set (string key, object value, system. datetimeoffset absoluteexpiration, string regionname = NULL)
176 :{
177: cacheitem item = new cacheitem (Key, value, regionname );
178: cacheitempolicy policy = new cacheitempolicy ();
179: Policy. absoluteexpiration = absoluteexpiration;
180:
181: Set (item, policy );
182 :}
183:
184: Public override object this [String key]
185 :{
186: Get
187 :{
188: return get (Key, defaultregionname );
189 :}
190: Set
191 :{
192: Set (Key, value, null, defaultregionname );
193 :}
194 :}
195:
196: Public objectcache getinstance ()
197 :{
198: return this;
199 :}
200:
201: Public String defaultregionname
202 :{
203: Get
204 :{
205: String defaultregion = frameworkconfiguationmanager. getconfiguration (). getappvariable ("appfabriccachedefaultregion ");
206: If (string. isnullorempty (defaultregion ))
207 :{
208: defaultregion = "default ";
209 :}
210: Return defaultregion;
211 :}
212 :}
213 :}
The output cache has a great advantage in improving performance. In ASP. NET 4.0, you can customize the output cache policy, such as saving the output to the disk and external memcached services. You can even define some advanced rules. For example, you can use the output Cache Policy for page a to save data to the memory, use the B output Cache Policy for page B to save data to the disk.
For code examples, see http://www.buraksenyurt.com/post/aspnet-40-custom-cache-provider.aspx.
1: <caching>
2: <outputcache defaultprovider = "aspnetinternalprovider">
3: <providers>
4: <Add name = "diskbasedcacheprovider" type = "customcaching. diskcacheprovider, customcaching"/>
5: </providers>
6: </outputcache>
7: </caching>
In the default output Cache Policy of ASP. NET 4. All HTTP responses, displayed pages, and control caches use the default output cache provider shown in the preceding example (the defaultprovider attribute value is aspnetinternalprovider ). You can specify different providers for defaprovider provider. You can change the default output cache provider of Web applications.
In addition, you can select different output cache providers for each user control and each request. To select different output cache providers for different Web user controls, the easiest way is to set the providername attribute added to the page or control commands, as shown in the following example:
<% @ Outputcache duration = "60" varybyparam = "NONE" providername = "diskbasedcacheprovider" %>
To specify different output cache providers for an HTTP request, you can overwrite the newly added getoutputcacheprovidername method in the global. asax file and specify the provider to be used for the specific request programmatically.
See article:. Net 4.0 new Scalable Cache
Http://www.cnblogs.com/shanyou/archive/2010/07/01/1769547.html