Verified Commit bcf09db0 authored by Aral Balkan's avatar Aral Balkan
Browse files

Add document cache

  - Must be explicitly activated
parent aabfac46
......@@ -37,6 +37,7 @@ set = (fs.readFileSync __dirname + '/set.js', 'utf8').toString()
partialCache = {}
templateCache = {}
documentCache = {}
includeRegEx = /\<!--\s*include\s+(\S+)\s*--\>/g
......@@ -105,6 +106,25 @@ render = (template, data) ->
a = new Date()
#
# To use document cache, explicitly set it on by passing
#
# { …, __set: {cache: true} }
#
# as part of your data object.
useCache = (data['__set'] != undefined) && (data['__set']['cache'] == true)
if useCache
# Check if the document rendered with this data exists in the cache.
documentHash = crypto.createHash('md5').update("#{template}#{JSON.stringify(data)}").digest('hex')
cachedDocument = documentCache[documentHash]
if cachedDocument != undefined
console.log "Returning document from cache."
console.log "Took: #{(new Date() - a)}"
return cachedDocument
# Add partials to template
template = addPartials template
......@@ -204,6 +224,10 @@ render = (template, data) ->
# Add the doctype to the top of the rest of the HTML
html = "#{doctypeHTML}#{html}"
if useCache
# Save the rendered document (for this data) in the cache.
documentCache[documentHash] = html
console.log "Took: #{(new Date() - a)} ms."
return html
......
......@@ -15,4 +15,4 @@ exports.route = (request, response) ->
meta = require('../../package.json')
version = meta.version
response.render 'main', {version: version}
response.render 'main', {version: version, __set:{cache: true}}
......@@ -11,4 +11,9 @@
################################################################################
exports.route = (request, response) ->
response.render 'partials', {title: 'Ind.ie Labs: Set: Partials'}
data =
title: 'Ind.ie Labs: Set: Partials'
__set:
cache: true # Use the document cache
response.render 'partials', data
......@@ -25,4 +25,6 @@ exports.route = (request, response) ->
data.title = 'Read me!'
data.readme = readme
data.__set.cache = true
response.render 'readme', data
......@@ -37,6 +37,7 @@ exports.route = (request, response) ->
return 'https://' + value + '.ie'
isSelected: (value) ->
return 'tab selected'
cache: true
}
response.render 'simple', data
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment