YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataSource Utility: DataSource with Caching

## DataSource Utility: DataSource with Caching

The DataSourceCache plugin enables caching on any DataSource to reduce high-latency calls to remote sources and reduce server load. In this example, the Cache's `max` value has been set to `3`.

View example in new window.

###### Send request to Yahoo! Web Search Webservice:

Use the `plug()` method to initialize the DataSourceJSONSchema plugin and pass in the configuration value `max` to set the maximum size.

    YUI().use("datasource-get", "datasource-jsonschema", "datasource-cache", function(Y) {    var source = "remote source",        myDataSource = new Y.DataSource.Get({            source:"http://query.yahooapis.com/v1/public/yql?format=json&"}),        callback = {            success: function(e){                alert(e.response);            },            failure: function(e){                alert("Could not retrieve data: " + e.error.message);            }        };     myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {        schema: {            resultListLocator: "query.results.result",            resultFields: ["title"]        }    });     myDataSource.plug(Y.Plugin.DataSourceCache, {max:3});     // Adds to cache    myDataSource.sendRequest(        "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback);    // Adds to cache    myDataSource.sendRequest(        "q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",callback);    // Retrieves from cache    myDataSource.sendRequest(        "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback);});

    YUI().use("datasource-get", "datasource-jsonschema", "datasource-cache", function(Y) {
        var source = "remote source",
            myDataSource = new Y.DataSource.Get({
                source:"http://query.yahooapis.com/v1/public/yql?format=json&"}),
            callback = {
                success: function(e){
                    alert(e.response);
                },
                failure: function(e){
                    alert("Could not retrieve data: " + e.error.message);
                }
            };

        myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
            schema: {
                resultListLocator: "query.results.result",
                resultFields: ["title"]
            }
        });

        myDataSource.plug(Y.Plugin.DataSourceCache, {max:3});

        // Adds to cache
        myDataSource.sendRequest(
            "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback);
        // Adds to cache
        myDataSource.sendRequest(
            "q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",callback);
        // Retrieves from cache
        myDataSource.sendRequest(
            "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback);
    });

YUI().use("datasource-get", "datasource-jsonschema", "datasource-cache", function(Y) { var source = "remote source", myDataSource = new Y.DataSource.Get({ source:"http://query.yahooapis.com/v1/public/yql?format=json&"}), callback = { success: function(e){ alert(e.response); }, failure: function(e){ alert("Could not retrieve data: " + e.error.message); } }; myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { schema: { resultListLocator: "query.results.result", resultFields: ["title"] } }); myDataSource.plug(Y.Plugin.DataSourceCache, {max:3}); // Adds to cache myDataSource.sendRequest( "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback); // Adds to cache myDataSource.sendRequest( "q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",callback); // Retrieves from cache myDataSource.sendRequest( "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback); });

Copyright © 2009 Yahoo! Inc. All rights reserved.

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