YUI 3.x Home -

Site Search (YDN & YUIBlog):

# 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.

View example in new window.

Cache max: Enforce unique keys:
Key: Value:
Key:

    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();});

    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();
    });

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