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>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
23
<p>The Cache Utility provides a basic caching mechanism for storing key/value pairs in local JavaScript memory. As a subclass of Plugin, it is designed to seamlessly integrate with other components (such as <a href="../datasource/" title="YUI 3: DataSource">DataSource</a>).</p>
26
<h2 id="upgrade">Upgrade Notes</h2>
29
<dt>Cache as a plugin has changed</dt>
30
<dd>Implementers upgrading from version 3.1.1 or earlier who are using Cache as a plugin now need to point to Y.Plugin.Cache instead of Y.Cache.</dd>
33
<h2 id="getting-started">Getting Started</h2>
36
To include the source files for Cache and its dependencies, first load
37
the YUI seed file if you haven't already loaded it.
40
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script></pre>
44
Next, create a new YUI instance for your application and populate it with the
45
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
46
YUI will automatically load any dependencies required by the modules you
50
<pre class="code prettyprint">// Create a new YUI instance and populate it with the required modules.
51
YUI().use('cache', function (Y) {
52
// Cache is available and ready for use. Add implementation
53
// code here.
58
For more information on creating YUI instances and on the
59
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
60
documentation for the <a href="../yui/index.html">YUI Global object</a>.
64
<h2 id="using">Using the Cache</h2>
66
<p>This section describes how to use the Cache in further detail.</p>
68
<h3 id="base">Basic caching</h3>
70
<p>Basic caching allows you to store frequently used data in local JavaScript memory. In cases where data is retrieved over the wire from a server, you can store the response in a local cache and eliminate future trips to the server for better performance and reduced server load.</p>
72
<p>Use the following Attributes to configure your Cache instance:</p>
84
<td><code>max</code></td>
85
<td><code>0</code></td>
86
<td>The maximum number of entries the cache can hold. <strong>The cache is disabled by default!</strong> Set this value to a number greater than zero to turn on caching.</td>
89
<td><code>size</code></td>
91
<td>Read-only. Returns the number of entries currently stored in the cache.</td>
94
<td><code>entries</code></td>
96
<td>Read-only. Returns an array of the entries currently stored in the cache.</td>
99
<td><code>expires</code></td>
100
<td><code>0</code></td>
101
<td>By default, expiration is disabled. To enable expiration of data, set this value to the Date when the data should expire, or the relative number of milliseconds from collection that the data should expire.</td>
104
<td><code>uniqueKeys</code></td>
105
<td><code>false</code></td>
106
<td>When calling add() with an entry, checks to see if the key is already stored in the cache. Enforcing unique keys requires iterating through all stored entries, so setting this attribute to <code>false</code> is more performant. Note: If expiration is enabled, you should probably set uniqueKeys to <code>false</code> to avoid problems if data is cached multiple times with conflicting expirations.</td>
111
<p>Here are the ways you can set the <code>max</code> value</strong> of a cache. <strong>You must set a <code>max</code> value to turn on caching.</strong></p>
113
<pre class="code prettyprint">// Configure Cache maximum size in the constructor
114
var myCache = new Y.Cache({max:5});
116
// Set the maximum size at runtime
117
myCache.set("max", 10);</pre>
120
<p><strong>Cache key/value pairs with the <code>add()</code> method.</strong></p>
122
<pre class="code prettyprint">// Add entries to the Cache
123
myCache.add("key1", "value1");</pre>
125
<p><strong>Retrieve cached entries with the <code>retrieve()</code> method.</strong> If there is no match for the given key, then <code>null</code> will be returned. Cached entries contain the properties <code>request</code>, <code>response</code>, <code>cached</code>, and <code>expires</code>.</p>
127
<pre class="code prettyprint">// Retrieve a cached entry
128
var cachedEntry = cache.retrieve("key1");</pre>
131
<p>By default, cached entries may contain duplicate keys: if you add an entry for request "foo" with value "bar" and then add another entry for request "foo" with value "bat", the cache will contain both entries. Retrieving an entry for request "foo" will only retrieve the newest value. However, setting <code>uniqueKeys</code> to <code>true</code> will validate for the uniqueness of keys stored in the cache.</p>
133
<pre class="code prettyprint">// Enforce unique keys in the constructor
134
var myCache = new Y.Cache({max:5, uniqueKeys:true});
136
// Enforce unique keys at runtime
137
myCache.set("uniqueKeys", true);</pre>
140
<p>A cache may be flushed of all its entries with the <code>flush()</code> method.</p>
142
<pre class="code prettyprint">// Flush the cache
143
myCache.flush();</pre>
145
<h3 id="offline">Offline caching</h3>
147
<p>The CacheOffline cache extends basic caching functionality by storing data in the <a href="http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage" title="Storing Objects in HTML5 localStorage - Stack Overflow">HTML5 localStorage object</a> on browsers where that is supported, so that data can be available when the browser is offline and can persist across browser sessions. In cases where HTML5 localStorage is not available (i.e., IE 6 and IE 7), the functionality falls back to basic caching in local JavaScript memory.</p>
149
<pre class="code prettyprint">var myCache = new Y.CacheOffline();</pre>
152
<p>Use the following Attributes to configure your CacheOffline instance:</p>
164
<td><code>sandbox</code></td>
165
<td><code>"default"</code></td>
166
<td>A unique identifier used to sandbox each cache instance's entries from other entries. This string persists across browser sessions so take care that it is not dynamically generated.</td>
169
<td><code>expires</code></td>
170
<td><code>86400000</code></td>
171
<td>By default, data will expire one day after it is cached. This Attribute accepts a Data value, or a number of milliseconds.</td>
174
<td><code>max</code></td>
175
<td><code>null</code></td>
176
<td>Read-only. This Attribute is disabled for CacheOffline — there is no notion of capping the number of entries in a cache. Each browser implements a maximum localStorage byte size.</td>
179
<td><code>uniqueKeys</code></td>
180
<td><code>true</code></td>
181
<td>Read-only. This Attribute is disabled for CacheOffline. All stored keys are unique.</td>
186
<h3 id="plugin">Cache as a plugin</h3>
188
<p>Use the <code>plug(Y.Plugin.Cache)</code> method on a host object to enable caching. The Y.Plugin.Cache class is available in the "cache-plugin" submodule.</p>
190
<pre class="code prettyprint">// Define a max value to enable plugging.
191
myWidget.plug(Y.Plugin.Cache, {max:3});
192
myWidget.cache.add("key", "value");</pre>
195
<p>The Cache plugin also accepts a <code>cache</code> value to enable you to point to any number of Cache implementations. Y.Cache and Y.CacheOffline are currently available.</p>
197
<pre class="code prettyprint">// Define a max value to enable plugging.
198
myWidget.plug(Y.Plugin.Cache, {cache:Y.CacheOffline});</pre>
201
<p>The Y.Plugin.DataSourceCache plugin enables seamless caching of DataSource responses.</p>
203
<pre class="code prettyprint">// Use a basic cache
204
myDataSource.plug(Y.Plugin.DataSourceCache, {
205
cache: Y.Cache, // this is the default, this line is not needed
209
// Use the "cache" configuration property to enable offline caching
210
myDataSource.plug(Y.Plugin.DataSourceCache, {
211
cache: Y.CacheOffline,
212
sandbox: "my3HrCache",
213
expires: 10800000 // 3 hours
217
<p>Once the DataSourceCache plugin is enabled, it handles caching and retrieval of values without the need for extra code. Furthermore, all the methods and properties of the Cache instance is available on the host's <code>cache</code> namespace.</p>
219
<pre class="code prettyprint">// Flush myDataSource's cache.
220
myDataSource.cache.flush();</pre>
223
<h3 id="events">Events</h3>
230
<th class="wrap">Properties available on the Event facade passed to handler</th>
235
<td><code>add</code></td>
236
<td>Entry is added to the cache.</td>
238
<dt><code>entry</code></dt>
239
<dd>The cached entry.</dd>
243
<td><code>request</code></td>
244
<td>Entry is requested from the cache.</td>
246
<dt><code>request</code></dt>
247
<dd>The request value.</dd>
251
<td><code>retrieve</code></td>
252
<td>Entry is retrieved from the cache.</td>
254
<dt><code>entry</code></dt>
255
<dd>The retrieved entry.</dd>
259
<td><code>flush</code></td>
260
<td>Cache is flushed.</td>
264
<td><code>error</code></td>
265
<td>CacheOffline only. Fired when an entry could not be added, most likely due to exceeding the browser quota for the localStorage object.</td>
267
<dt><code>error</code></dt>
268
<dd>The error object.</dd>
276
<div id="sidebar" class="yui3-u">
278
<div id="toc" class="sidebox">
280
<h2 class="no-toc">Table of Contents</h2>
286
<a href="#upgrade">Upgrade Notes</a>
289
<a href="#getting-started">Getting Started</a>
292
<a href="#using">Using the Cache</a>
295
<a href="#base">Basic caching</a>
298
<a href="#offline">Offline caching</a>
301
<a href="#plugin">Cache as a plugin</a>
304
<a href="#events">Events</a>
314
<div class="sidebox">
316
<h2 class="no-toc">Examples</h2>
320
<ul class="examples">
323
<li data-description="Basic caching functionality with the Cache Utility.">
324
<a href="cache-basic.html">Basic Caching</a>
329
<li data-description="Offline caching implements HTML 5 localStorage when available, to allow data to persist across browser sessions.">
330
<a href="cache-offline.html">Offline Caching</a>
344
<div class="sidebox">
346
<h2 class="no-toc">Examples That Use This Component</h2>
350
<ul class="examples">
357
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
358
<a href="../datasource/datasource-caching.html">DataSource with Caching</a>
363
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
364
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a>
376
<script src="../assets/vendor/prettify/prettify-min.js"></script>
377
<script>prettyPrint();</script>