Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ind.ie Projects
Set
Commits
bcf09db0
Verified
Commit
bcf09db0
authored
Dec 03, 2017
by
Aral Balkan
Browse files
Add document cache
- Must be explicitly activated
parent
aabfac46
Changes
5
Hide whitespace changes
Inline
Side-by-side
coffee/lib/set-express.coffee
View file @
bcf09db0
...
...
@@ -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
...
...
coffee/routes/main.coffee
View file @
bcf09db0
...
...
@@ -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
}
}
coffee/routes/partials.coffee
View file @
bcf09db0
...
...
@@ -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
coffee/routes/readme.coffee
View file @
bcf09db0
...
...
@@ -25,4 +25,6 @@ exports.route = (request, response) ->
data
.
title
=
'Read me!'
data
.
readme
=
readme
data
.
__set
.
cache
=
true
response
.
render
'readme'
,
data
coffee/routes/simple.coffee
View file @
bcf09db0
...
...
@@ -37,6 +37,7 @@ exports.route = (request, response) ->
return
'https://'
+
value
+
'.ie'
isSelected
:
(
value
)
->
return
'tab selected'
cache
:
true
}
response
.
render
'simple'
,
data
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment