Pre-caching views in memory. Closes #253
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This mirrors the partial caching functionality. This causes a slight overhead at boot time, however prevents the need to perform cache checks per render() call.
authored by visionmedia · committed by visionmedia · parent c29852f · Browse files · 2 files·+9 added−46 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+9 added−46 removed
- tests:+0 added−20 removed
- other:+9 added−26 removed
Provenance
- Commit
faaef54- Tree
fb4e3fa- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
c29852f
2 files changed+9 added−46 removed
modified:lib/express/plugins/view.js+9 added-26 removed
30 unmodified lines31323334353637383987 unmodified lines1271281291301301311321311321331341351361361371381381391401401411421431441451461461471481491501511511521531521541551561571581591601611621631641651661671681691531541551562 unmodified lines30 unmodified linesfunction cacheFiles(type) { var dir = set(type) fs.readdirSync(dir).each(function(file){ file = dir + '/' + file if (!fs.statSync(file).isFile()) return cache[type][file] = fs.readFileSync(file) })}
87 unmodified lines
render: function(view, options) { var self = this, options = options || {}, partial = options.partial, path = set(partial ? 'partials' : 'views') + '/' + view, type = path.split('.').slice(-2)[0], type = options.partial ? 'partials' : 'views', path = set(type) + '/' + view, engine = path.split('.').slice(-2)[0], ext = extname(path), layout = options.layout === undefined ? 'layout' : options.layout options.context = options.context || this if (!partial) self.contentType(ext) if (type === 'views') self.contentType(ext) function render(content) { content = (engines[type] = engines[type] || require(type)).render(content, options) content = (engines[engine] = engines[engine] || require(engine)).render(content, options) if (layout) self.render(layout + '.' + type + ext, options.mergeDeep({ self.render(layout + '.' + engine + ext, options.mergeDeep({ layout: false, locals: { body: content } })) else if (partial) else if (type === 'partials') return content else self.halt(200, content) } function renderFromDisc() { if (partial) return render(cache.partials[path] || fs.readFileSync(path)) return render(cache[type][path] || fs.readFileSync(path)) fs.readFile(path, function(err, content){ if (err) throw err set('cache view contents') ? self.cache.set('view:' + path, content, function(cache){ render(cache) }) : render(content) }) } if (!partial && set('cache view contents')) self.cache.get('view:' + path, function(cache){ if (cache) return render(cache) else return renderFromDisc() }) else return renderFromDisc() } }) } }2 unmodified linesmodified:spec/spec.plugins.view.js-20 removed
119 unmodified lines120 get('/').body.should.include '<h2>Hello'121 end122 end123 124- describe 'when "cache view contents" is enabled'125- it 'should read the views into memory only once'126- enable('cache view contents')127- get('/', function(){128- this.render('hello.haml.html')129- })130- get('/cached', function(){131- var self = this132- this.cache.get('view:spec/fixtures/hello.haml.html', function(val){133- self.halt(200, val ? 'yes' : 'no')134- })135- })136- get('/cached').body.should.eql 'no'137- get('/')138- get('/cached').body.should.eql 'yes'139- get('/')140- get('/cached').body.should.eql 'yes'141- end142- end143- 124 describe 'when engine cannot be found'125 it 'should throw an error'126 get('/', function(){127 this.render('user.invalid.html')53 unmodified lines