|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('cache', function(Y) { |
|
9 |
|
10 /** |
|
11 * The Cache utility provides a common configurable interface for components to |
|
12 * cache and retrieve data from a local JavaScript struct. |
|
13 * |
|
14 * @module cache |
|
15 */ |
|
16 var LANG = Y.Lang, |
|
17 |
|
18 /** |
|
19 * Base class for the YUI Cache utility. |
|
20 * @class Cache |
|
21 * @extends Plugin.Base |
|
22 * @constructor |
|
23 */ |
|
24 Cache = function() { |
|
25 Cache.superclass.constructor.apply(this, arguments); |
|
26 }; |
|
27 |
|
28 ///////////////////////////////////////////////////////////////////////////// |
|
29 // |
|
30 // Cache static properties |
|
31 // |
|
32 ///////////////////////////////////////////////////////////////////////////// |
|
33 Y.mix(Cache, { |
|
34 /** |
|
35 * The namespace for the plugin. This will be the property on the host which |
|
36 * references the plugin instance. |
|
37 * |
|
38 * @property NS |
|
39 * @type String |
|
40 * @static |
|
41 * @final |
|
42 * @value "cache" |
|
43 */ |
|
44 NS: "cache", |
|
45 |
|
46 |
|
47 /** |
|
48 * Class name. |
|
49 * |
|
50 * @property NAME |
|
51 * @type String |
|
52 * @static |
|
53 * @final |
|
54 * @value "cache" |
|
55 */ |
|
56 NAME: "cache", |
|
57 |
|
58 |
|
59 ATTRS: { |
|
60 ///////////////////////////////////////////////////////////////////////////// |
|
61 // |
|
62 // Cache Attributes |
|
63 // |
|
64 ///////////////////////////////////////////////////////////////////////////// |
|
65 |
|
66 /** |
|
67 * @attribute max |
|
68 * @description Maximum number of entries the Cache can hold. |
|
69 * Set to 0 to turn off caching. |
|
70 * @type Number |
|
71 * @default 0 |
|
72 */ |
|
73 max: { |
|
74 value: 0, |
|
75 validator: function(value) { |
|
76 return (LANG.isNumber(value)); |
|
77 }, |
|
78 setter: function(value) { |
|
79 // If the cache is full, make room by removing stalest element (index=0) |
|
80 var entries = this._entries; |
|
81 if(value > 0) { |
|
82 if(entries) { |
|
83 while(entries.length > value) { |
|
84 entries.shift(); |
|
85 } |
|
86 } |
|
87 } |
|
88 else { |
|
89 this._entries = []; |
|
90 } |
|
91 return value; |
|
92 } |
|
93 }, |
|
94 |
|
95 /** |
|
96 * @attribute size |
|
97 * @description Number of entries currently cached. |
|
98 * @type Number |
|
99 */ |
|
100 size: { |
|
101 readOnly: true, |
|
102 getter: function() { |
|
103 return this._entries.length; |
|
104 } |
|
105 }, |
|
106 |
|
107 /** |
|
108 * @attribute entries |
|
109 * @description Cached entries. |
|
110 * @type Array |
|
111 */ |
|
112 entries: { |
|
113 readOnly: true, |
|
114 getter: function() { |
|
115 return this._entries; |
|
116 } |
|
117 } |
|
118 } |
|
119 }); |
|
120 |
|
121 Y.extend(Cache, Y.Plugin.Base, { |
|
122 ///////////////////////////////////////////////////////////////////////////// |
|
123 // |
|
124 // Cache private properties |
|
125 // |
|
126 ///////////////////////////////////////////////////////////////////////////// |
|
127 |
|
128 /** |
|
129 * Array of request/response objects indexed chronologically. |
|
130 * |
|
131 * @property _entries |
|
132 * @type Object[] |
|
133 * @private |
|
134 */ |
|
135 _entries: null, |
|
136 |
|
137 ///////////////////////////////////////////////////////////////////////////// |
|
138 // |
|
139 // Cache private methods |
|
140 // |
|
141 ///////////////////////////////////////////////////////////////////////////// |
|
142 |
|
143 /** |
|
144 * @method initializer |
|
145 * @description Internal init() handler. |
|
146 * @param config {Object} Config object. |
|
147 * @private |
|
148 */ |
|
149 initializer: function(config) { |
|
150 |
|
151 /** |
|
152 * @event add |
|
153 * @description Fired when an entry is added. |
|
154 * @param e {Event.Facade} Event Facade with the following properties: |
|
155 * <dl> |
|
156 * <dt>entry (Object)</dt> <dd>The cached entry.</dd> |
|
157 * </dl> |
|
158 * @preventable _defAddFn |
|
159 */ |
|
160 this.publish("add", {defaultFn: this._defAddFn}); |
|
161 |
|
162 /** |
|
163 * @event flush |
|
164 * @description Fired when the cache is flushed. |
|
165 * @param e {Event.Facade} Event Facade object. |
|
166 * @preventable _defFlushFn |
|
167 */ |
|
168 this.publish("flush", {defaultFn: this._defFlushFn}); |
|
169 |
|
170 /** |
|
171 * @event request |
|
172 * @description Fired when an entry is requested from the cache. |
|
173 * @param e {Event.Facade} Event Facade with the following properties: |
|
174 * <dl> |
|
175 * <dt>request (Object)</dt> <dd>The request object.</dd> |
|
176 * </dl> |
|
177 */ |
|
178 |
|
179 /** |
|
180 * @event retrieve |
|
181 * @description Fired when an entry is retrieved from the cache. |
|
182 * @param e {Event.Facade} Event Facade with the following properties: |
|
183 * <dl> |
|
184 * <dt>entry (Object)</dt> <dd>The retrieved entry.</dd> |
|
185 * </dl> |
|
186 */ |
|
187 |
|
188 // Initialize internal values |
|
189 this._entries = []; |
|
190 Y.log("Cache initialized", "info", "cache"); |
|
191 }, |
|
192 |
|
193 /** |
|
194 * @method destructor |
|
195 * @description Internal destroy() handler. |
|
196 * @private |
|
197 */ |
|
198 destructor: function() { |
|
199 this._entries = null; |
|
200 Y.log("Cache destroyed", "info", "cache"); |
|
201 }, |
|
202 |
|
203 ///////////////////////////////////////////////////////////////////////////// |
|
204 // |
|
205 // Cache protected methods |
|
206 // |
|
207 ///////////////////////////////////////////////////////////////////////////// |
|
208 |
|
209 /** |
|
210 * Adds entry to cache. |
|
211 * |
|
212 * @method _defAddFn |
|
213 * @param e {Event.Facade} Event Facade with the following properties: |
|
214 * <dl> |
|
215 * <dt>entry (Object)</dt> <dd>The cached entry.</dd> |
|
216 * </dl> |
|
217 * @protected |
|
218 */ |
|
219 _defAddFn: function(e) { |
|
220 var entries = this._entries, |
|
221 max = this.get("max"), |
|
222 entry = e.entry; |
|
223 |
|
224 // If the cache at or over capacity, make room by removing stalest element (index=0) |
|
225 while(entries.length>=max) { |
|
226 entries.shift(); |
|
227 } |
|
228 |
|
229 // Add entry to cache in the newest position, at the end of the array |
|
230 entries[entries.length] = entry; |
|
231 Y.log("Cached entry: " + Y.dump(entry), "info", "cache"); |
|
232 }, |
|
233 |
|
234 /** |
|
235 * Flushes cache. |
|
236 * |
|
237 * @method _defFlushFn |
|
238 * @param e {Event.Facade} Event Facade object. |
|
239 * @protected |
|
240 */ |
|
241 _defFlushFn: function(e) { |
|
242 this._entries = []; |
|
243 Y.log("Cache flushed", "info", "cache"); |
|
244 }, |
|
245 |
|
246 /** |
|
247 * Default overridable method compares current request with given cache entry. |
|
248 * Returns true if current request matches the cached request, otherwise |
|
249 * false. Implementers should override this method to customize the |
|
250 * cache-matching algorithm. |
|
251 * |
|
252 * @method _isMatch |
|
253 * @param request {Object} Request object. |
|
254 * @param entry {Object} Cached entry. |
|
255 * @return {Boolean} True if current request matches given cached request, false otherwise. |
|
256 * @protected |
|
257 */ |
|
258 _isMatch: function(request, entry) { |
|
259 return (request === entry.request); |
|
260 }, |
|
261 |
|
262 ///////////////////////////////////////////////////////////////////////////// |
|
263 // |
|
264 // Cache public methods |
|
265 // |
|
266 ///////////////////////////////////////////////////////////////////////////// |
|
267 |
|
268 /** |
|
269 * Adds a new entry to the cache of the format |
|
270 * {request:request, response:response, payload:payload}. |
|
271 * If cache is full, evicts the stalest entry before adding the new one. |
|
272 * |
|
273 * @method add |
|
274 * @param request {Object} Request value. |
|
275 * @param response {Object} Response value. |
|
276 * @param payload {Object} (optional) Arbitrary data payload. |
|
277 */ |
|
278 add: function(request, response, payload) { |
|
279 if(this.get("entries") && (this.get("max")>0) && |
|
280 (LANG.isValue(request) || LANG.isNull(request) || LANG.isUndefined(request))) { |
|
281 this.fire("add", {entry: {request:request, response:response, payload:payload}}); |
|
282 } |
|
283 else { |
|
284 Y.log("Could not add " + Y.dump(response) + " to cache for " + Y.dump(request), "info", "cache"); |
|
285 } |
|
286 }, |
|
287 |
|
288 /** |
|
289 * Flushes cache. |
|
290 * |
|
291 * @method flush |
|
292 */ |
|
293 flush: function() { |
|
294 this.fire("flush"); |
|
295 }, |
|
296 |
|
297 /** |
|
298 * Retrieves cached entry for given request, if available, and refreshes |
|
299 * entry in the cache. Returns null if there is no cache match. |
|
300 * |
|
301 * @method retrieve |
|
302 * @param request {Object} Request object. |
|
303 * @return {Object} Cached entry object with the properties request, response, and payload, or null. |
|
304 */ |
|
305 retrieve: function(request) { |
|
306 // If cache is enabled... |
|
307 var entries = this._entries, |
|
308 length = entries.length, |
|
309 entry = null, |
|
310 i = length-1; |
|
311 |
|
312 if((this.get("max") > 0) && (length > 0)) { |
|
313 this.fire("request", {request: request}); |
|
314 |
|
315 // Loop through each cached entry starting from the newest |
|
316 for(; i >= 0; i--) { |
|
317 entry = entries[i]; |
|
318 |
|
319 // Execute matching function |
|
320 if(this._isMatch(request, entry)) { |
|
321 this.fire("retrieve", {entry: entry}); |
|
322 |
|
323 // Refresh the position of the cache hit |
|
324 if(i < length-1) { |
|
325 // Remove element from its original location |
|
326 entries.splice(i,1); |
|
327 // Add as newest |
|
328 entries[entries.length] = entry; |
|
329 Y.log("Refreshed cache entry: " + Y.dump(entry) + |
|
330 " for request: " + Y.dump(request), "info", "cache"); |
|
331 } |
|
332 |
|
333 Y.log("Retrieved cached response: " + Y.dump(entry) + |
|
334 " for request: " + Y.dump(request), "info", "cache"); |
|
335 return entry; |
|
336 } |
|
337 } |
|
338 } |
|
339 return null; |
|
340 } |
|
341 }); |
|
342 |
|
343 Y.Cache = Cache; |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 }, '3.0.0b1' ,{requires:['plugin']}); |