Cache api using Cache instances
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 542cab1 · Browse files · 3 files·+101 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+101 added−73 removed
- tests:+81 added−70 removed
- other:+20 added−3 removed
Provenance
- Commit
e2fee9b- Tree
1a03bcf- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
542cab1
3 files changed+101 added−73 removed
modified:lib/express/plugins/cache.js+20 added-3 removed
1234567891011121314151617181920212262 unmodified lines8586878874899091929315 unmodified lines1091101111129811311411511611711811912010412112212312412551 unmodified lines
// Express - Cache - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// --- Cache
var Cache = Class({ /** * Initialize cache with _key_ and _val_. */ init: function(key, val) { this.key = key this.val = val this.created = Number(new Date) }})
// --- Store
exports.Store = Class({ 62 unmodified lines */ set: function(key, val) { this.__super__(key, val) return this.data[key] = val return this.data[key] = new Cache(key, val), val }, /** * Get data found matching the given _key_.15 unmodified lines get: function(key) { this.__super__(key) if (key.indexOf('*') === -1) return this.data[key] return this.data[key] instanceof Cache ? this.data[key].val : null var vals = {}, regexp = this.normalize(key) for (var key in this.data) if (this.data.hasOwnProperty(key)) if (regexp.test(key)) vals[key] = this.data[key] vals[key] = this.data[key].val return vals }, /**51 unmodified linesmodified:spec/spec.plugins.cache.js+80 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+ // memory.commit({ id: '1', lastAccess: Number(new Date) - 300 })110+ // memory.commit({ id: '2', lastAccess: Number(new Date) - 250 })111+ // memory.commit({ id: '3', lastAccess: Number(new Date) - 100 })112+ // memory.commit({ id: '4', lastAccess: Number(new Date) })113+ // memory.reap(200)114+ // memory.store.should.not.have_property '1'115+ // memory.store.should.not.have_property '2'116+ // memory.store.should.have_property '3'117+ // memory.store.should.have_property '4'118+ end119+ end120 end121 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