~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/maasserver/static/jslibs/yui/3.4.1/build/cache-base/cache-base.js

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
YUI 3.4.1 (build 4118)
3
 
Copyright 2011 Yahoo! Inc. All rights reserved.
4
 
Licensed under the BSD License.
5
 
http://yuilibrary.com/license/
6
 
*/
7
 
YUI.add('cache-base', function(Y) {
8
 
 
9
 
/**
10
 
 * The Cache utility provides a common configurable interface for components to
11
 
 * cache and retrieve data from a local JavaScript struct.
12
 
 *
13
 
 * @module cache
14
 
 * @main
15
 
 */
16
 
 
17
 
/**
18
 
 * Provides the base class for the YUI Cache utility.
19
 
 * 
20
 
 * @submodule cache-base
21
 
 */
22
 
var LANG = Y.Lang,
23
 
    isDate = Y.Lang.isDate,
24
 
 
25
 
/**
26
 
 * Base class for the YUI Cache utility.
27
 
 * @class Cache
28
 
 * @extends Base
29
 
 * @constructor
30
 
 */
31
 
Cache = function() {
32
 
    Cache.superclass.constructor.apply(this, arguments);
33
 
};
34
 
 
35
 
    /////////////////////////////////////////////////////////////////////////////
36
 
    //
37
 
    // Cache static properties
38
 
    //
39
 
    /////////////////////////////////////////////////////////////////////////////
40
 
Y.mix(Cache, {
41
 
    /**
42
 
     * Class name.
43
 
     *
44
 
     * @property NAME
45
 
     * @type String
46
 
     * @static
47
 
     * @final
48
 
     * @value "cache"
49
 
     */
50
 
    NAME: "cache",
51
 
 
52
 
 
53
 
    ATTRS: {
54
 
        /////////////////////////////////////////////////////////////////////////////
55
 
        //
56
 
        // Cache Attributes
57
 
        //
58
 
        /////////////////////////////////////////////////////////////////////////////
59
 
 
60
 
        /**
61
 
        * @attribute max
62
 
        * @description Maximum number of entries the Cache can hold.
63
 
        * Set to 0 to turn off caching.
64
 
        * @type Number
65
 
        * @default 0
66
 
        */
67
 
        max: {
68
 
            value: 0,
69
 
            setter: "_setMax"
70
 
        },
71
 
 
72
 
        /**
73
 
        * @attribute size
74
 
        * @description Number of entries currently cached.
75
 
        * @type Number
76
 
        */
77
 
        size: {
78
 
            readOnly: true,
79
 
            getter: "_getSize"
80
 
        },
81
 
 
82
 
        /**
83
 
        * @attribute uniqueKeys
84
 
        * @description Validate uniqueness of stored keys. Default is false and
85
 
        * is more performant.
86
 
        * @type Boolean
87
 
        */
88
 
        uniqueKeys: {
89
 
            value: false
90
 
        },
91
 
 
92
 
        /**
93
 
        * @attribute expires
94
 
        * @description Absolute Date when data expires or
95
 
        * relative number of milliseconds. Zero disables expiration.
96
 
        * @type Date | Number
97
 
        * @default 0
98
 
        */
99
 
        expires: {
100
 
            value: 0,
101
 
            validator: function(v) {
102
 
                return Y.Lang.isDate(v) || (Y.Lang.isNumber(v) && v >= 0);
103
 
            }
104
 
        },
105
 
 
106
 
        /**
107
 
         * @attribute entries
108
 
         * @description Cached entries.
109
 
         * @type Array
110
 
         */
111
 
        entries: {
112
 
            readOnly: true,
113
 
            getter: "_getEntries"
114
 
        }
115
 
    }
116
 
});
117
 
 
118
 
Y.extend(Cache, Y.Base, {
119
 
    /////////////////////////////////////////////////////////////////////////////
120
 
    //
121
 
    // Cache private properties
122
 
    //
123
 
    /////////////////////////////////////////////////////////////////////////////
124
 
 
125
 
    /**
126
 
     * Array of request/response objects indexed chronologically.
127
 
     *
128
 
     * @property _entries
129
 
     * @type Object[]
130
 
     * @private
131
 
     */
132
 
    _entries: null,
133
 
 
134
 
    /////////////////////////////////////////////////////////////////////////////
135
 
    //
136
 
    // Cache private methods
137
 
    //
138
 
    /////////////////////////////////////////////////////////////////////////////
139
 
 
140
 
    /**
141
 
    * @method initializer
142
 
    * @description Internal init() handler.
143
 
    * @param config {Object} Config object.
144
 
    * @private
145
 
    */
146
 
    initializer: function(config) {
147
 
 
148
 
        /**
149
 
        * @event add
150
 
        * @description Fired when an entry is added.
151
 
        * @param e {Event.Facade} Event Facade with the following properties:
152
 
         * <dl>
153
 
         * <dt>entry (Object)</dt> <dd>The cached entry.</dd>
154
 
         * </dl>
155
 
        * @preventable _defAddFn
156
 
        */
157
 
        this.publish("add", {defaultFn: this._defAddFn});
158
 
 
159
 
        /**
160
 
        * @event flush
161
 
        * @description Fired when the cache is flushed.
162
 
        * @param e {Event.Facade} Event Facade object.
163
 
        * @preventable _defFlushFn
164
 
        */
165
 
        this.publish("flush", {defaultFn: this._defFlushFn});
166
 
 
167
 
        /**
168
 
        * @event request
169
 
        * @description Fired when an entry is requested from the cache.
170
 
        * @param e {Event.Facade} Event Facade with the following properties:
171
 
        * <dl>
172
 
        * <dt>request (Object)</dt> <dd>The request object.</dd>
173
 
        * </dl>
174
 
        */
175
 
 
176
 
        /**
177
 
        * @event retrieve
178
 
        * @description Fired when an entry is retrieved from the cache.
179
 
        * @param e {Event.Facade} Event Facade with the following properties:
180
 
        * <dl>
181
 
        * <dt>entry (Object)</dt> <dd>The retrieved entry.</dd>
182
 
        * </dl>
183
 
        */
184
 
 
185
 
        // Initialize internal values
186
 
        this._entries = [];
187
 
    },
188
 
 
189
 
    /**
190
 
    * @method destructor
191
 
    * @description Internal destroy() handler.
192
 
    * @private
193
 
    */
194
 
    destructor: function() {
195
 
        this._entries = [];
196
 
    },
197
 
 
198
 
    /////////////////////////////////////////////////////////////////////////////
199
 
    //
200
 
    // Cache protected methods
201
 
    //
202
 
    /////////////////////////////////////////////////////////////////////////////
203
 
 
204
 
    /**
205
 
     * Sets max.
206
 
     *
207
 
     * @method _setMax
208
 
     * @protected
209
 
     */
210
 
    _setMax: function(value) {
211
 
        // If the cache is full, make room by removing stalest element (index=0)
212
 
        var entries = this._entries;
213
 
        if(value > 0) {
214
 
            if(entries) {
215
 
                while(entries.length > value) {
216
 
                    entries.shift();
217
 
                }
218
 
            }
219
 
        }
220
 
        else {
221
 
            value = 0;
222
 
            this._entries = [];
223
 
        }
224
 
        return value;
225
 
    },
226
 
 
227
 
    /**
228
 
     * Gets size.
229
 
     *
230
 
     * @method _getSize
231
 
     * @protected
232
 
     */
233
 
    _getSize: function() {
234
 
        return this._entries.length;
235
 
    },
236
 
 
237
 
    /**
238
 
     * Gets all entries.
239
 
     *
240
 
     * @method _getEntries
241
 
     * @protected
242
 
     */
243
 
    _getEntries: function() {
244
 
        return this._entries;
245
 
    },
246
 
 
247
 
 
248
 
    /**
249
 
     * Adds entry to cache.
250
 
     *
251
 
     * @method _defAddFn
252
 
     * @param e {Event.Facade} Event Facade with the following properties:
253
 
     * <dl>
254
 
     * <dt>entry (Object)</dt> <dd>The cached entry.</dd>
255
 
     * </dl>
256
 
     * @protected
257
 
     */
258
 
    _defAddFn: function(e) {
259
 
        var entries = this._entries,
260
 
            max = this.get("max"),
261
 
            entry = e.entry;
262
 
 
263
 
        if(this.get("uniqueKeys") && (this.retrieve(e.entry.request))) {
264
 
            entries.shift();
265
 
        }
266
 
 
267
 
 
268
 
        // If the cache at or over capacity, make room by removing stalest element (index=0)
269
 
        while(max && entries.length>=max) {
270
 
            entries.shift();
271
 
        }
272
 
 
273
 
        // Add entry to cache in the newest position, at the end of the array
274
 
        entries[entries.length] = entry;
275
 
    },
276
 
 
277
 
    /**
278
 
     * Flushes cache.
279
 
     *
280
 
     * @method _defFlushFn
281
 
     * @param e {Event.Facade} Event Facade object.
282
 
     * @protected
283
 
     */
284
 
    _defFlushFn: function(e) {
285
 
        var entries = this._entries,
286
 
            details = e.details[0],
287
 
            pos;
288
 
        
289
 
        //passed an item, flush only that
290
 
        if(details && LANG.isValue(details.request)) {
291
 
            pos = this._position(details.request);
292
 
            
293
 
            if(LANG.isValue(pos)) {
294
 
                entries.splice(pos,1);
295
 
                
296
 
            }
297
 
        } 
298
 
        //no item, flush everything
299
 
        else {
300
 
            this._entries = [];
301
 
        }
302
 
    },
303
 
 
304
 
    /**
305
 
     * Default overridable method compares current request with given cache entry.
306
 
     * Returns true if current request matches the cached request, otherwise
307
 
     * false. Implementers should override this method to customize the
308
 
     * cache-matching algorithm.
309
 
     *
310
 
     * @method _isMatch
311
 
     * @param request {Object} Request object.
312
 
     * @param entry {Object} Cached entry.
313
 
     * @return {Boolean} True if current request matches given cached request, false otherwise.
314
 
     * @protected
315
 
     */
316
 
    _isMatch: function(request, entry) {
317
 
        if(!entry.expires || new Date() < entry.expires) {
318
 
            return (request === entry.request);
319
 
        }
320
 
        return false;
321
 
    },
322
 
    
323
 
    /**
324
 
     * Returns position of a request in the entries array, otherwise null.
325
 
     *
326
 
     * @method _position
327
 
     * @param request {Object} Request object.
328
 
     * @return {Number} Array position if found, null otherwise.
329
 
     * @protected
330
 
     */
331
 
    _position: function(request) {
332
 
        // If cache is enabled...
333
 
        var entries = this._entries,
334
 
            length = entries.length,
335
 
            i = length-1;
336
 
        
337
 
        if((this.get("max") === null) || this.get("max") > 0) {
338
 
            // Loop through each cached entry starting from the newest
339
 
            for(; i >= 0; i--) {
340
 
                // Execute matching function
341
 
                if(this._isMatch(request, entries[i])) {
342
 
                    return i;
343
 
                }
344
 
            }
345
 
        }
346
 
        
347
 
        return null;
348
 
    },
349
 
 
350
 
    /////////////////////////////////////////////////////////////////////////////
351
 
    //
352
 
    // Cache public methods
353
 
    //
354
 
    /////////////////////////////////////////////////////////////////////////////
355
 
 
356
 
    /**
357
 
     * Adds a new entry to the cache of the format
358
 
     * {request:request, response:response, cached:cached, expires:expires}.
359
 
     * If cache is full, evicts the stalest entry before adding the new one.
360
 
     *
361
 
     * @method add
362
 
     * @param request {Object} Request value.
363
 
     * @param response {Object} Response value.
364
 
     */
365
 
    add: function(request, response) {
366
 
        var expires = this.get("expires");
367
 
        if(this.get("initialized") && ((this.get("max") === null) || this.get("max") > 0) &&
368
 
                (LANG.isValue(request) || LANG.isNull(request) || LANG.isUndefined(request))) {
369
 
            this.fire("add", {entry: {
370
 
                request:request,
371
 
                response:response,
372
 
                cached: new Date(),
373
 
                expires: isDate(expires) ? expires :
374
 
            (expires ? new Date(new Date().getTime() + this.get("expires")) : null)
375
 
            }});
376
 
        }
377
 
        else {
378
 
        }
379
 
    },
380
 
 
381
 
    /**
382
 
     * Flushes cache.
383
 
     *
384
 
     * @method flush
385
 
     */
386
 
    flush: function(request) {
387
 
        this.fire("flush", { request: (LANG.isValue(request) ? request : null) });
388
 
    },
389
 
    
390
 
    /**
391
 
     * Retrieves cached object for given request, if available, and refreshes
392
 
     * entry in the cache. Returns null if there is no cache match.
393
 
     *
394
 
     * @method retrieve
395
 
     * @param request {Object} Request object.
396
 
     * @return {Object} Cached object with the properties request and response, or null.
397
 
     */
398
 
    retrieve: function(request) {
399
 
        // If cache is enabled...
400
 
        var entries = this._entries,
401
 
            length = entries.length,
402
 
            entry = null,
403
 
            pos;
404
 
 
405
 
        if((length > 0) && ((this.get("max") === null) || (this.get("max") > 0))) {
406
 
            this.fire("request", {request: request});
407
 
            
408
 
            pos = this._position(request);
409
 
            
410
 
            if(LANG.isValue(pos)) {
411
 
                entry = entries[pos];
412
 
                
413
 
                this.fire("retrieve", {entry: entry});
414
 
 
415
 
                // Refresh the position of the cache hit
416
 
                if(pos < length-1) {
417
 
                    // Remove element from its original location
418
 
                    entries.splice(pos,1);
419
 
                    // Add as newest
420
 
                    entries[entries.length] = entry;
421
 
                }
422
 
 
423
 
                return entry;
424
 
            }
425
 
        }
426
 
        return null;
427
 
    }
428
 
});
429
 
 
430
 
Y.Cache = Cache;
431
 
 
432
 
 
433
 
}, '3.4.1' ,{requires:['base']});