DataSource: DataSource with Caching
The DataSourceCache plugin enables caching on any DataSource to reduce high-latency calls to remote sources and to reduce server load. In this example, the Cache's
max
value has been set to 3
.
Use the plug()
method to initialize the DataSourceCache 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({ 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 }); });
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 }); });