datasource-caching.mustache revision 72378c4b11f0468874fbad14749246d2ae564474
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff<style scoped>
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews/* custom styles for this example */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#demo .output {
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews margin-bottom:1em;
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews padding:10px;
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews border:1px solid #D9D9D9;
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff}
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff#demo .output pre {
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater font-size: 11px;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#demo .output strong {
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence padding: .25em .4em;
9416e9751e522256dd6dd60d56cb4e8e8f549149Andreas Gustafsson background: #333;
9416e9751e522256dd6dd60d56cb4e8e8f549149Andreas Gustafsson color: #fff;
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews text-shadow: -1px -1px 1px #000;
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff border-radius: 5px;
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff}
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff</style>
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff
364a82f7c25b62967678027043425201a5e5171aBob Halley<div class="intro">
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff <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`.
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff</div>
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff<div class="example">
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff {{>datasource-caching-source}}
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff</div>
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff<p>Use the <code>plug()</code> method to initialize the
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff<code>DataSourceCache</code> plugin and pass in the configuration value
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff<code>max</code> to set the maximum size.</p>
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff```
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael GraffYUI().use("datasource", "dataschema", "cache", function (Y) {
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff var callback = {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff success: function (e) { /* output to screen */ },
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff failure: function (e) { /* output to screen */ }
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff },
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff myDataSource = new Y.DataSource.Get({
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff source: "https://api.github.com/users/",
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff // this is only needed because the query appends the url
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff // rather than the url's query params
21cbbc3e8301f3959fa1c37d1ee7d597e15c5bf3Michael Graff generateRequestCallback: function (guid) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid;
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff }
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff }),
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff schema: {
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff resultListLocator: "data",
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff resultFields: ["name"]
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff }
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff });
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff myDataSource.plug(Y.Plugin.DataSourceCache, { max: 3 });
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff // Adds to cache
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff myDataSource.sendRequest({
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff request : "lsmith",
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff callback: callback
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff });
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff // Adds to cache
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff myDataSource.sendRequest({
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff request : "davglass",
767fd19da4b699aae46959d834f53af0e1166e80Mark Andrews callback: callback
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff });
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff // Retrieves from cache
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff myDataSource.sendRequest({
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff request : "lsmith",
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff callback: callback
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff });
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff});
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff```
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff<h3 id="fullsource">Full Example Source Listing</h3>
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff```
ad91a3d204891c4b039606f9b5ef557d9036f8c8Michael Graff {{>datasource-caching-source}}
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff```
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff