cache.js revision 9eb0c723ed90ba9c691cd35c025115b361da3eb4
/**
* 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
* @param args {Object} Object literal data payload.
* @param args.entry {Object} The cached entry.
*/
/**
* @event request
* @param args {Object} Object literal data payload.
* @param args.request {Object} The request object.
* @param args.callback {Object} The callback object.
*/
/**
* @event retrieve
* @description Fired when an entry is retrieved from the cache.
* @param args {Object} Object literal data payload.
* @param args.request {Object} The request object.
* @param args.entry {Object} The retrieved entry.
* @param args.callback {Object} The callback object.
*/
/**
* @event flush
* @description Fired when the cache is flushed.
*/
// Initialize internal values
this._entries = [];
},
/**
* @method destructor
* @description Internal destroy() handler.
* @private
*/
destructor: function() {
this._entries = null;
},
/////////////////////////////////////////////////////////////////////////////
//
// 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: resopnse}
* If cache is full, evicts the stalest entry before adding the new one.
*
* @method cache
* @param request {Object} Request object.
* @param response {Object} Response object.
* @param payload {Object} Arbitrary data payload.
*/
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
Y.log("Cached entry: " + Y.dump(entry) + " for request: " + Y.dump(request) + "", "info", this.toString());
},
/**
* Retrieves entry from cache 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 following properties:
* {request:request, response:response, payload:payload}, or null.
*/
// If cache is enabled...
response = null,
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;
},
/**
* Flushes cache.
*
* @method flush
*/
flush: function() {
this._entries = [];
this.fire("flush");
}
});
Y.namespace('Cache');