Showing
6 changed files
with
91 additions
and
11 deletions
@@ -94,6 +94,7 @@ try { | @@ -94,6 +94,7 @@ try { | ||
94 | const setPageInfo = require('./doraemon/middleware/set-pageinfo'); | 94 | const setPageInfo = require('./doraemon/middleware/set-pageinfo'); |
95 | const devtools = require('./doraemon/middleware/devtools'); | 95 | const devtools = require('./doraemon/middleware/devtools'); |
96 | const seo = require('./doraemon/middleware/seo'); | 96 | const seo = require('./doraemon/middleware/seo'); |
97 | + const pageCache = require('./doraemon/middleware/page-cache'); | ||
97 | 98 | ||
98 | // YOHO 前置中间件 | 99 | // YOHO 前置中间件 |
99 | app.use(subDomain()); | 100 | app.use(subDomain()); |
@@ -107,16 +108,8 @@ try { | @@ -107,16 +108,8 @@ try { | ||
107 | app.use(devtools()); | 108 | app.use(devtools()); |
108 | } | 109 | } |
109 | 110 | ||
110 | - // set no cache | ||
111 | - app.use((req, res, next) => { | ||
112 | - if (req.get('X-Requested-With') === 'XMLHttpRequest') { | ||
113 | - res.set('Cache-Control', 'no-cache'); | ||
114 | - } | ||
115 | - next(); | ||
116 | - }); | ||
117 | - | 111 | + app.use(pageCache()); |
118 | require('./dispatch')(app); | 112 | require('./dispatch')(app); |
119 | - | ||
120 | app.all('*', errorHanlder.notFound()); // 404 | 113 | app.all('*', errorHanlder.notFound()); // 404 |
121 | 114 | ||
122 | // YOHO 后置中间件 | 115 | // YOHO 后置中间件 |
@@ -13,6 +13,7 @@ var app = express(); | @@ -13,6 +13,7 @@ var app = express(); | ||
13 | var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root | 13 | var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root |
14 | 14 | ||
15 | app.on('mount', function(parent) { | 15 | app.on('mount', function(parent) { |
16 | + | ||
16 | delete parent.locals.settings; // 不继承父 App 的设置 | 17 | delete parent.locals.settings; // 不继承父 App 的设置 |
17 | Object.assign(app.locals, parent.locals); | 18 | Object.assign(app.locals, parent.locals); |
18 | }); | 19 | }); |
config/cache.js
0 → 100644
1 | + | ||
2 | +'use strict'; | ||
3 | + | ||
4 | +const SECOND = 1; | ||
5 | +const MINUTE = 60 * SECOND; | ||
6 | + | ||
7 | +const cachePage = { | ||
8 | + '/': 5 * MINUTE, | ||
9 | + | ||
10 | + // 频道页 | ||
11 | + '/boys': 30 * SECOND, | ||
12 | + '/woman': 30 * SECOND, | ||
13 | + '/kids': 30 * SECOND, | ||
14 | + '/lifestyle': 30 * SECOND, | ||
15 | + | ||
16 | + // 商品详情页 | ||
17 | + '/product/\/pro_([\d]+)_([\d]+)\/(.*)/': 30 * MINUTE, | ||
18 | + | ||
19 | + // 逛 | ||
20 | + '/guang': 1 * MINUTE, | ||
21 | + '/guang/info/index': 10 * MINUTE, | ||
22 | + '/guang/author/index': 1 * MINUTE, | ||
23 | + '/guang/tags/index': 1 * MINUTE, | ||
24 | + | ||
25 | + // 领券中心 | ||
26 | + '/activity/coupon/floor': 5 * MINUTE, | ||
27 | + | ||
28 | + // 商品列表 | ||
29 | + '/product/list/index': 5 * MINUTE, | ||
30 | + '/product/index/index': 5 * MINUTE, | ||
31 | + | ||
32 | + // 秒杀列表 | ||
33 | + '/product/seckill': 30 * SECOND, | ||
34 | + | ||
35 | + // 秒杀详情 | ||
36 | + | ||
37 | + // sale | ||
38 | + '/product/sale': 5 * MINUTE, | ||
39 | + | ||
40 | + '/product/outlet': 30 * SECOND, | ||
41 | + | ||
42 | + '/product/index/brand': 2 * MINUTE | ||
43 | + | ||
44 | +}; | ||
45 | + | ||
46 | +module.exports = cachePage; |
@@ -73,7 +73,7 @@ module.exports = { | @@ -73,7 +73,7 @@ module.exports = { | ||
73 | port: 4444 // influxdb port | 73 | port: 4444 // influxdb port |
74 | }, | 74 | }, |
75 | console: { | 75 | console: { |
76 | - level: 'info', | 76 | + level: 'debug', |
77 | colorize: 'all', | 77 | colorize: 'all', |
78 | prettyPrint: true | 78 | prettyPrint: true |
79 | } | 79 | } |
doraemon/middleware/page-cache.js
0 → 100644
1 | + | ||
2 | +'use strict'; | ||
3 | + | ||
4 | +const path = require('path'); | ||
5 | +const cachePage = require('../../config/cache'); | ||
6 | + | ||
7 | +module.exports = () => { | ||
8 | + return (req, res, next) => { | ||
9 | + if (req.get('X-Requested-With') === 'XMLHttpRequest') { | ||
10 | + res.set('Cache-Control', 'no-cache'); | ||
11 | + } | ||
12 | + | ||
13 | + function onRender() { | ||
14 | + let route = req.route ? req.route.path : ''; | ||
15 | + let appPath = req.app.mountpath; | ||
16 | + let key = path.join(appPath, route.toString()); // route may be a regexp | ||
17 | + | ||
18 | + req.app.set('etag', false); | ||
19 | + | ||
20 | + // 如果存在cache配置,并且业务代码中没有设置 | ||
21 | + if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') { | ||
22 | + res.set({ | ||
23 | + 'Cache-Control': 'max-age=' + cachePage[key] | ||
24 | + }); | ||
25 | + res.removeHeader('Pragma'); | ||
26 | + res.removeHeader('Expires'); | ||
27 | + } else { | ||
28 | + res.set({ | ||
29 | + 'Cache-Control': 'no-cache', | ||
30 | + Pragma: 'no-cache', | ||
31 | + Expires: new Date(1900, 0, 1, 0, 0, 0, 0) | ||
32 | + }); | ||
33 | + } | ||
34 | + | ||
35 | + } | ||
36 | + | ||
37 | + res.on('render', onRender); | ||
38 | + next(); | ||
39 | + }; | ||
40 | +}; |
@@ -39,7 +39,7 @@ | @@ -39,7 +39,7 @@ | ||
39 | "request-promise": "^3.0.0", | 39 | "request-promise": "^3.0.0", |
40 | "serve-favicon": "^2.3.0", | 40 | "serve-favicon": "^2.3.0", |
41 | "uuid": "^2.0.3", | 41 | "uuid": "^2.0.3", |
42 | - "yoho-node-lib": "0.1.23", | 42 | + "yoho-node-lib": "0.1.25", |
43 | "yoho-zookeeper": "^1.0.3" | 43 | "yoho-zookeeper": "^1.0.3" |
44 | }, | 44 | }, |
45 | "devDependencies": { | 45 | "devDependencies": { |
-
Please register or login to post a comment