Authored by ccbikai

优化缓存使用

@@ -22,7 +22,8 @@ module.exports = { @@ -22,7 +22,8 @@ module.exports = {
22 master: ['192.168.102.161:11213'], 22 master: ['192.168.102.161:11213'],
23 slave: ['192.168.102.161:11213'], 23 slave: ['192.168.102.161:11213'],
24 session: ['192.168.102.161:11213'], 24 session: ['192.168.102.161:11213'],
25 - timeout: 3000 25 + timeout: 1000,
  26 + retries: 0
26 }, 27 },
27 loggers: { 28 loggers: {
28 infoFile: { 29 infoFile: {
@@ -53,10 +53,13 @@ class Http { @@ -53,10 +53,13 @@ class Http {
53 // 写缓存, 否则返回 Slave 缓存服务器的数据 53 // 写缓存, 否则返回 Slave 缓存服务器的数据
54 if (config.useCache && cacheOption) { 54 if (config.useCache && cacheOption) {
55 const cacheTime = _.isNumber(cacheOption) ? cacheOption : 60; 55 const cacheTime = _.isNumber(cacheOption) ? cacheOption : 60;
  56 + const catchErr = (err) => {
  57 + log.error(`cache: ${err.toString()}`);
  58 + };
56 59
57 reqId = reqId || this._getReqId(options); 60 reqId = reqId || this._getReqId(options);
58 - cache.set(`apiCache:${reqId}`, result, cacheTime);  
59 - cache.setSlave(`apiCache:${reqId}`, result, 86400); // 二级缓存存储一天 61 + cache.set(`apiCache:${reqId}`, result, cacheTime).catch(catchErr);
  62 + cache.setSlave(`apiCache:${reqId}`, result, 86400).catch(catchErr); // 二级缓存存储一天
60 } 63 }
61 64
62 log.info(`get api success: use: ${duration}ms`); 65 log.info(`get api success: use: ${duration}ms`);
@@ -8,7 +8,6 @@ @@ -8,7 +8,6 @@
8 const Promise = require('bluebird'); 8 const Promise = require('bluebird');
9 const Memcached = require('memcached'); 9 const Memcached = require('memcached');
10 const _ = require('lodash'); 10 const _ = require('lodash');
11 -const log = require('./logger');  
12 const config = require('../config/common'); 11 const config = require('../config/common');
13 12
14 let master = new Memcached(config.memcache.master, config.memcache); 13 let master = new Memcached(config.memcache.master, config.memcache);
@@ -76,7 +75,6 @@ exports.getMultiFromSlave = (list) => { @@ -76,7 +75,6 @@ exports.getMultiFromSlave = (list) => {
76 exports.set = (key, value, lifetime) => { 75 exports.set = (key, value, lifetime) => {
77 lifetime = lifetime || 86400; 76 lifetime = lifetime || 86400;
78 77
79 - log.info(`write cache: ${key}`);  
80 if (_.isObject(value)) { 78 if (_.isObject(value)) {
81 value = JSON.stringify(value); 79 value = JSON.stringify(value);
82 } 80 }