Authored by 姜枫

fix cache

@@ -78,6 +78,7 @@ try { @@ -78,6 +78,7 @@ try {
78 const setYohoData = require('./doraemon/middleware/set-yoho-data'); 78 const setYohoData = require('./doraemon/middleware/set-yoho-data');
79 const errorHanlder = require('./doraemon/middleware/error-handler'); 79 const errorHanlder = require('./doraemon/middleware/error-handler');
80 const setPageInfo = require('./doraemon/middleware/set-pageinfo'); 80 const setPageInfo = require('./doraemon/middleware/set-pageinfo');
  81 + const pageCache = require('./doraemon/middleware/page-cache');
81 82
82 // YOHO 前置中间件 83 // YOHO 前置中间件
83 app.use(subDomain()); 84 app.use(subDomain());
@@ -88,6 +89,7 @@ try { @@ -88,6 +89,7 @@ try {
88 app.use(seo()); 89 app.use(seo());
89 app.use(setPageInfo()); 90 app.use(setPageInfo());
90 91
  92 + app.use(pageCache());
91 require('./dispatch')(app); 93 require('./dispatch')(app);
92 94
93 app.all('*', errorHanlder.notFound()); // 404 95 app.all('*', errorHanlder.notFound()); // 404
@@ -4,28 +4,15 @@ @@ -4,28 +4,15 @@
4 * @date: 2016/10/11 4 * @date: 2016/10/11
5 */ 5 */
6 6
7 -var express = require('express'),  
8 - path = require('path'),  
9 - hbs = require('express-handlebars'); 7 +var express = require('express');
10 8
11 var app = express(); 9 var app = express();
12 10
13 -// set view engin  
14 -var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root  
15 -  
16 app.on('mount', function(parent) { 11 app.on('mount', function(parent) {
17 delete parent.locals.settings; // 不继承父 App 的设置 12 delete parent.locals.settings; // 不继承父 App 的设置
18 Object.assign(app.locals, parent.locals); 13 Object.assign(app.locals, parent.locals);
19 }); 14 });
20 15
21 -app.set('views', path.join(__dirname, 'views/action'));  
22 -app.engine('.hbs', hbs({  
23 - extname: '.hbs',  
24 - defaultLayout: 'layout',  
25 - layoutsDir: doraemon,  
26 - partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],  
27 - helpers: global.yoho.helpers  
28 -}));  
29 16
30 // router 17 // router
31 app.use(require('./router')); 18 app.use(require('./router'));
@@ -23,7 +23,7 @@ app.use(global.yoho.hbs({ @@ -23,7 +23,7 @@ app.use(global.yoho.hbs({
23 extname: '.hbs', 23 extname: '.hbs',
24 defaultLayout: 'layout', 24 defaultLayout: 'layout',
25 layoutsDir: doraemon, 25 layoutsDir: doraemon,
26 - partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`], 26 + partialsDir: [path.join(__dirname, 'views/partial')],
27 views: path.join(__dirname, 'views/action'), 27 views: path.join(__dirname, 'views/action'),
28 helpers: global.yoho.helpers 28 helpers: global.yoho.helpers
29 })); 29 }));
@@ -12,7 +12,6 @@ const config = global.yoho.config; @@ -12,7 +12,6 @@ const config = global.yoho.config;
12 const helpers = global.yoho.helpers; 12 const helpers = global.yoho.helpers;
13 const cache = global.yoho.cache; 13 const cache = global.yoho.cache;
14 const logger = global.yoho.logger; 14 const logger = global.yoho.logger;
15 -const config = require('../../../config/common');  
16 const images = require('../../../utils/images.js'); 15 const images = require('../../../utils/images.js');
17 16
18 const getSortByConditionAsync = (condition) => { 17 const getSortByConditionAsync = (condition) => {
@@ -64,7 +64,7 @@ module.exports = { @@ -64,7 +64,7 @@ module.exports = {
64 port: '4444' // influxdb port 64 port: '4444' // influxdb port
65 }, 65 },
66 console: { 66 console: {
67 - level: 'error', 67 + level: 'debug',
68 colorize: 'all', 68 colorize: 'all',
69 prettyPrint: true 69 prettyPrint: true
70 } 70 }
@@ -3,12 +3,10 @@ @@ -3,12 +3,10 @@
3 3
4 const path = require('path'); 4 const path = require('path');
5 const cachePage = require('../../config/cache'); 5 const cachePage = require('../../config/cache');
  6 +const logger = global.yoho.logger;
6 7
7 module.exports = () => { 8 module.exports = () => {
8 return (req, res, next) => { 9 return (req, res, next) => {
9 - if (req.get('X-Requested-With') === 'XMLHttpRequest') {  
10 - res.set('Cache-Control', 'no-cache');  
11 - }  
12 10
13 function onRender() { 11 function onRender() {
14 let route = req.route ? req.route.path : ''; 12 let route = req.route ? req.route.path : '';
@@ -17,6 +15,8 @@ module.exports = () => { @@ -17,6 +15,8 @@ module.exports = () => {
17 15
18 req.app.set('etag', false); 16 req.app.set('etag', false);
19 17
  18 + logger.debug(`route: ${key} cache = ${cachePage[key]}`);
  19 +
20 // 如果存在cache配置,并且业务代码中没有设置 20 // 如果存在cache配置,并且业务代码中没有设置
21 if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') { 21 if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') {
22 res.set({ 22 res.set({
@@ -24,6 +24,8 @@ module.exports = () => { @@ -24,6 +24,8 @@ module.exports = () => {
24 }); 24 });
25 res.removeHeader('Pragma'); 25 res.removeHeader('Pragma');
26 res.removeHeader('Expires'); 26 res.removeHeader('Expires');
  27 + } else if (req.get('X-Requested-With') === 'XMLHttpRequest') {
  28 + res.set('Cache-Control', 'no-cache');
27 } else { 29 } else {
28 res.set({ 30 res.set({
29 'Cache-Control': 'no-cache', 31 'Cache-Control': 'no-cache',