datasource-caching.mustache revision 819e90d415ed17d59af3a247b2ad9d6feb0c21b5
6209N/A<style scoped>
6209N/A/* custom styles for this example */
6209N/A#demo .output {
6209N/A margin-bottom:1em;
6209N/A padding:10px;
6209N/A border:1px solid #D9D9D9;
6209N/A}
6209N/A#demo .output pre {
6209N/A font-size: 11px;
6209N/A}
6209N/A#demo .output strong {
6209N/A padding: .25em .4em;
6209N/A background: #333;
6209N/A color: #fff;
6209N/A text-shadow: -1px -1px 1px #000;
6209N/A border-radius: 5px;
6209N/A}
6209N/A</style>
6209N/A
6209N/A<div class="intro">
6209N/A <p>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`.
6209N/A
6209N/A</div>
6209N/A
6209N/A<div class="example yui3-skin-sam">
6209N/A {{>datasource-caching-source}}
6209N/A</div>
6209N/A
6209N/A<p>Use the <code>plug()</code> method to initialize the
6209N/A<code>DataSourceCache</code> plugin and pass in the configuration value
6209N/A<code>max</code> to set the maximum size.</p>
6209N/A
6209N/A```
6209N/AYUI().use("datasource", "dataschema", "cache", function (Y) {
6209N/A var callback = {
6209N/A success: function (e) { /* output to screen */ },
6209N/A failure: function (e) { /* output to screen */ }
6209N/A },
6209N/A
6209N/A myDataSource = new Y.DataSource.Get({
6209N/A source: "https://api.github.com/users/",
6209N/A
6209N/A // this is only needed because the query appends the url
6209N/A // rather than the url's query params
6209N/A generateRequestCallback: function (guid) {
6209N/A return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid;
6209N/A }
6209N/A }),
6209N/A
6209N/A myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
6209N/A schema: {
6209N/A resultListLocator: "data",
6209N/A resultFields: ["name"]
6209N/A }
6209N/A });
6209N/A
6209N/A myDataSource.plug(Y.Plugin.DataSourceCache, { max: 3 });
6209N/A
6209N/A // Adds to cache
6209N/A myDataSource.sendRequest({
6209N/A request : "lsmith",
6209N/A callback: callback
6209N/A });
6209N/A
6209N/A // Adds to cache
6209N/A myDataSource.sendRequest({
6209N/A request : "davglass",
6209N/A callback: callback
6209N/A });
6209N/A
6209N/A // Retrieves from cache
6209N/A myDataSource.sendRequest({
6209N/A request : "lsmith",
6209N/A callback: callback
6209N/A });
6209N/A});
6209N/A```
6209N/A
6209N/A<h3 id="fullsource">Full Example Source Listing</h3>
6209N/A
6209N/A```
6209N/A {{>datasource-caching-source}}
6209N/A```
6209N/A