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