YUI 3.x Home -

YUI Library Examples: Cache Utility: Offline Caching

Cache Utility: Offline Caching

OfflineCache stores data in HTML 5 localStorage when available so that data persists across browser sessions. When localStorage is unavailable (i.e., IE6 and IE7) data is simply cached in local JavaScript memory and will not persist across browser sessions.
CacheOffline Instance #1
CacheOffline Instance #2
  1. YUI().use("cache-offline", function(Y) {
  2. var cache = new Y.CacheOffline({
  3. sandbox:"6-hr-cache", // Pass in a unique identifier
  4. expires:21600000 // Expire data after 6 hours
  5. });
  6.  
  7. // Add entries to the Cache
  8. cache.add("key1", "value1");
  9. cache.add("key2", "value2");
  10.  
  11. // Retrieve a cached entry
  12. var cachedentry = cache.retrieve("key1");
  13.  
  14. // Cached entry is an object with the following properties
  15. alert("cached key: " + cachedentry.request +
  16. " cached value: " + cachedentry.response +
  17. " cached at: " + cachedentry.cached +
  18. " expires at: " + cachedentry.expires);
  19.  
  20. // Flush the cache
  21. cache.flush();
  22. });
YUI().use("cache-offline", function(Y) {
    var cache = new Y.CacheOffline({
        sandbox:"6-hr-cache", // Pass in a unique identifier
        expires:21600000 // Expire data after 6 hours
    });
 
    // 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 the following properties
    alert("cached key: " + cachedentry.request +
        " cached value: " + cachedentry.response +
        " cached at: " + cachedentry.cached +
        " expires at: " + cachedentry.expires);
 
    // Flush the cache
    cache.flush();
});

Copyright © 2010 Yahoo! Inc. All rights reserved.

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