YUI 3: Cache

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 DataSource).

Getting Started

Include Dependencies

The easiest way to include the source files for Cache and its dependencies is to add the YUI seed file to your page, using the following script tag, and allow the YUI instance to download any additional files which may be required:

  1. <script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>
<script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>

The YUI instance will automatically pull down Cache's source files and any missing dependencies when the cache module is used. This helps you avoid having to manually manage the list of files needed on your page to support multiple components while also optimizing your initial page weight by loading files only when they are required.

If you do want to include file dependencies manually on your page, the YUI Dependency Configurator can be used to determine the list of files you need to include in order to use Cache.

The YUI Instance

Once you have the YUI seed file on your page (yui-min.js), you can create a new YUI instance for your application to use and populate it with the modules you need, specified as the first set of arguments to the use method:

  1. // Create new YUI instance, and populate it with the required modules
  2. YUI().use('cache', function(Y) {
  3.  
  4. // Cache available, and ready for use.
  5.  
  6. });
// Create new YUI instance, and populate it with the required modules
YUI().use('cache', function(Y) {
 
    // Cache available, and ready for use.
 
});

The last argument passed to use is a callback function. The callback function will be invoked as soon as the YUI instance is done downloading any required files missing from your page. Once those files are loaded, your local YUI instance will be supplemented with the classes which make up the cache module and any modules it depends on. A reference to the populated YUI instance (Y) is passed back to your callback function. Within your callback, then, you can start writing your application code based on your own custom instance of YUI.

For more information on creating instances of YUI and the use method, see the YUI Global object documentation.

Using the Cache Utility

This section describes how to use the Cache Utility in further detail. It contains these subsections:

Cache basics

Instantiate the cache with a max configuration value. By default, max is set to 0, which effectively disables caching. The max value can also be set at runtime.

  1. // Configure Cache maximum size in the constructor
  2. var myCache = new Y.Cache({max:5});
  3.  
  4. // Set the maximum size at runtime
  5. myCache.set("max", 10);
  6.  
// Configure Cache maximum size in the constructor
var myCache = new Y.Cache({max:5});
 
// Set the maximum size at runtime
myCache.set("max", 10);
 

Cache key/value pairs with the add() method.

  1. // Add entries to the Cache
  2. myCache.add("key1", "value1");
  3.  
  4. // Entries may include an optional arbitrary payload
  5. myCache.add("key2", "value2", "optional-payload");
  6.  
// Add entries to the Cache
myCache.add("key1", "value1");
 
// Entries may include an optional arbitrary payload
myCache.add("key2", "value2", "optional-payload");
 

Retrieve cached entries with the retrieve() method. If there is no match for the given key, then null will be returned. Cached entries contain the properties request, response, and payload.

  1. // Retrieve a cached entry
  2. var cachedEntry = cache.retrieve("key1");
  3.  
// Retrieve a cached entry
var cachedEntry = cache.retrieve("key1");
 

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 uniqueKeys to true will validate for the uniqueness of keys stored in the cache.

  1. // Enforce unique keys in the constructor
  2. var myCache = new Y.Cache({max:5, uniqueKeys:true});
  3.  
  4. // Enforce unique keys at runtime
  5. myCache.set("uniqueKeys", true);
  6.  
// Enforce unique keys in the constructor
var myCache = new Y.Cache({max:5, uniqueKeys:true});
 
// Enforce unique keys at runtime
myCache.set("uniqueKeys", true);
 

The cache may be flushed of all its entries with the flush() method.

  1. // Flush the cache
  2. myCache.flush();
  3.  
// Flush the cache
myCache.flush();
 

Cache as a plugin

Use the plug() method on the host instance to enable caching.

  1. myDataSource.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
  2.  
myDataSource.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
 

Once the plugin is enabled, it will handle caching and retrieval of values seamlessly for you without the need for extra code. However, all the methods and properties of the Cache instance is available on the host's cache namespace.

  1. // Flush myDataSource's cache.
  2. myDataSource.cache.flush();
  3.  
  4. // Disable myDataSource's cache
  5. myDataSource.cache.set("max", 0);
  6.  
// Flush myDataSource's cache.
myDataSource.cache.flush();
 
// Disable myDataSource's cache
myDataSource.cache.set("max", 0);
 

Events

Event When Properties available on the Event Facade passed to handler
add Entry is added to the Cache.
entry
The cached entry.
request Entry is requested from the Cache.
request
The request value.
retrieve Entry is retrieved from the Cache.
entry
The retrieved entry.
flush Cache is flushed. none

Support & Community

Forums & Blog

YUI 3 discussion forums are hosted on YUILibrary.com.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Copyright © 2010 Yahoo! Inc. All rights reserved. Copyright | Privacy Policy | Terms of Use | Job Openings