5
<title>Example: Offline Caching</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Offline Caching</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>OfflineCache stores data in HTML 5 localStorage when available so that data persists across browser sessions. When localStorage is disabled or altogether unavailable (i.e., IE6 and IE7) data is simply cached in local JavaScript memory and will not persist across browser sessions.</p>
26
/* custom styles for this example */
27
.demo fieldset {display:block; border:0;}
28
.demo .short {width:2em;}
31
<form id="demo1" class="demo">
32
<h6>CacheOffline Instance #1</h6>
34
<label for="demo1_expires">Data expires after: <input type="text" id="demo1_expires" class="med" value="86400000"> milliseconds
35
<input type="button" id="demo1_setExpires" value="Set expires"></label>
38
<label for="demo1_addKey">Key: <input type="text" id="demo1_addKey"></label>
39
<label for="demo1_addValue">Value: <input type="text" id="demo1_addValue"></label>
40
<input type="button" id="demo1_add" value="Cache value">
43
<label for="demo1_retrieveKey">Key: <input type="text" id="demo1_retrieveKey"></label>
44
<input type="button" id="demo1_retrieve" value="Retrieve value">
47
<input type="button" id="demo1_flush" value="Flush cache"></label>
51
<form id="demo2" class="demo">
52
<h6>CacheOffline Instance #2</h6>
54
<label for="demo2_expires">Data expires after: <input type="text" id="demo2_expires" class="med" value="86400000"> milliseconds
55
<input type="button" id="demo2_setExpires" value="Set expires"></label>
58
<label for="demo2_addKey">Key: <input type="text" id="demo2_addKey"></label>
59
<label for="demo2_addValue">Value: <input type="text" id="demo2_addValue"></label>
60
<input type="button" id="demo2_add" value="Cache value">
63
<label for="demo2_retrieveKey">Key: <input type="text" id="demo2_retrieveKey"></label>
64
<input type="button" id="demo2_retrieve" value="Retrieve value">
67
<input type="button" id="demo2_flush" value="Flush cache"></label>
73
YUI().use("node", "datatype-number", "cache-offline" ,function (Y) {
74
var cache1 = new Y.CacheOffline({sandbox:"instance1"});
76
Y.on("click", function(e){
77
cache1.set("expires", Y.DataType.Number.parse(Y.one("#demo1_expires").get("value")));
78
alert("Cache 1 \"expires\" set to " + cache1.get("expires") + ".");
79
}, "#demo1_setExpires");
81
Y.on("click", function(e){
82
cache1.add(Y.one("#demo1_addKey").get("value"), Y.one("#demo1_addValue").get("value"));
83
var msg = "Value cached. Cache 1 \"size\" is now " + cache1.get("size") + ".";
87
Y.on("click", function(e){
88
var entry = cache1.retrieve(Y.one("#demo1_retrieveKey").get("value")),
89
output = entry ? entry.response : "Value not cached.";
91
}, "#demo1_retrieve");
93
Y.on("click", function(e){
95
alert("Cache 1 flushed.");
98
var cache2 = new Y.CacheOffline({sandbox:"instance2"});
100
Y.on("click", function(e){
101
cache2.set("expires", Y.DataType.Number.parse(Y.one("#demo2_expires").get("value")));
102
alert("Cache 2 \"expires\" set to " + cache2.get("expires") + ".");
103
}, "#demo2_setExpires");
105
Y.on("click", function(e){
106
cache2.add(Y.one("#demo2_addKey").get("value"), Y.one("#demo2_addValue").get("value"));
107
var msg = "Value cached. Cache 2 \"size\" is now " + cache2.get("size") + ".";
111
Y.on("click", function(e){
112
var entry = cache2.retrieve(Y.one("#demo2_retrieveKey").get("value")),
113
output = entry ? entry.response : "Value not cached.";
115
}, "#demo2_retrieve");
117
Y.on("click", function(e){
119
alert("Cache 2 flushed.");
126
<pre class="code prettyprint">YUI().use("cache-offline", function(Y) {
127
var cache = new Y.CacheOffline({
128
sandbox:"6-hr-cache", // Pass in a unique identifier
129
expires:21600000 // Expire data after 6 hours
132
// Add entries to the Cache
133
cache.add("key1", "value1");
134
cache.add("key2", "value2");
136
// Retrieve a cached entry
137
var cachedentry = cache.retrieve("key1");
139
// Cached entry is an object with the following properties
140
alert("cached key: " + cachedentry.request +
141
" cached value: " + cachedentry.response +
142
" cached at: " + cachedentry.cached +
143
" expires at: " + cachedentry.expires);
145
// Flush the cache
150
<h2>Complete Example Source</h2>
152
<pre class="code prettyprint"><style scoped>
153
/* custom styles for this example */
154
.demo fieldset {display:block; border:0;}
155
.demo .short {width:2em;}
158
<form id="demo1" class="demo">
159
<h6>CacheOffline Instance #1</h6>
161
<label for="demo1_expires">Data expires after: <input type="text" id="demo1_expires" class="med" value="86400000"> milliseconds
162
<input type="button" id="demo1_setExpires" value="Set expires"></label>
163
</fieldset>
165
<label for="demo1_addKey">Key: <input type="text" id="demo1_addKey"></label>
166
<label for="demo1_addValue">Value: <input type="text" id="demo1_addValue"></label>
167
<input type="button" id="demo1_add" value="Cache value">
168
</fieldset>
170
<label for="demo1_retrieveKey">Key: <input type="text" id="demo1_retrieveKey"></label>
171
<input type="button" id="demo1_retrieve" value="Retrieve value">
172
</fieldset>
174
<input type="button" id="demo1_flush" value="Flush cache"></label>
175
</fieldset>
178
<form id="demo2" class="demo">
179
<h6>CacheOffline Instance #2</h6>
181
<label for="demo2_expires">Data expires after: <input type="text" id="demo2_expires" class="med" value="86400000"> milliseconds
182
<input type="button" id="demo2_setExpires" value="Set expires"></label>
183
</fieldset>
185
<label for="demo2_addKey">Key: <input type="text" id="demo2_addKey"></label>
186
<label for="demo2_addValue">Value: <input type="text" id="demo2_addValue"></label>
187
<input type="button" id="demo2_add" value="Cache value">
188
</fieldset>
190
<label for="demo2_retrieveKey">Key: <input type="text" id="demo2_retrieveKey"></label>
191
<input type="button" id="demo2_retrieve" value="Retrieve value">
192
</fieldset>
194
<input type="button" id="demo2_flush" value="Flush cache"></label>
195
</fieldset>
200
YUI().use("node", "datatype-number", "cache-offline" ,function (Y) {
201
var cache1 = new Y.CacheOffline({sandbox:"instance1"});
203
Y.on("click", function(e){
204
cache1.set("expires", Y.DataType.Number.parse(Y.one("#demo1_expires").get("value")));
205
alert("Cache 1 \"expires\" set to " + cache1.get("expires") + ".");
206
}, "#demo1_setExpires");
208
Y.on("click", function(e){
209
cache1.add(Y.one("#demo1_addKey").get("value"), Y.one("#demo1_addValue").get("value"));
210
var msg = "Value cached. Cache 1 \"size\" is now " + cache1.get("size") + ".";
212
}, "#demo1_add");
214
Y.on("click", function(e){
215
var entry = cache1.retrieve(Y.one("#demo1_retrieveKey").get("value")),
216
output = entry ? entry.response : "Value not cached.";
218
}, "#demo1_retrieve");
220
Y.on("click", function(e){
222
alert("Cache 1 flushed.");
223
}, "#demo1_flush");
225
var cache2 = new Y.CacheOffline({sandbox:"instance2"});
227
Y.on("click", function(e){
228
cache2.set("expires", Y.DataType.Number.parse(Y.one("#demo2_expires").get("value")));
229
alert("Cache 2 \"expires\" set to " + cache2.get("expires") + ".");
230
}, "#demo2_setExpires");
232
Y.on("click", function(e){
233
cache2.add(Y.one("#demo2_addKey").get("value"), Y.one("#demo2_addValue").get("value"));
234
var msg = "Value cached. Cache 2 \"size\" is now " + cache2.get("size") + ".";
236
}, "#demo2_add");
238
Y.on("click", function(e){
239
var entry = cache2.retrieve(Y.one("#demo2_retrieveKey").get("value")),
240
output = entry ? entry.response : "Value not cached.";
242
}, "#demo2_retrieve");
244
Y.on("click", function(e){
246
alert("Cache 2 flushed.");
247
}, "#demo2_flush");
249
</script></pre>
254
<div id="sidebar" class="yui3-u">
258
<div class="sidebox">
260
<h2 class="no-toc">Examples</h2>
264
<ul class="examples">
267
<li data-description="Basic caching functionality with the Cache Utility.">
268
<a href="cache-basic.html">Basic Caching</a>
273
<li data-description="Offline caching implements HTML 5 localStorage when available, to allow data to persist across browser sessions.">
274
<a href="cache-offline.html">Offline Caching</a>
288
<div class="sidebox">
290
<h2 class="no-toc">Examples That Use This Component</h2>
294
<ul class="examples">
301
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
302
<a href="../datasource/datasource-caching.html">DataSource with Caching</a>
307
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
308
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a>
320
<script src="../assets/vendor/prettify/prettify-min.js"></script>
321
<script>prettyPrint();</script>