Authored by 陈峰

cache add query

module.exports = [
{
route: /grass\/article\/\d+$/,
cacheKey: '$url$yoho-protocol',
query: {
columnType: Number
},
cacheTime: 900,
cache: true
},
{
route: /grass\/article\/\d+\/user/,
cacheKey: '$url$yoho-protocol',
cache: false
},
{
route: /grass\/article\/share\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/article\/detail\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/topic\/\d+\/(.*)/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/\d+\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/mine/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/follow\/\d+\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/fans\/\d+\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/mine\/follow/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/mine\/fans/,
cacheKey: '$url$yoho-protocol',
cacheTime: 900,
cache: true
},
... ...
... ... @@ -62,7 +62,7 @@ const versionToNumber = (str, len = 3) => {
}
return verNum;
}
};
const getImmersedStatus = (req) => {
let status = false;
... ... @@ -76,7 +76,7 @@ const getImmersedStatus = (req) => {
}
return status;
}
};
const getContext = (req) => {
return {
... ... @@ -107,16 +107,20 @@ const handlerError = (err = {}, req, res, next) => {
return next(err);
};
const getCacheKey = (req, cacheKey = '') => {
const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
const getCacheKey = (req, route) => {
const urlObj = url.parse(req.url);
const isIos = req.yoho.isiOS;
const yohoProtocol = (req.get('User-Agent').indexOf('yoho-protocol') >= 0 && isYohoProtocol) ? 'yoho-protocol' : '';
let ck = urlObj.pathname;
if (route.query) {
const qks = Object.keys(route.query);
return md5(cacheKey
.replace('$url', urlObj.pathname)
.replace('$params', urlObj.query || '')
.replace('$yoho-protocol', yohoProtocol) + (isIos ? 'ios' : 'android'));
ck += `?${qks.map(qk => `${qk}=${req.query && req.query[qk] || ''}`).join('&')}`;
}
ck += `|${isIos ? 'ios' : 'android'}`;
return md5(ck);
};
const render = (route) => {
... ... @@ -124,12 +128,11 @@ const render = (route) => {
try {
res.setHeader('X-YOHO-Version', pkg.version);
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
if (isDegrade) {
return res.send(degradeHtml);
}
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
const ck = getCacheKey(req, route);
if (config.useCache && route.cache && ck) {
const html = await redis.getAsync(ck);
... ... @@ -161,23 +164,17 @@ const render = (route) => {
let resources = context.renderResourceHints();
const states = context.renderState();
let asyncScripts;
let zk = {};
let zk = {
asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true)
};
if (process.env.NODE_ENV === 'production') {
zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
zk.asyncJs = _.get(req.app.locals.wap, 'webapp.ios-async-js', true);
}
if (req.yoho.isiOS && zk.asyncJs) {
asyncScripts = asyncLoadScripts(scripts);
}
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
route.cacheKey &&
route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
isYohoProtocol) {
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
resources = resources.replace(/<link rel="preload" href="[^"]+" as="style">/g, '');
}
const result = template({
html,
... ... @@ -205,7 +202,7 @@ const devRender = (route) => {
return async(req, res, next) => {
try {
res.setHeader('X-YOHO-Version', pkg.version);
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
const ck = getCacheKey(req, route);
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
... ... @@ -249,11 +246,6 @@ const devRender = (route) => {
}
let {styles, scripts, resources, states, html} = msg;
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
route.cacheKey &&
route.cacheKey.indexOf('$yoho-protocol') >= 0) {
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
}
const result = template({
html,
styles,
... ...