Authored by 姜枫

fix cache

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