Authored by 陈峰

解决lru内存占用过大问题

@@ -2,6 +2,7 @@ const fs = require('fs'); @@ -2,6 +2,7 @@ const fs = require('fs');
2 const path = require('path'); 2 const path = require('path');
3 const rp = require('request-promise'); 3 const rp = require('request-promise');
4 const LRU = require('lru-cache'); 4 const LRU = require('lru-cache');
  5 +const url = require('url');
5 const _ = require('lodash'); 6 const _ = require('lodash');
6 const pkg = require('../../package.json'); 7 const pkg = require('../../package.json');
7 const logger = global.yoho.logger; 8 const logger = global.yoho.logger;
@@ -15,22 +16,27 @@ const routes = [ @@ -15,22 +16,27 @@ const routes = [
15 }, 16 },
16 { 17 {
17 route: '/channel', 18 route: '/channel',
  19 + cacheRule: 'pathname',
18 cache: true 20 cache: true
19 }, 21 },
20 { 22 {
21 route: '/channel/search', 23 route: '/channel/search',
  24 + cacheRule: 'pathname',
22 cache: true 25 cache: true
23 }, 26 },
24 { 27 {
25 route: '/channel/men', 28 route: '/channel/men',
  29 + cacheRule: 'pathname',
26 cache: true 30 cache: true
27 }, 31 },
28 { 32 {
29 route: '/channel/women', 33 route: '/channel/women',
  34 + cacheRule: 'pathname',
30 cache: true 35 cache: true
31 }, 36 },
32 { 37 {
33 route: '/about', 38 route: '/about',
  39 + cacheRule: 'pathname',
34 cache: true 40 cache: true
35 }, 41 },
36 ]; 42 ];
@@ -40,7 +46,7 @@ let renderer; @@ -40,7 +46,7 @@ let renderer;
40 let template = fs.readFileSync(path.join(__dirname, '../../src/index.html'), 'utf-8'); 46 let template = fs.readFileSync(path.join(__dirname, '../../src/index.html'), 'utf-8');
41 47
42 const microCache = LRU({ // eslint-disable-line 48 const microCache = LRU({ // eslint-disable-line
43 - max: 10000, 49 + max: 1000,
44 maxAge: 2000 50 maxAge: 2000
45 }); 51 });
46 52
@@ -60,10 +66,12 @@ const getContext = (req) => { @@ -60,10 +66,12 @@ const getContext = (req) => {
60 }; 66 };
61 }; 67 };
62 68
63 -const render = (options) => { 69 +const render = ({cache, cacheRule}) => {
64 return (req, res, next) => { 70 return (req, res, next) => {
65 - if (options.cache) {  
66 - const html = microCache.get(req.url); 71 + const reqUrl = url.parse(req.url);
  72 +
  73 + if (cache && reqUrl[cacheRule]) {
  74 + const html = microCache.get(reqUrl[cacheRule]);
67 75
68 if (html) { 76 if (html) {
69 console.log('cache', req.url); 77 console.log('cache', req.url);
@@ -77,8 +85,8 @@ const render = (options) => { @@ -77,8 +85,8 @@ const render = (options) => {
77 // TODO 处理错误类型 85 // TODO 处理错误类型
78 return next(err); 86 return next(err);
79 } 87 }
80 - if (options.cache) {  
81 - microCache.set(req.url, html); 88 + if (cache && reqUrl[cacheRule]) {
  89 + microCache.set(reqUrl[cacheRule], html);
82 } 90 }
83 return res.send(html); 91 return res.send(html);
84 }); 92 });
@@ -154,9 +162,7 @@ module.exports = async (app) => { @@ -154,9 +162,7 @@ module.exports = async (app) => {
154 await loadBundle(); 162 await loadBundle();
155 _.each(routes, r => { 163 _.each(routes, r => {
156 if (!r.disable) { 164 if (!r.disable) {
157 - app.get(r.route, ssrRender({  
158 - cache: r.cache  
159 - })); 165 + app.get(r.route, ssrRender(r));
160 } 166 }
161 }); 167 });
162 }; 168 };