Merge branch 'pre-cache-views'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents 7d100da, faaef54 · Browse files · 2 files·+34 added−57 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+34 added−57 removed
- tests:+0 added−20 removed
- other:+34 added−37 removed
Provenance
- Commit
6806f95- Tree
fb4e3fa- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
7d100da- Parent 2
faaef54
2 files changed+34 added−57 removed
modified:lib/express/plugins/view.js+34 added-37 removed
13 unmodified lines141516171818192021212223242526272829303132333435363738394041424310 unmodified lines545556575859604261626364656644454647484950516768697056 unmodified lines1271281291301161171181311321331341351361221371381241391401261411421431441451461321471481491501511371381391521401411421431441451461471481491501511521531541551531541551562 unmodified lines13 unmodified lines
var engines = {}
/** * Cache partial store * View cache. */ var partials = {}var cache = { views: {}, partials: {}}
/** * Cache view files where _type_ is * "partials" or "views". * * @param {string} type * @api public */
function 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) })}
// --- View
exports.View = Plugin.extend({ extend: {10 unmodified lines set('views', function(){ return set('root') + '/views' }) if (!set('partials')) set('partials', function(){ return set('views') + '/partials' }) // Cache views in memory // Cache partials in memory if (set('cache view contents')) cacheFiles('views') if (set('cache view partials')) cacheFiles('partials') if (set('cache view partials')) { var dir = set('partials') fs.readdirSync(dir).each(function(file){ file = dir + '/' + file partials[file] = fs.readFileSync(file) }) } // Request Request.include({ 56 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(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