cache.js
This file
Path
lib/express/plugins/cache.js
234 lines · 4.9 KiB · JavaScript
Last changed by
Utilizing Date.now()visionmediabefore adoption3cc7b4d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
// Express - Cache - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
/** * Module dependencies. */ var Request = require('express/request').Request
// --- Cache
var Cache = new Class({ /** * Initialize cache with _key_ and _val_. */ constructor: function(key, val) { this.key = key this.val = val this.created = Date.now() }})
// --- Store
exports.Store = new Class({ /** * Ensure that the given _key_ is a string. * Override in subclass to provide data-store specific functionality. * * @param {string} key * @param {string} val * @param {function} callback * @api public */ set: function(key, val, callback) { if (typeof key !== 'string') throw new Error(this.name + ' store #set() key must be a string') }, /** * Ensure that the given _key_ is a string. * Override in subclass to provide data-store specific functionality. * * @param {string} key * @param {function} callback * @api public */ get: function(key, callback) { if (typeof key !== 'string') throw new Error(this.name + 'store #get() key must be a string') }, /** * Convert to '[NAME Store]'. * * @return {string} * @api public */ toString: function() { return '[' + this.name + ' Store]' }})
// --- Store.Memory
exports.Store.Memory = exports.Store.extend({ /** * Datastore name. */ name: 'Memory', /** * Initialize data. */ constructor: function() { this.data = {} }, /** * Set the given _key_ to _val_. * * @param {string} key * @param {string} val * @param {function} callback * @return {string} * @api public */ set: function(key, val, callback) { exports.Store.prototype.set.apply(this, arguments) this.data[key] = new Cache(key, val) if (callback) callback(val) }, /** * Get data found matching the given _key_. * * Examples: * * cache.get('page:front', function(val){}) * // => '<html>...</html>' * * cache.get('page:*', function(vals){}) * // => { 'page:front': '<html>...</html>', * 'page:users': '<html>...</html>', * ... } * * @param {string} key * @param {function} callback * @return {mixed} * @api public */ get: function(key, callback) { exports.Store.prototype.get.apply(this, arguments) if (key.indexOf('*') === -1) return callback(this.data[key] instanceof Cache ? this.data[key].val : null) var regexp = this.normalize(key) callback(this.data.reduce(function(vals, cache){ if (regexp.test(cache.key)) vals[cache.key] = cache.val return vals }, {})) }, /** * Clear data matching the given _key_. * * Examples: * * cache.clear('page:front', function(){}) * cache.clear('page:*', function(){}) * * @param {string} key * @param {function} callback * @api public */ clear: function(key, callback) { if (key.indexOf('*') === -1) delete this.data[key] else { var regexp = this.normalize(key) for (var key in this.data) if (this.data.hasOwnProperty(key)) if (regexp.test(key)) delete this.data[key] } callback() }, /** * Reap caches older than _ms_. * * @param {int} ms * @api private */ reap: function(ms, callback) { var self = this, threshold = +new Date(Date.now() - ms) this.data.each(function(cache){ if (cache.created < threshold) self.clear(cache.key, function(){}) }) if (callback) callback() }, /** * Convert the given key matching _pattern_ * into a RegExp. * * - * is converted to (.*?) * * @param {string} pattern * @return {regexp} * @api private */ normalize: function(pattern) { return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$') }})
// --- Cache
exports.Cache = Plugin.extend({ extend: { /** * Initialize memory store and start reaper. * * Options: * * - dataStore constructor name of cache data store, defaults to Store.Memory * - lifetime lifetime of cache in milliseconds, defaults to one day * - reapInterval, reapEvery interval in milliseconds in which to reap old caches, defaults to one hour * * @param {hash} options * @api private */ init: function(options) { this.merge(options || {}) this.store = new (this.dataStore || exports.Store.Memory)(options) Request.include({ cache: this.store }) this.startReaper() }, /** * Start reaper. * * @param {function} callback * @api private */ startReaper: function(callbac) { setInterval(function(self){ self.store.reap(self.lifetime || (1).day) }, this.reapInterval || this.reapEvery || (1).hour, this) } }})