cache-debug.js revision 3db65648daa65959bc625e4f81cee50763625fc9
/**
* The Cache utility provides a common configurable interface for components to
* cache and retrieve data from a local JavaScript struct.
*
* @module cache
* @requires base
* @title Cache Utility
*/
/**
* Base class for the YUI Cache utility.
* @class Cache
* @extends Base
* @constructor
*/
Cache = function() {
};
/////////////////////////////////////////////////////////////////////////////
//
// Cache static properties
//
/////////////////////////////////////////////////////////////////////////////
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "cache'
*/
NAME: "cache",
ATTRS: {
/////////////////////////////////////////////////////////////////////////////
//
// Cache Attributes
//
/////////////////////////////////////////////////////////////////////////////
/**
* @attribute size
* @description Max number of entries the Cache will hold.
* Set to 0 to turn off caching.
* @type Number
* @default 0
*/
size: {
value: 0,
},
// If the cache is full, make room by removing stalest element (index=0)
if(entries) {
}
}
return value;
}
else {
this._entries = [];
return 0;
}
}
}
}
});
/////////////////////////////////////////////////////////////////////////////
//
// Cache private properties
//
/////////////////////////////////////////////////////////////////////////////
/**
*
* @property _entries
* @type Object[]
* @private
*/
_entries: null,
/////////////////////////////////////////////////////////////////////////////
//
// Cache private methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* @method initializer
* @description Internal init() handler.
* @private
*/
initializer: function() {
/**
* @event add
* @description Fired when an entry is added.
* @param e {Event.Facade} Event Facade object.
* @param entry {Object} The cached entry.
* @preventable _defAddFn
*/
/**
* @event flush
* @description Fired when the cache is flushed.
* @param e {Event.Facade} Event Facade object.
* @preventable _defFlushFn
*/
/**
* @event request
* @description Fired when an entry is requested from the cache.
* @param e {Event.Facade} Event Facade object.
* @param request {Object} The request object.
*/
/**
* @event retrieve
* @description Fired when an entry is retrieved from the cache.
* @param e {Event.Facade} Event Facade object.
* @param entry {Object} The retrieved entry.
*/
// Initialize internal values
this._entries = [];
},
/**
* @method destructor
* @description Internal destroy() handler.
* @private
*/
destructor: function() {
this._entries = null;
},
/////////////////////////////////////////////////////////////////////////////
//
// Cache protected methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Adds entry to cache.
*
* @method _defAddFn
* @param e {Event.Facade} Event Facade object.
* @param entry {Object} The cached entry.
* @protected
*/
return;
}
// If the cache at or over capacity, make room by removing stalest element (index=0)
}
// Add entry to cache in the newest position, at the end of the array
},
/**
* Flushes cache.
*
* @method _defFlushFn
* @param e {Event.Facade} Event Facade object.
* @protected
*/
_defFlushFn: function(e) {
this._entries = [];
},
/////////////////////////////////////////////////////////////////////////////
//
// Cache public methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Accessor to internal array of entries
*
* @method getEntries
* @return {Array} Internal array of cache entries.
*/
getEntries: function() {
return this._entries;
},
/**
* Default overridable method compares current request with given cache entry.
* Returns true if current request matches the cached request, otherwise
* false. Implementers should override this method to customize the
* cache-matching algorithm.
*
* @method isMatch
* @param request {Object} Request object.
* @param entry {Object} Cached entry.
* @return {Boolean} True if current request matches given cached request, false otherwise.
*/
},
/**
* Adds a new entry to the cache of the format
* {request:request, response:response, payload:payload}.
* If cache is full, evicts the stalest entry before adding the new one.
*
* @method add
* @param request {Object} Request object.
* @param response {Object} Response object.
* @param payload {Object} (optional) Arbitrary data payload.
*/
}
else {
Y.log("Could not add " + Y.dump(response) + " to cache for " + Y.dump(request), "info", this.toString());
}
},
/**
* Flushes cache.
*
* @method flush
*/
flush: function() {
this.fire("flush");
},
/**
* Retrieves cached entry for given request, if available, and refreshes
* entry in the cache. Returns null if there is no cache match.
*
* @method retrieve
* @param request {Object} Request object.
* @return {Object} Cached entry object with the properties request, response, and payload, or null.
*/
// If cache is enabled...
entry = null,
i = length-1;
// Loop through each cached entry starting from the newest
for(; i >= 0; i--) {
// Execute matching function
// Refresh the position of the cache hit
if(i < length-1) {
// Remove element from its original location
// Add as newest
break;
}
}
}
return entry;
}
return null;
}
});
Y.namespace("Cache");