Merge branch 'cache-lifetime'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents 542cab1, 75c8566 · Browse files · 9 files·+169 added−85 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+169 added−85 removed
- tests:+80 added−70 removed
- other:+89 added−15 removed
Provenance
- Commit
5ce1306- Tree
03ce446- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
542cab1- Parent 2
75c8566
9 files changed+169 added−85 removed
modified:examples/chat/app.js+1 added
8 unmodified lines9101112131415161766 unmodified lines8 unmodified lines use(MethodOverride) use(ContentLength) use(CommonLogger) use(Cookie) use(Cache, { lifetime: fiveMinutes, reapInterval: oneMinute }) use(Session, { lifetime: fiveMinutes, reapInterval: oneMinute }) set('root', __dirname) enable('cache static files') //enable('cache view contents')66 unmodified linesmodified:lib/express/plugins/cache.js+73 added-14 removed
1 2 // Express - Cache - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4+// --- Cache5+6+var Cache = Class({7+ 8+ /**9+ * Initialize cache with _key_ and _val_.10+ */11+ 12+ init: function(key, val) {13+ this.key = key14+ this.val = val15+ this.created = Number(new Date)16+ }17+})18+19 // --- Store20 21 exports.Store = Class({22 62 unmodified lines85 */86 87 set: function(key, val) {88 this.__super__(key, val)74- return this.data[key] = val89+ return this.data[key] = new Cache(key, val), val90 },91 92 /**93 * Get data found matching the given _key_.15 unmodified lines109 110 get: function(key) {111 this.__super__(key)112 if (key.indexOf('*') === -1)98- return this.data[key]99- var vals = {},100- regexp = this.normalize(key)101- for (var key in this.data)102- if (this.data.hasOwnProperty(key))103- if (regexp.test(key))104- vals[key] = this.data[key]105- return vals 113+ return this.data[key] instanceof Cache ?114+ this.data[key].val :115+ null116+ var regexp = this.normalize(key) 117+ return $(this.data).reduce({}, function(vals, cache){118+ if (regexp.test(cache.key))119+ vals[cache.key] = cache.val120+ return vals121+ })122 },123 124 /**125 * Clear data matching the given _key_.17 unmodified lines143 delete this.data[key]144 },145 146 /**147+ * Reap caches older than _ms_.148+ *149+ * @param {int} ms150+ * @api private151+ */152+ 153+ reap: function(ms) {154+ var self = this,155+ threshold = Number(new Date(Number(new Date) - ms))156+ $(this.data).each(function(cache){157+ if (cache.created < threshold)158+ self.clear(cache.key)159+ })160+ },161+ 162+ /**163 * Convert the given key matching _pattern_ 164 * into a RegExp.165 *166 * - * is converted to (.*?)7 unmodified lines174 return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$')175 }176 })177 178+// --- Cache179+180 exports.Cache = Plugin.extend({181 extend: {182 183 /**150- * Initialize extensions.184+ * Initialize memory store and start reaper.185+ *186+ * Options:187+ *188+ * - dataStore constructor name of cache data store, defaults to Store.Memory189+ * - lifetime lifetime of cache in milliseconds, defaults to one day190+ * - reapInterval interval in milliseconds in which to reap old caches, defaults to one hour191+ * 192+ * @param {hash} options193+ * @api private194 */195 153- init: function() {154- Request.include({155- cache: new exports.Store.Memory156- })196+ init: function(options) {197+ process.mixin(this, options)198+ this.store = new (this.dataStore || exports.Store.Memory)(options)199+ Request.include({ cache: this.store })200+ this.startReaper()201+ },202+ 203+ /**204+ * Start reaper.205+ *206+ * @api private207+ */208+ 209+ startReaper: function() {210+ var self = this,211+ oneDay = 86400000,212+ oneHour = 3600000213+ setInterval(function(){214+ self.store.reap(self.lifetime || oneDay)215+ }, self.reapInterval || oneHour)216 }217 }218 })modified:lib/express/plugins/session.js+2 added
136 unmodified lines137 })138 }139 })140 141+// --- Cache142+143 exports.Session = Plugin.extend({144 extend: {145 146 /**59 unmodified linesmodified:lib/express/dsl.js+4 added
67 unmodified lines68 */69 70 exports.run = function() {71 configure(Express.environment = process.ENV.EXPRESS_ENV || 'development')72+ $(Express.plugins).each(function(plugin){73+ if ('init' in plugin.klass)74+ plugin.klass.init(plugin.options)75+ })76 Express.server.run.apply(Express.server, arguments)77 }78 79 /**43 unmodified linesmodified:lib/express/plugin.js+8 added-1 removed
1234567891011121314131516171819202122232431 unmodified lines
// Express - Plugin - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
/** * Push _plugin_ with _options_ to the plugin stack. * If _plugin_ has already been pushed, then it's options * will override any previously set. * * @param {Plugin} plugin * @param {hash} options * @api public */
exports.use = function(plugin, options) { if ('init' in plugin) plugin.init(options) if (Express.environment === 'test' && 'init' in plugin) plugin.init(options) $(Express.plugins).each(function(other, i){ if (other.klass === plugin) delete Express.plugins[i] }) Express.plugins.push({ klass: plugin, options: options })31 unmodified linesmodified:lib/express/plugins.js+1 added
1234567892 unmodified lines
// Express - Plugins - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
process.mixin(require('express/plugins/hooks'))process.mixin(require('express/plugins/cache'))process.mixin(require('express/plugins/cookie'))process.mixin(require('express/plugins/session'))process.mixin(require('express/plugins/profiler'))process.mixin(require('express/plugins/common-logger'))2 unmodified linesmodified:spec/node.js+1 added
62 unmodified lines63 default: 64 run([process.ARGV[2]])65 }66 67+Express.environment = 'test'68 JSpec.run({ reporter: JSpec.reporters.Terminal, failuresOnly: true }).report()modified:spec/spec.plugins.cache.js+78 added-69 removed
17 unmodified lines18 end19 end20 end21 22- describe 'Store'23- describe 'Memory'24- before_each25- store = new cache.Store.Memory22+ describe 'cache Store.Memory'23+ before_each24+ store = new cache.Store.Memory25+ end26+ 27+ describe '#toString()'28+ it 'should return [Memory Store]'29+ store.toString().should.eql '[Memory Store]'30 end27- 28- describe '#toString()'29- it 'should return [Memory Store]'30- store.toString().should.eql '[Memory Store]'31+ end32+ 33+ describe '#set()'34+ describe 'given a key and value'35+ it 'should set the cache data'36+ store.set('foo', 'bar')37+ store.get('foo').should.eql 'bar'38 end32- end33- 34- describe '#set()'35- describe 'given a key and value'36- it 'should set the cache data'37- store.set('foo', 'bar')38- store.get('foo').should.eql 'bar'39- end40- 41- it 'should override existing data'42- store.set('foo', 'bar')43- store.set('foo', 'baz')44- store.get('foo').should.eql 'baz'45- end46- 47- it 'should return data'48- store.set('foo', 'bar').should.eql 'bar'49- end50- end39 52- describe 'given an abitrary key'53- it 'should throw an error'54- -{ store.set({}, 'foo') }.should.throw_error55- end40+ it 'should override existing data'41+ store.set('foo', 'bar')42+ store.set('foo', 'baz')43+ store.get('foo').should.eql 'baz'44 end45 58- describe 'given an abitrary value'59- it 'should serialize as JSON'60- store.set('user', { name: 'tj' }).should.eql { name: 'tj' }61- end46+ it 'should return data'47+ store.set('foo', 'bar').should.eql 'bar'48 end49 end50 65- describe '#get()'66- describe 'given a key'67- it 'should return cached value'68- store.set('foo', 'bar')69- store.get('foo').should.eql 'bar'70- end71- 72- it 'should unserialize JSON data'73- store.set('user', { name: 'tj' })74- store.get('user').should.eql { name: 'tj' }75- end51+ describe 'given an abitrary key'52+ it 'should throw an error'53+ -{ store.set({}, 'foo') }.should.throw_error54 end55+ end56+ 57+ describe 'given an abitrary value'58+ it 'should serialize as JSON'59+ store.set('user', { name: 'tj' }).should.eql { name: 'tj' }60+ end61+ end62+ end63+ 64+ describe '#get()'65+ describe 'given a key'66+ it 'should return cached value'67+ store.set('foo', 'bar')68+ store.get('foo').should.eql 'bar'69+ end70 78- describe 'given wildcards'79- it 'should return a set of caches'80- store.set('user:1', 'a')81- store.set('user:2', 'b')82- store.set('foo', 'bar')83- store.get('user:*').should.eql { 'user:1': 'a', 'user:2': 'b' }84- end71+ it 'should unserialize JSON data'72+ store.set('user', { name: 'tj' })73+ store.get('user').should.eql { name: 'tj' }74 end75 end76 88- describe '#clear()'89- describe 'given a key'90- it 'should delete previous data'91- store.set('foo', 'bar')92- store.clear('foo')93- store.get('foo').should.be_null94- end77+ describe 'given wildcards'78+ it 'should return a set of caches'79+ store.set('user:1', 'a')80+ store.set('user:2', 'b')81+ store.set('foo', 'bar')82+ store.get('user:*').should.eql { 'user:1': 'a', 'user:2': 'b' }83 end96- 97- describe 'given wildcards'98- it 'should clear a set of caches'99- store.set('user:one', '1')100- store.set('user:two', '2')101- store.clear('user:*')102- store.get('user:one').should.be_null103- store.get('user:two').should.be_null104- end84+ end85+ end86+ 87+ describe '#clear()'88+ describe 'given a key'89+ it 'should delete previous data'90+ store.set('foo', 'bar')91+ store.clear('foo')92+ store.get('foo').should.be_null93 end94 end95 96+ describe 'given wildcards'97+ it 'should clear a set of caches'98+ store.set('user:one', '1')99+ store.set('user:two', '2')100+ store.clear('user:*')101+ store.get('user:one').should.be_null102+ store.get('user:two').should.be_null103+ end104+ end105 end106+ 107+ describe '#reap()'108+ it 'should destroy caches older than the given age in milliseconds'109+ store.set('user:one', '1')110+ store.data['user:one'].created = Number(new Date) - 300111+ store.set('user:two', '2')112+ store.data['user:two'].created = Number(new Date) - 100113+ store.reap(200)114+ store.get('user:one').should.be_null115+ store.get('user:two').should.not.be_null116+ end117+ end118 end119 endmodified:spec/spec.plugins.session.js+1 added-1 removed
20 unmodified lines21 get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie'22 end23 end24 25- describe 'Store.Memory'25+ describe 'session Store.Memory'26 before_each27 memory = new (require('express/plugins/session').Store.Memory)28 end29 79 unmodified lines