YUI 3.x Home -

YUI Library Examples: DataSource Utility: DataSource with Offline Cache

DataSource Utility: DataSource with Offline Cache

The DataSourceCache plugin enables caching on any DataSource to reduce high-latency calls to remote sources and to reduce server load.< The plugin can point to Y.OfflineCache to allow cached data to persist across browser sessions in browsers that support HTML5 localStorage.

Send request to Yahoo! Web Search Webservice:

Use the plug() method to initialize the DataSourceCache plugin and point the configuration value cache to Y.CacheOffline to enable offline caching. The configuration value expires can be set to change how soon the data expires.

  1. YUI().use("datasource-get", "datasource-jsonschema", "datasource-cache", "cache-offline", 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. // Enable offline data caching by pointing to Y.CacheOffline.
  22. // For demonstration purposes, data is set to expire after 5 seconds
  23. myDataSource.plug(Y.Plugin.DataSourceCache,
  24. {cache: Y.CacheOffline, expires:5000});
  25.  
  26. // Retrieves from server. Adds to cache
  27. myDataSource.sendRequest({
  28. request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
  29. callback:callback
  30. });
  31. // Retrieves from server. Adds to cache
  32. myDataSource.sendRequest({
  33. request:"q=select%20*%20from%20search.web%20where%20query%3D%22bar%22",
  34. callback:callback
  35. });
  36. // Retrieves from cache
  37. myDataSource.sendRequest({
  38. request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
  39. callback:callback
  40. });
  41.  
  42. // ... wait 5 seconds ...
  43.  
  44. // Cached data has expired. Retrieves from server. Adds to cache.
  45. myDataSource.sendRequest({
  46. request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
  47. callback:callback
  48. });
  49. });
YUI().use("datasource-get", "datasource-jsonschema", "datasource-cache", "cache-offline", 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"]
        }
    });
 
    // Enable offline data caching by pointing to Y.CacheOffline.
    // For demonstration purposes, data is set to expire after 5 seconds
    myDataSource.plug(Y.Plugin.DataSourceCache,
        {cache: Y.CacheOffline, expires:5000});
 
    // Retrieves from server. Adds to cache
    myDataSource.sendRequest({
        request:"q=select%20*%20from%20search.web%20where%20query%3D%22foo%22",
        callback:callback
    });
    // Retrieves from server. 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
    });
 
    // ... wait 5 seconds ...
 
    // Cached data has expired. Retrieves from server. Adds to 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