Authored by 陈峰

cache add query

1 module.exports = [ 1 module.exports = [
2 { 2 {
3 route: /grass\/article\/\d+$/, 3 route: /grass\/article\/\d+$/,
4 - cacheKey: '$url$yoho-protocol', 4 + query: {
  5 + columnType: Number
  6 + },
5 cacheTime: 900, 7 cacheTime: 900,
6 cache: true 8 cache: true
7 }, 9 },
8 { 10 {
9 route: /grass\/article\/\d+\/user/, 11 route: /grass\/article\/\d+\/user/,
10 - cacheKey: '$url$yoho-protocol',  
11 cache: false 12 cache: false
12 }, 13 },
13 { 14 {
14 route: /grass\/article\/share\/\d+/, 15 route: /grass\/article\/share\/\d+/,
15 - cacheKey: '$url$yoho-protocol',  
16 cacheTime: 900, 16 cacheTime: 900,
17 cache: true 17 cache: true
18 }, 18 },
19 { 19 {
20 route: /grass\/article\/detail\/\d+/, 20 route: /grass\/article\/detail\/\d+/,
21 - cacheKey: '$url$yoho-protocol',  
22 cacheTime: 900, 21 cacheTime: 900,
23 cache: true 22 cache: true
24 }, 23 },
25 { 24 {
26 route: /grass\/topic\/\d+\/(.*)/, 25 route: /grass\/topic\/\d+\/(.*)/,
27 - cacheKey: '$url$yoho-protocol',  
28 cacheTime: 900, 26 cacheTime: 900,
29 cache: true 27 cache: true
30 }, 28 },
31 { 29 {
32 route: /grass\/author\/\d+\/\d+/, 30 route: /grass\/author\/\d+\/\d+/,
33 - cacheKey: '$url$yoho-protocol',  
34 cacheTime: 900, 31 cacheTime: 900,
35 cache: true 32 cache: true
36 }, 33 },
37 { 34 {
38 route: /grass\/author\/mine/, 35 route: /grass\/author\/mine/,
39 - cacheKey: '$url$yoho-protocol',  
40 cacheTime: 900, 36 cacheTime: 900,
41 cache: true 37 cache: true
42 }, 38 },
43 { 39 {
44 route: /grass\/author\/follow\/\d+\/\d+/, 40 route: /grass\/author\/follow\/\d+\/\d+/,
45 - cacheKey: '$url$yoho-protocol',  
46 cacheTime: 900, 41 cacheTime: 900,
47 cache: true 42 cache: true
48 }, 43 },
49 { 44 {
50 route: /grass\/author\/fans\/\d+\/\d+/, 45 route: /grass\/author\/fans\/\d+\/\d+/,
51 - cacheKey: '$url$yoho-protocol',  
52 cacheTime: 900, 46 cacheTime: 900,
53 cache: true 47 cache: true
54 }, 48 },
55 { 49 {
56 route: /grass\/mine\/follow/, 50 route: /grass\/mine\/follow/,
57 - cacheKey: '$url$yoho-protocol',  
58 cacheTime: 900, 51 cacheTime: 900,
59 cache: true 52 cache: true
60 }, 53 },
61 { 54 {
62 route: /grass\/mine\/fans/, 55 route: /grass\/mine\/fans/,
63 - cacheKey: '$url$yoho-protocol',  
64 cacheTime: 900, 56 cacheTime: 900,
65 cache: true 57 cache: true
66 }, 58 },
@@ -62,7 +62,7 @@ const versionToNumber = (str, len = 3) => { @@ -62,7 +62,7 @@ const versionToNumber = (str, len = 3) => {
62 } 62 }
63 63
64 return verNum; 64 return verNum;
65 -} 65 +};
66 66
67 const getImmersedStatus = (req) => { 67 const getImmersedStatus = (req) => {
68 let status = false; 68 let status = false;
@@ -76,7 +76,7 @@ const getImmersedStatus = (req) => { @@ -76,7 +76,7 @@ const getImmersedStatus = (req) => {
76 } 76 }
77 77
78 return status; 78 return status;
79 -} 79 +};
80 80
81 const getContext = (req) => { 81 const getContext = (req) => {
82 return { 82 return {
@@ -107,16 +107,20 @@ const handlerError = (err = {}, req, res, next) => { @@ -107,16 +107,20 @@ const handlerError = (err = {}, req, res, next) => {
107 return next(err); 107 return next(err);
108 }; 108 };
109 109
110 -const getCacheKey = (req, cacheKey = '') => {  
111 - const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false); 110 +const getCacheKey = (req, route) => {
112 const urlObj = url.parse(req.url); 111 const urlObj = url.parse(req.url);
113 const isIos = req.yoho.isiOS; 112 const isIos = req.yoho.isiOS;
114 - const yohoProtocol = (req.get('User-Agent').indexOf('yoho-protocol') >= 0 && isYohoProtocol) ? 'yoho-protocol' : ''; 113 + let ck = urlObj.pathname;
  114 +
  115 + if (route.query) {
  116 + const qks = Object.keys(route.query);
115 117
116 - return md5(cacheKey  
117 - .replace('$url', urlObj.pathname)  
118 - .replace('$params', urlObj.query || '')  
119 - .replace('$yoho-protocol', yohoProtocol) + (isIos ? 'ios' : 'android')); 118 + ck += `?${qks.map(qk => `${qk}=${req.query && req.query[qk] || ''}`).join('&')}`;
  119 + }
  120 +
  121 + ck += `|${isIos ? 'ios' : 'android'}`;
  122 +
  123 + return md5(ck);
120 }; 124 };
121 125
122 const render = (route) => { 126 const render = (route) => {
@@ -124,12 +128,11 @@ const render = (route) => { @@ -124,12 +128,11 @@ const render = (route) => {
124 try { 128 try {
125 res.setHeader('X-YOHO-Version', pkg.version); 129 res.setHeader('X-YOHO-Version', pkg.version);
126 const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false); 130 const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
127 - const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);  
128 131
129 if (isDegrade) { 132 if (isDegrade) {
130 return res.send(degradeHtml); 133 return res.send(degradeHtml);
131 } 134 }
132 - const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0; 135 + const ck = getCacheKey(req, route);
133 136
134 if (config.useCache && route.cache && ck) { 137 if (config.useCache && route.cache && ck) {
135 const html = await redis.getAsync(ck); 138 const html = await redis.getAsync(ck);
@@ -161,23 +164,17 @@ const render = (route) => { @@ -161,23 +164,17 @@ const render = (route) => {
161 let resources = context.renderResourceHints(); 164 let resources = context.renderResourceHints();
162 const states = context.renderState(); 165 const states = context.renderState();
163 let asyncScripts; 166 let asyncScripts;
164 - let zk = {}; 167 + let zk = {
  168 + asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true)
  169 + };
165 170
166 if (process.env.NODE_ENV === 'production') { 171 if (process.env.NODE_ENV === 'production') {
167 zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false); 172 zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
168 - zk.asyncJs = _.get(req.app.locals.wap, 'webapp.ios-async-js', true);  
169 } 173 }
170 174
171 if (req.yoho.isiOS && zk.asyncJs) { 175 if (req.yoho.isiOS && zk.asyncJs) {
172 asyncScripts = asyncLoadScripts(scripts); 176 asyncScripts = asyncLoadScripts(scripts);
173 } 177 }
174 - if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&  
175 - route.cacheKey &&  
176 - route.cacheKey.indexOf('$yoho-protocol') >= 0 &&  
177 - isYohoProtocol) {  
178 - styles = styles.replace(/"\/\//g, '"yoho-protocol://');  
179 - resources = resources.replace(/<link rel="preload" href="[^"]+" as="style">/g, '');  
180 - }  
181 178
182 const result = template({ 179 const result = template({
183 html, 180 html,
@@ -205,7 +202,7 @@ const devRender = (route) => { @@ -205,7 +202,7 @@ const devRender = (route) => {
205 return async(req, res, next) => { 202 return async(req, res, next) => {
206 try { 203 try {
207 res.setHeader('X-YOHO-Version', pkg.version); 204 res.setHeader('X-YOHO-Version', pkg.version);
208 - const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0; 205 + const ck = getCacheKey(req, route);
209 206
210 const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false); 207 const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
211 208
@@ -249,11 +246,6 @@ const devRender = (route) => { @@ -249,11 +246,6 @@ const devRender = (route) => {
249 } 246 }
250 let {styles, scripts, resources, states, html} = msg; 247 let {styles, scripts, resources, states, html} = msg;
251 248
252 - if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&  
253 - route.cacheKey &&  
254 - route.cacheKey.indexOf('$yoho-protocol') >= 0) {  
255 - styles = styles.replace(/"\/\//g, '"yoho-protocol://');  
256 - }  
257 const result = template({ 249 const result = template({
258 html, 250 html,
259 styles, 251 styles,