Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周韬
node-sample
Commits
6ffdc5d3
Commit
6ffdc5d3
authored
Apr 12, 2017
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make micro cache controlled via env var
parent
159abf80
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
5 deletions
+7
-5
server.js
server.js
+7
-5
No files found.
server.js
View file @
6ffdc5d3
...
@@ -8,6 +8,7 @@ const resolve = file => path.resolve(__dirname, file)
...
@@ -8,6 +8,7 @@ const resolve = file => path.resolve(__dirname, file)
const
{
createBundleRenderer
}
=
require
(
'vue-server-renderer'
)
const
{
createBundleRenderer
}
=
require
(
'vue-server-renderer'
)
const
isProd
=
process
.
env
.
NODE_ENV
===
'production'
const
isProd
=
process
.
env
.
NODE_ENV
===
'production'
const
useMicroCache
=
process
.
env
.
MICRO_CACHE
!==
'false'
const
serverInfo
=
const
serverInfo
=
`express/
${
require
(
'express/package.json'
).
version
}
`
+
`express/
${
require
(
'express/package.json'
).
version
}
`
+
`vue-server-renderer/
${
require
(
'vue-server-renderer/package.json'
).
version
}
`
`vue-server-renderer/
${
require
(
'vue-server-renderer/package.json'
).
version
}
`
...
@@ -64,16 +65,17 @@ app.use('/manifest.json', serve('./manifest.json', true))
...
@@ -64,16 +65,17 @@ app.use('/manifest.json', serve('./manifest.json', true))
app
.
use
(
'/service-worker.js'
,
serve
(
'./dist/service-worker.js'
))
app
.
use
(
'/service-worker.js'
,
serve
(
'./dist/service-worker.js'
))
// 1-second microcache.
// 1-second microcache.
const
pageCache
=
LRU
({
// https://www.nginx.com/blog/benefits-of-microcaching-nginx/
const
microCache
=
LRU
({
max
:
100
,
max
:
100
,
maxAge
:
1000
maxAge
:
1000
})
})
// since this app has no user-specific content, every page is cacheable.
// since this app has no user-specific content, every page is
micro-
cacheable.
// if your app involves user-specific content, you need to implement custom
// if your app involves user-specific content, you need to implement custom
// logic to determine whether a request is cacheable based on its url and
// logic to determine whether a request is cacheable based on its url and
// headers.
// headers.
const
isCacheable
=
req
=>
tru
e
const
isCacheable
=
req
=>
useMicroCach
e
app
.
get
(
'*'
,
(
req
,
res
)
=>
{
app
.
get
(
'*'
,
(
req
,
res
)
=>
{
if
(
!
renderer
)
{
if
(
!
renderer
)
{
...
@@ -98,7 +100,7 @@ app.get('*', (req, res) => {
...
@@ -98,7 +100,7 @@ app.get('*', (req, res) => {
const
cacheable
=
isCacheable
(
req
)
const
cacheable
=
isCacheable
(
req
)
if
(
cacheable
)
{
if
(
cacheable
)
{
const
hit
=
page
Cache
.
get
(
req
.
url
)
const
hit
=
micro
Cache
.
get
(
req
.
url
)
if
(
hit
)
{
if
(
hit
)
{
if
(
!
isProd
)
{
if
(
!
isProd
)
{
console
.
log
(
`cache hit!`
)
console
.
log
(
`cache hit!`
)
...
@@ -113,7 +115,7 @@ app.get('*', (req, res) => {
...
@@ -113,7 +115,7 @@ app.get('*', (req, res) => {
}
}
res
.
end
(
html
)
res
.
end
(
html
)
if
(
cacheable
)
{
if
(
cacheable
)
{
page
Cache
.
set
(
req
.
url
,
html
)
micro
Cache
.
set
(
req
.
url
,
html
)
}
}
if
(
!
isProd
)
{
if
(
!
isProd
)
{
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
)
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
)
...
...
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