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. request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
  26. callback:callback
  27. });
  28. // Adds to cache
  29. myDataSource.sendRequest({
  30. request:"q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",
  31. callback:callback
  32. });
  33. // Retrieves from cache
  34. myDataSource.sendRequest({
  35. request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
  36. callback:callback
  37. });
  38. });
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({
        request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
        callback:callback
    });
    // Adds to cache
    myDataSource.sendRequest({
        request:"q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",
        callback:callback
    });
    // Retrieves from cache
    myDataSource.sendRequest({
        request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
        callback:callback
    });
});

Copyright © 2010 Yahoo! Inc. All rights reserved.

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