Merge branch 'async-cache'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents 273a51a, cabb43b · Browse files · 9 files·+151 added−162 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+151 added−162 removed
- tests:+82 added−37 removed
- other:+69 added−125 removed
Provenance
- Commit
7606f1b- Tree
621254e- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
273a51a- Parent 2
cabb43b
9 files changed+151 added−162 removed
modified:lib/express/plugins/cache.js+31 added-22 removed
24 unmodified lines25 * Override in subclass to provide data-store specific functionality.26 *27 * @param {string} key28 * @param {string} val29+ * @param {function} fn30 * @api public31 */32 32- set: function(key, val) {33+ set: function(key, val, fn) {34 if (typeof key !== 'string')35 throw new Error(this.name + ' store #set() key must be a string')36 },37 1 unmodified line39 * Ensure that the given _key_ is a string.40 * Override in subclass to provide data-store specific functionality.41 *42 * @param {string} key43+ * @param {function} fn44 * @api public45 */46 45- get: function(key) {47+ get: function(key, fn) {48 if (typeof key !== 'string')49 throw new Error(this.name + 'store #get() key must be a string')50 },51 27 unmodified lines79 this.data = {}80 },81 82 /**81- * Set the given _key_ to _val_, returning _val_.83+ * Set the given _key_ to _val_.84 *85 * @param {string} key86 * @param {string} val87+ * @param {function} fn88 * @return {string}89 * @api public90 */91 89- set: function(key, val) {92+ set: function(key, val, fn) {93 exports.Store.prototype.set.apply(this, arguments)91- return this.data[key] = new Cache(key, val), val94+ this.data[key] = new Cache(key, val)95+ if (fn instanceof Function) fn(val)96 },97 98 /**99 * Get data found matching the given _key_.100 *101 * Examples:102 *99- * cache.get('page:front')103+ * cache.get('page:front', function(val){})104 * // => '<html>...</html>'105 *102- * cache.get('page:*')106+ * cache.get('page:*', function(vals){})107 * // => { 'page:front': '<html>...</html>', 108 * 'page:users': '<html>...</html>',109 * ... }110 *111 * @param {string} key112+ * @param {function} fn113 * @return {mixed}114 * @api public115 */116 112- get: function(key) {117+ get: function(key, fn) {118 exports.Store.prototype.get.apply(this, arguments)119 if (key.indexOf('*') === -1)115- return this.data[key] instanceof Cache120+ return fn(this.data[key] instanceof Cache121 ? this.data[key].val117- : null122+ : null)123 var regexp = this.normalize(key) 119- return this.data.reduce(function(vals, cache){124+ fn(this.data.reduce(function(vals, cache){125 if (regexp.test(cache.key))126 vals[cache.key] = cache.val127 return vals123- }, {})128+ }, {}))129 },130 131 /**132 * Clear data matching the given _key_.133 *134 * Examples:135 *131- * cache.clear('page:front')132- * cache.clear('page:*')136+ * cache.clear('page:front', function(){})137+ * cache.clear('page:*', function(){})138 *139 * @param {string} key140+ * @param {function} fn141 * @api public142 */143 138- clear: function(key) {144+ clear: function(key, fn) {145 if (key.indexOf('*') === -1)146+ delete this.data[key]140- return delete this.data[key]141- var regexp = this.normalize(key)142- for (var key in this.data)143- if (this.data.hasOwnProperty(key))144- if (regexp.test(key))145- delete this.data[key]147+ else {148+ var regexp = this.normalize(key)149+ for (var key in this.data)150+ if (this.data.hasOwnProperty(key))151+ if (regexp.test(key))152+ delete this.data[key]153+ }154+ fn()155 },156 157 /**158 * Reap caches older than _ms_.6 unmodified lines165 var self = this,166 threshold = Number(new Date(Number(new Date) - ms))167 this.data.each(function(cache){168 if (cache.created < threshold)160- self.clear(cache.key)169+ self.clear(cache.key, function(){})170 })171 },172 173 /**53 unmodified linesmodified:lib/express/plugins/static.js+24 added-15 removed
37 unmodified lines383940414243444546474849505142434445464748495053545556515253545556575958596061626364656667686970717245 unmodified lines37 unmodified lines * @api public */ sendTo: function(request) { var cache, file = this.path if (set('cache static files') && (cache = request.cache.get('static:' + file))) return request.contentType(cache.type), request.halt(200, cache.content, 'binary') path.exists(file, function(exists){ if (!exists) return request.halt() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.halt() fs.readFile(file, 'binary', function(err, content){ var file = this.path function sendFromDisc() { path.exists(file, function(exists){ if (!exists) return request.halt() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.halt() fs.readFile(file, 'binary', function(err, content){ if (err) throw err request.contentType(file) if (set('cache static files')) request.cache.set('static:' + file, { type: file, content: content }) request.halt(200, content, 'binary') request.contentType(file) if (set('cache static files')) request.cache.set('static:' + file, { type: file, content: content }) request.halt(200, content, 'binary') }) }) }) }) } if (set('cache static files')) request.cache.get('static:' + file, function(cache){ if (cache) request.contentType(cache.type), request.halt(200, cache.content, 'binary') else sendFromDisc() }) else sendFromDisc() }})
// --- Static45 unmodified linesmodified:lib/express/plugins/view.js+13 added-6 removed
59 unmodified lines60 * @api public61 */62 63 render: function(view, options) {64- var cache,65- self = this,64+ var self = this,65 options = options || {},66 path = set('views') + '/' + view,67 type = path.split('.').slice(-2)[0],68 ext = extname(path),11 unmodified lines80 }))81 else82 self.halt(200, content)83 }85- if (set('cache view contents') && (cache = self.cache.get('view:' + path)))86- render(cache)87- else84+ function renderFromDisc() {85 fs.readFile(path, function(err, content){86 if (err) throw err87 set('cache view contents')91- ? render(self.cache.set('view:' + path, content))88+ ? self.cache.set('view:' + path, content, function(cache){89+ render(cache)90+ })91 : render(content)92 })93+ }94+ if (set('cache view contents'))95+ self.cache.get('view:' + path, function(cache){96+ if (cache) render(cache)97+ else renderFromDisc()98+ })99+ else100+ renderFromDisc()101 }102 })103 }104 }2 unmodified linesmodified:lib/express/request.js+1 added-18 removed
3 unmodified lines4 /**5 * Module dependencies.6 */7 8-var StaticFile = require('express/static').File,9- statusBodies = require('http').STATUS_CODES,8+var statusBodies = require('http').STATUS_CODES,9 queryString = require('querystring'),10 mime = require('express/mime'),11 utils = require('express/utils'),12 url = require('url')266 unmodified lines279 this.header('content-disposition', path280 ? 'attachment; filename="' + path + '"'281 : 'attachment')282 return this284- },285- 286- /**287- * Transfer static file at the given _path_.288- *289- * @param {string} path290- * @return {Request}291- * @api public292- */293- 294- // DEPRECATED: remove in 0.9.0295- 296- sendfile: function(path) {297- Ext.warn('Request#sendfile() is deprecated, it is now part of the Static plugin, use(Static) to re-gain this method')298- (new StaticFile(path)).send(this)299- return this283 }284 })deleted:lib/express/static.js-64 removed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
// Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// DEPRECATED: remove in 0.9.0
/** * Module dependencies. */
var path = require('path'), fs = require('fs') // --- File
exports.File = new Class({ /** * Initialize with file _path_. * * @param {string} path * @api public */ constructor: function(path) { Ext.warn('require("express/static").File is deprecated, use require("express/plugins/static").File') this.path = path if (path.indexOf('..') != -1) Error.raise('InvalidPathError', "`" + path + "' is not a valid path") }, /** * Transfer static file to the given _request_. * * - Ensures the file exists * - Ensures the file is a regular file (not FIFO, Socket, etc) * - Automatically assigns content type * - Halts with 404 when failing * * @param {Request} request * @settings 'cache static files' * @api public */ send: function(request) { var cache, file = this.path if (set('cache static files') && (cache = request.cache.get(file))) return request.contentType(cache.type), request.halt(200, cache.content, 'binary') path.exists(file, function(exists){ if (!exists) return request.halt() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.halt() fs.readFile(file, 'binary', function(err, content){ if (err) throw err request.contentType(file) if (set('cache static files')) request.cache.set(file, { type: file, content: content }) request.halt(200, content, 'binary') }) }) }) }})No newline at end of filemodified:spec/lib/jspec.js+2 added
1519 unmodified lines1520 * @api public1521 */1522 1523 error : function(message, e) {1524+ if ('stack' in e)1525+ require('sys').puts(e.stack + '\n')1526 throw (message ? message : '') + e.toString() + 1527 (e.line ? ' near line ' + e.line : '')1528 },1529 246 unmodified linesmodified:spec/spec.plugins.cache.js+72 added-35 removed
31 unmodified lines32 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'36+ var result37+ store.set('foo', 'bar', function(){38+ store.get('foo', function(val){39+ result = val40+ })41+ })42+ result.should.eql 'bar'43 end44 45 it 'should override existing data'41- store.set('foo', 'bar')42- store.set('foo', 'baz')43- store.get('foo').should.eql 'baz'46+ var result47+ store.set('foo', 'bar', function(){48+ store.set('foo', 'baz', function(){49+ store.get('foo', function(val){50+ result = val51+ })52+ })53+ })54+ result.should.eql 'baz'55 end45- 46- it 'should return data'47- store.set('foo', 'bar').should.eql 'bar'48- end56 end57 58 describe 'given an abitrary key'59 it 'should throw an error'2 unmodified lines62 end63 64 describe 'given an abitrary value'65 it 'should serialize as JSON'66+ var result59- store.set('user', { name: 'tj' }).should.eql { name: 'tj' }67+ store.set('user', { name: 'tj' }, function(val){68+ result = val69+ })70+ result.should.eql { name: 'tj' }71 end72 end73 end74 75 describe '#get()'76 describe 'given a key'66- it 'should return cached value'67- store.set('foo', 'bar')68- store.get('foo').should.eql 'bar'69- end70- 77 it 'should unserialize JSON data'72- store.set('user', { name: 'tj' })73- store.get('user').should.eql { name: 'tj' }78+ var result79+ store.set('user', { name: 'tj' }, function(){80+ store.get('user', function(val){81+ result = val82+ })83+ })84+ result.should.eql { name: 'tj' }85 end86 end87 88 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' }89+ it 'should pass a set of caches'90+ var result91+ store.set('user:1', 'a', function(){92+ store.set('user:2', 'b', function(){93+ store.set('foo', 'bar', function(){94+ store.get('user:*', function(val){95+ result = val96+ })97+ })98+ })99+ })100+ result.should.eql { 'user:1': 'a', 'user:2': 'b' }101 end102 end103 end104 105 describe '#clear()'106 describe 'given a key'107 it 'should delete previous data'90- store.set('foo', 'bar')91- store.clear('foo')92- store.get('foo').should.be_null108+ var result109+ store.set('foo', 'bar', function(){110+ store.clear('foo', function(){111+ store.get('foo', function(val){112+ result = val113+ })114+ })115+ })116+ result.should.be_null117 end118 end119 120 describe 'given wildcards'121 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_null122+ var results = []123+ store.set('user:one', '1', function(){124+ store.set('user:two', '2', function(){125+ store.clear('user:*', function(){126+ store.get('user:one', function(val){127+ results.push(val)128+ store.get('user:two', function(val){129+ results.push(val)130+ })131+ })132+ })133+ }) 134+ })135+ results.should.eql [null, null]136 end137 end138 end139 140 describe '#reap()'141 it 'should destroy caches older than the given age in milliseconds'142 store.set('user:one', '1')110- store.data['user:one'].created = Number(new Date) - 300143+ store.data['user:one'].created = Number((5).minutes.ago)144 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_null145+ store.data['user:two'].created = Number((2).seconds.ago)146+ store.reap((1).minute)147+ store.get('user:one', function(val){148+ val.should.be_null149+ })150+ store.get('user:two', function(val){151+ val.should.not.be_null152+ })153 end154 end155 end156 endmodified:spec/spec.plugins.static.js+4 added-1 removed
49 unmodified lines50 get('/public/*', function(file){51 this.sendfile('spec/fixtures/' + file)52 })53 get('/cached', function(){54- return this.cache.get('static:spec/fixtures/user.json') ? 'yes' : 'no'54+ var self = this55+ this.cache.get('static:spec/fixtures/user.json', function(val){56+ self.halt(200, val ? 'yes' : 'no')57+ })58 })59 get('/cached').body.should.eql 'no'60 get('/public/user.json').body.should.include '"name":'61 get('/cached').body.should.eql 'yes'17 unmodified linesmodified:spec/spec.plugins.view.js+4 added-1 removed
78 unmodified lines79 get('/', function(){80 this.render('hello.haml.html')81 })82 get('/cached', function(){83- return this.cache.get('view:spec/fixtures/hello.haml.html') ? 'yes' : 'no'83+ var self = this84+ this.cache.get('view:spec/fixtures/hello.haml.html', function(val){85+ self.halt(200, val ? 'yes' : 'no')86+ })87 })88 get('/cached').body.should.eql 'no'89 get('/')90 get('/cached').body.should.eql 'yes'62 unmodified lines