|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Basic Caching</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Basic Caching</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>The Cache Utility provides a basic caching mechanism for storing key/value pairs in local JavaScript memory. |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example"> |
|
|
33 |
<style scoped> |
|
|
34 |
/* custom styles for this example */ |
|
|
35 |
#demo fieldset {display:block; border:0;} |
|
|
36 |
#demo .short {width:2em;} |
|
|
37 |
#out {border:1px solid #CCC; padding:1em} |
|
|
38 |
</style> |
|
|
39 |
|
|
|
40 |
<form id="demo"> |
|
|
41 |
<fieldset> |
|
|
42 |
<label for="demo_max">Cache max: <input type="text" id="demo_max" class="short" value="0"> <input type="button" id="demo_setMax" value="Set max"></label> |
|
|
43 |
<label for="demo_setUniqueKeys">Enforce unique keys: <input type="checkbox" id="demo_setUniqueKeys"></label> |
|
|
44 |
</fieldset> |
|
|
45 |
<fieldset> |
|
|
46 |
<label for="demo_expires">Data expires after: <input type="text" id="demo_expires" class="med" value="86400000"> milliseconds |
|
|
47 |
<input type="button" id="demo_setExpires" value="Set expires"></label> |
|
|
48 |
</fieldset> |
|
|
49 |
<fieldset> |
|
|
50 |
<label for="demo_addKey">Key: <input type="text" id="demo_addKey"></label> |
|
|
51 |
<label for="demo_addValue">Value: <input type="text" id="demo_addValue"></label> |
|
|
52 |
<input type="button" id="demo_add" value="Cache value"> |
|
|
53 |
</fieldset> |
|
|
54 |
<fieldset> |
|
|
55 |
<label for="demo_retrieveKey">Key: <input type="text" id="demo_retrieveKey"></label> |
|
|
56 |
<input type="button" id="demo_retrieve" value="Retrieve value"> |
|
|
57 |
</fieldset> |
|
|
58 |
<fieldset> |
|
|
59 |
<input type="button" id="demo_flush" value="Flush cache"></label> |
|
|
60 |
</fieldset> |
|
|
61 |
</form> |
|
|
62 |
<div id="out">(cache results here)</div> |
|
|
63 |
|
|
|
64 |
<script> |
|
|
65 |
YUI().use("node", "datatype-number", "cache-base", "escape" , function (Y) { |
|
|
66 |
var cache = new Y.Cache(), |
|
|
67 |
out = Y.one('#out'), |
|
|
68 |
escape = Y.Escape.html; |
|
|
69 |
|
|
|
70 |
Y.on("click", function(e){ |
|
|
71 |
cache.set("max", Y.DataType.Number.parse(Y.one("#demo_max").get("value"))); |
|
|
72 |
var msg = "Cache max set to " + cache.get("max") + "."; |
|
|
73 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
74 |
}, "#demo_setMax"); |
|
|
75 |
|
|
|
76 |
Y.on("click", function(e){ |
|
|
77 |
cache.set("uniqueKeys", Y.one("#demo_setUniqueKeys").get("checked")); |
|
|
78 |
var msg = "Cache uniqueKeys set to " + cache.get("uniqueKeys") + "."; |
|
|
79 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
80 |
}, "#demo_setUniqueKeys"); |
|
|
81 |
|
|
|
82 |
Y.on("click", function(e){ |
|
|
83 |
cache.set("expires", Y.DataType.Number.parse(Y.one("#demo_expires").get("value"))); |
|
|
84 |
var msg = "Cache \"expires\" set to " + cache.get("expires") + "."; |
|
|
85 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
86 |
}, "#demo_setExpires"); |
|
|
87 |
|
|
|
88 |
Y.on("click", function(e){ |
|
|
89 |
cache.add(Y.one("#demo_addKey").get("value"), Y.one("#demo_addValue").get("value")); |
|
|
90 |
var msg = cache.get("max") ? |
|
|
91 |
"Value cached. Cache size is now " + cache.get("size") + "." : |
|
|
92 |
"Cache max is " + cache.get("max") + ". Value not cached." |
|
|
93 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
94 |
}, "#demo_add"); |
|
|
95 |
|
|
|
96 |
Y.on("click", function(e){ |
|
|
97 |
var entry = cache.retrieve(Y.one("#demo_retrieveKey").get("value")), |
|
|
98 |
msg = entry ? entry.response : "Value not cached."; |
|
|
99 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
100 |
}, "#demo_retrieve"); |
|
|
101 |
|
|
|
102 |
Y.on("click", function(e){ |
|
|
103 |
cache.flush(); |
|
|
104 |
out.setHTML("Cache flushed."); |
|
|
105 |
}, "#demo_flush"); |
|
|
106 |
}); |
|
|
107 |
</script> |
|
|
108 |
|
|
|
109 |
</div> |
|
|
110 |
|
|
|
111 |
<h2>Complete Example Source</h2> |
|
|
112 |
|
|
|
113 |
<pre class="code prettyprint"><style scoped> |
|
|
114 |
/* custom styles for this example */ |
|
|
115 |
#demo fieldset {display:block; border:0;} |
|
|
116 |
#demo .short {width:2em;} |
|
|
117 |
#out {border:1px solid #CCC; padding:1em} |
|
|
118 |
</style> |
|
|
119 |
|
|
|
120 |
<form id="demo"> |
|
|
121 |
<fieldset> |
|
|
122 |
<label for="demo_max">Cache max: <input type="text" id="demo_max" class="short" value="0"> <input type="button" id="demo_setMax" value="Set max"></label> |
|
|
123 |
<label for="demo_setUniqueKeys">Enforce unique keys: <input type="checkbox" id="demo_setUniqueKeys"></label> |
|
|
124 |
</fieldset> |
|
|
125 |
<fieldset> |
|
|
126 |
<label for="demo_expires">Data expires after: <input type="text" id="demo_expires" class="med" value="86400000"> milliseconds |
|
|
127 |
<input type="button" id="demo_setExpires" value="Set expires"></label> |
|
|
128 |
</fieldset> |
|
|
129 |
<fieldset> |
|
|
130 |
<label for="demo_addKey">Key: <input type="text" id="demo_addKey"></label> |
|
|
131 |
<label for="demo_addValue">Value: <input type="text" id="demo_addValue"></label> |
|
|
132 |
<input type="button" id="demo_add" value="Cache value"> |
|
|
133 |
</fieldset> |
|
|
134 |
<fieldset> |
|
|
135 |
<label for="demo_retrieveKey">Key: <input type="text" id="demo_retrieveKey"></label> |
|
|
136 |
<input type="button" id="demo_retrieve" value="Retrieve value"> |
|
|
137 |
</fieldset> |
|
|
138 |
<fieldset> |
|
|
139 |
<input type="button" id="demo_flush" value="Flush cache"></label> |
|
|
140 |
</fieldset> |
|
|
141 |
</form> |
|
|
142 |
<div id="out">(cache results here)</div> |
|
|
143 |
|
|
|
144 |
<script> |
|
|
145 |
YUI().use("node", "datatype-number", "cache-base", "escape" , function (Y) { |
|
|
146 |
var cache = new Y.Cache(), |
|
|
147 |
out = Y.one('#out'), |
|
|
148 |
escape = Y.Escape.html; |
|
|
149 |
|
|
|
150 |
Y.on("click", function(e){ |
|
|
151 |
cache.set("max", Y.DataType.Number.parse(Y.one("#demo_max").get("value"))); |
|
|
152 |
var msg = "Cache max set to " + cache.get("max") + "."; |
|
|
153 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
154 |
}, "#demo_setMax"); |
|
|
155 |
|
|
|
156 |
Y.on("click", function(e){ |
|
|
157 |
cache.set("uniqueKeys", Y.one("#demo_setUniqueKeys").get("checked")); |
|
|
158 |
var msg = "Cache uniqueKeys set to " + cache.get("uniqueKeys") + "."; |
|
|
159 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
160 |
}, "#demo_setUniqueKeys"); |
|
|
161 |
|
|
|
162 |
Y.on("click", function(e){ |
|
|
163 |
cache.set("expires", Y.DataType.Number.parse(Y.one("#demo_expires").get("value"))); |
|
|
164 |
var msg = "Cache \"expires\" set to " + cache.get("expires") + "."; |
|
|
165 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
166 |
}, "#demo_setExpires"); |
|
|
167 |
|
|
|
168 |
Y.on("click", function(e){ |
|
|
169 |
cache.add(Y.one("#demo_addKey").get("value"), Y.one("#demo_addValue").get("value")); |
|
|
170 |
var msg = cache.get("max") ? |
|
|
171 |
"Value cached. Cache size is now " + cache.get("size") + "." : |
|
|
172 |
"Cache max is " + cache.get("max") + ". Value not cached." |
|
|
173 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
174 |
}, "#demo_add"); |
|
|
175 |
|
|
|
176 |
Y.on("click", function(e){ |
|
|
177 |
var entry = cache.retrieve(Y.one("#demo_retrieveKey").get("value")), |
|
|
178 |
msg = entry ? entry.response : "Value not cached."; |
|
|
179 |
out.setHTML(escape(msg)); // Escape user input for example. |
|
|
180 |
}, "#demo_retrieve"); |
|
|
181 |
|
|
|
182 |
Y.on("click", function(e){ |
|
|
183 |
cache.flush(); |
|
|
184 |
out.setHTML("Cache flushed."); |
|
|
185 |
}, "#demo_flush"); |
|
|
186 |
}); |
|
|
187 |
</script></pre> |
|
|
188 |
|
|
|
189 |
</div> |
|
|
190 |
</div> |
|
|
191 |
</div> |
|
|
192 |
|
|
|
193 |
<div class="yui3-u-1-4"> |
|
|
194 |
<div class="sidebar"> |
|
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
<div class="sidebox"> |
|
|
199 |
<div class="hd"> |
|
|
200 |
<h2 class="no-toc">Examples</h2> |
|
|
201 |
</div> |
|
|
202 |
|
|
|
203 |
<div class="bd"> |
|
|
204 |
<ul class="examples"> |
|
|
205 |
|
|
|
206 |
|
|
|
207 |
<li data-description="Basic caching functionality with the Cache Utility."> |
|
|
208 |
<a href="cache-basic.html">Basic Caching</a> |
|
|
209 |
</li> |
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
<li data-description="Offline caching implements HTML 5 localStorage when available, to allow data to persist across browser sessions."> |
|
|
214 |
<a href="cache-offline.html">Offline Caching</a> |
|
|
215 |
</li> |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
</ul> |
|
|
223 |
</div> |
|
|
224 |
</div> |
|
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
<div class="sidebox"> |
|
|
229 |
<div class="hd"> |
|
|
230 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
231 |
</div> |
|
|
232 |
|
|
|
233 |
<div class="bd"> |
|
|
234 |
<ul class="examples"> |
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
242 |
<a href="../datasource/datasource-caching.html">DataSource with Caching</a> |
|
|
243 |
</li> |
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
248 |
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
249 |
</li> |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
</ul> |
|
|
253 |
</div> |
|
|
254 |
</div> |
|
|
255 |
|
|
|
256 |
</div> |
|
|
257 |
</div> |
|
|
258 |
</div> |
|
|
259 |
</div> |
|
|
260 |
|
|
|
261 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
262 |
<script>prettyPrint();</script> |
|
|
263 |
|
|
|
264 |
<script> |
|
|
265 |
YUI.Env.Tests = { |
|
|
266 |
examples: [], |
|
|
267 |
project: '../assets', |
|
|
268 |
assets: '../assets/cache', |
|
|
269 |
name: 'cache-basic', |
|
|
270 |
title: 'Basic Caching', |
|
|
271 |
newWindow: '', |
|
|
272 |
auto: false |
|
|
273 |
}; |
|
|
274 |
YUI.Env.Tests.examples.push('cache-basic'); |
|
|
275 |
YUI.Env.Tests.examples.push('cache-offline'); |
|
|
276 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
277 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
278 |
|
|
|
279 |
</script> |
|
|
280 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
</body> |
|
|
285 |
</html> |