YUI 3.x Home -

YUI Library Examples: Cache Utility: Basic Caching

Cache Utility: Basic Caching

The Cache Utility provides a basic caching mechanism for storing key/value pairs in local JavaScript memory.
  1. YUI().use("cache", function(Y) {
  2. // Configure Cache maximum size in the constructor
  3. var cache = new Y.Cache({max:5});
  4.  
  5. // Add entries to the Cache
  6. cache.add("key1", "value1");
  7. cache.add("key2", "value2");
  8.  
  9. // Retrieve a cached entry
  10. var cachedentry = cache.retrieve("key1");
  11.  
  12. // Cached entry is an object with a request property and a response property
  13. alert("cached key: " + cachedentry.request +
  14. "/cached value: " + cachedentry.response);
  15.  
  16. // Flush the cache
  17. cache.flush();
  18. });
YUI().use("cache", function(Y) {
    // Configure Cache maximum size in the constructor
    var cache = new Y.Cache({max:5});
 
    // Add entries to the Cache
    cache.add("key1", "value1");
    cache.add("key2", "value2");
 
    // Retrieve a cached entry
    var cachedentry = cache.retrieve("key1");
 
    // Cached entry is an object with a request property and a response property
    alert("cached key: " + cachedentry.request +
        "/cached value: " + cachedentry.response);
 
    // Flush the cache
    cache.flush();
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings