YUI 3.x Home -

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

  1. YUI().use("datasource-get", "datasource-jsonschema", "datasource-cache", function(Y) {
  2. var source = "remote source",
  3. myDataSource = new Y.DataSource.Get({
  4. source:"http://query.yahooapis.com/v1/public/yql?format=json&"}),
  5. callback = {
  6. success: function(e){
  7. alert(e.response);
  8. },
  9. failure: function(e){
  10. alert("Could not retrieve data: " + e.error.message);
  11. }
  12. };
  13.  
  14. myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  15. schema: {
  16. resultListLocator: "query.results.result",
  17. resultFields: ["title"]
  18. }
  19. });
  20.  
  21. myDataSource.plug(Y.Plugin.DataSourceCache, {max:3});
  22.  
  23. // Adds to cache
  24. myDataSource.sendRequest(
  25. "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback);
  26. // Adds to cache
  27. myDataSource.sendRequest(
  28. "q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",callback);
  29. // Retrieves from cache
  30. myDataSource.sendRequest(
  31. "q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",callback);
  32. });
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