Authored by 李奇

replace memcached session store with redis

... ... @@ -81,13 +81,6 @@ module.exports = {
},
useOneapm: false,
useCache: true,
memcache: {
master: ['127.0.0.1:11211'],
slave: ['127.0.0.1:11211'],
session: ['127.0.0.1:11211'],
timeout: 1000,
retries: 0
},
interfaceShunt: {
open: false
},
... ... @@ -152,8 +145,8 @@ module.exports = {
apiCache: {
cache: true
},
sessionRedisPrefix: 'yohobuy_session:',
zookeeperServer: '192.168.102.168:2188',
sessionMemcachedPrefix: 'yohobuy_session:',
redis: {
connect: {
host: '192.168.102.49',
... ... @@ -204,19 +197,6 @@ if (isProduction) {
unionApi: 'http://union.yoho.cn/',
yohoNowApi: 'http://new.yohoboys.com/',
},
memcache: {
master: ['memcache1.yohoops.org:12111', 'memcache2.yohoops.org:12111',
'memcache3.yohoops.org:12111', 'memcache4.yohoops.org:12111'],
slave: ['memcache1.yohoops.org:12112', 'memcache2.yohoops.org:12112',
'memcache3.yohoops.org:12112', 'memcache4.yohoops.org:12112'],
session: ['memcache1.yohoops.org:12111', 'memcache2.yohoops.org:12111',
'memcache3.yohoops.org:12111', 'memcache4.yohoops.org:12111'],
poolSize: 100,
reconnect: 5000,
timeout: 300,
retries: 0,
retry: 3000
},
useOneapm: true,
useCache: true,
interfaceShunt: {
... ... @@ -265,13 +245,6 @@ if (isProduction) {
},
useOneapm: true,
useCache: true,
memcache: {
master: ['127.0.0.1:12111', '127.0.0.1:12111', '127.0.0.1:12111'],
slave: ['127.0.0.1:12112', '127.0.0.1:12112', '127.0.0.1:12112'],
session: ['120.0.0.1:12111', '127.0.0.1:12111', '127.0.0.1:12111'],
timeout: 1000,
retries: 0
},
redis: {
connect: {
host: '192.168.104.32',
... ...
const config = global.yoho.config;
const memcachedSession = require('yoho-express-session');
const _ = require('lodash');
const uuid = require('uuid');
const config = global.yoho.config;
const session = require('yoho-express-session');
const cookieSession = require('client-sessions');
const memcached = require('connect-memcached');
const MemcachedStore = memcached(memcachedSession);
const redis = require('connect-redis');
const RedisStore = redis(session);
/**
* 该中间件主要把 express-session 和 client-session 集中起来处理,如果 memcached 出错了,使用 cookie session
* 该中间件主要把 express-session 和 client-session 集中起来处理,如果 redis 出错了,使用 cookie session
* @param opts.backSession cookieSession 的键名
* @returns {function(*=, *=, *)}
*/
function yohoSession(opts) {
return (req, res, next) => {
let notUseMemcached = _.get(req.app.locals.pc, 'session.removeMemcached', false);
let notUseRedis = _.get(req.app.locals.pc, 'session.removeRedis', false);
opts.backSession = opts.backSession || 'session2';
if (req.session && !notUseMemcached) {
if (req.session && !notUseRedis) {
req.sessionError = false;
} else {
// 重建 session
... ... @@ -26,8 +24,8 @@ function yohoSession(opts) {
req.sessionError = true;
req.sessionID = req.sessionID || uuid.v4();
req.session = new memcachedSession.Session(req, req[opts.backSession].sessionBack);
req.session.cookie = new memcachedSession.Cookie({
req.session = new session.Session(req, req[opts.backSession].sessionBack);
req.session.cookie = new session.Cookie({
domain: config.cookieDomain,
httpOnly: false
});
... ... @@ -51,7 +49,7 @@ function yohoSession(opts) {
}
module.exports = (app) => {
app.use(memcachedSession({ // eslint-disable-line
app.use(session({ // eslint-disable-line
proxy: true,
resave: false,
saveUninitialized: true,
... ... @@ -65,12 +63,10 @@ module.exports = (app) => {
getid() {
return uuid.v4();
},
store: new MemcachedStore({
hosts: config.memcache.session,
prefix: config.sessionMemcachedPrefix,
reconnect: 5000,
timeout: 1000,
retries: 0
store: new RedisStore({
port: config.redis.port,
host: config.redis.connect.host,
prefix: config.sessionRedisPrefix
})
}));
... ...
... ... @@ -28,8 +28,8 @@
"client-sessions": "^0.7.0",
"clipboard": "^1.7.1",
"compression": "^1.6.2",
"connect-memcached": "^0.2.0",
"connect-multiparty": "^2.0.0",
"connect-redis": "^3.3.3",
"cookie-parser": "^1.4.3",
"csurf": "^1.9.0",
"dnscache": "^1.0.1",
... ... @@ -57,7 +57,7 @@
"urlencode": "^1.1.0",
"uuid": "^2.0.2",
"yoho-express-session": "^2.0.0",
"yoho-node-lib": "=0.6.3",
"yoho-node-lib": "=0.6.5",
"yoho-zookeeper": "^1.0.8"
},
"devDependencies": {
... ...
... ... @@ -1623,12 +1623,6 @@ connect-history-api-fallback@^1.3.0:
version "1.5.0"
resolved "http://npm.yohops.com/connect-history-api-fallback/-/connect-history-api-fallback-1.5.0.tgz#b06873934bc5e344fef611a196a6faae0aee015a"
connect-memcached@^0.2.0:
version "0.2.0"
resolved "http://npm.yohops.com/connect-memcached/-/connect-memcached-0.2.0.tgz#64907180324e117d9e8f299c994c0cb03d5020cf"
dependencies:
memcached "2.2.x"
connect-multiparty@^2.0.0:
version "2.1.0"
resolved "http://npm.yohops.com/connect-multiparty/-/connect-multiparty-2.1.0.tgz#b562232ea638e13222d87e7b67be437f7ad89814"
... ... @@ -1638,9 +1632,12 @@ connect-multiparty@^2.0.0:
qs "~6.5.1"
type-is "~1.6.15"
connection-parse@0.0.x:
version "0.0.7"
resolved "http://npm.yohops.com/connection-parse/-/connection-parse-0.0.7.tgz#18e7318aab06a699267372b10c5226d25a1c9a69"
connect-redis@^3.3.3:
version "3.3.3"
resolved "http://npm.yohops.com/connect-redis/-/connect-redis-3.3.3.tgz#0fb8f370192f62da75ec7a9507807599fbe15b37"
dependencies:
debug "^3.1.0"
redis "^2.1.0"
console-browserify@^1.1.0:
version "1.1.0"
... ... @@ -3390,13 +3387,6 @@ hasha@^2.2.0:
is-stream "^1.0.1"
pinkie-promise "^2.0.0"
hashring@3.2.x:
version "3.2.0"
resolved "http://npm.yohops.com/hashring/-/hashring-3.2.0.tgz#fda4efde8aa22cdb97fb1d2a65e88401e1c144ce"
dependencies:
connection-parse "0.0.x"
simple-lru-cache "0.0.x"
hawk@3.1.3, hawk@~3.1.3:
version "3.1.3"
resolved "http://npm.yohops.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4"
... ... @@ -4015,12 +4005,6 @@ isstream@0.1.x, isstream@~0.1.2:
version "0.1.2"
resolved "http://npm.yohops.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
jackpot@>=0.0.6:
version "0.0.6"
resolved "http://npm.yohops.com/jackpot/-/jackpot-0.0.6.tgz#3cff064285cbf66f4eab2593c90bce816a821849"
dependencies:
retry "0.6.0"
jpeg-js@0.0.4:
version "0.0.4"
resolved "http://npm.yohops.com/jpeg-js/-/jpeg-js-0.0.4.tgz#06aaf47efec7af0b1924a59cd695a6d2b5ed870e"
... ... @@ -4641,13 +4625,6 @@ mem@^1.1.0:
dependencies:
mimic-fn "^1.0.0"
memcached@2.2.x, memcached@^2.2.2:
version "2.2.2"
resolved "http://npm.yohops.com/memcached/-/memcached-2.2.2.tgz#68f86ccfd84bcf93cc25ed46d6d7fc0c7521c9d5"
dependencies:
hashring "3.2.x"
jackpot ">=0.0.6"
memory-fs@^0.4.0, memory-fs@~0.4.1:
version "0.4.1"
resolved "http://npm.yohops.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"
... ... @@ -6644,7 +6621,7 @@ redis-parser@^2.6.0:
version "2.6.0"
resolved "http://npm.yohops.com/redis-parser/-/redis-parser-2.6.0.tgz#52ed09dacac108f1a631c07e9b69941e7a19504b"
redis@^2.7.1:
redis@^2.1.0, redis@^2.7.1, redis@^2.8.0:
version "2.8.0"
resolved "http://npm.yohops.com/redis/-/redis-2.8.0.tgz#202288e3f58c49f6079d97af7a10e1303ae14b02"
dependencies:
... ... @@ -6948,10 +6925,6 @@ ret@~0.1.10:
version "0.1.15"
resolved "http://npm.yohops.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
retry@0.6.0:
version "0.6.0"
resolved "http://npm.yohops.com/retry/-/retry-0.6.0.tgz#1c010713279a6fd1e8def28af0c3ff1871caa537"
rewire@^2.5.1:
version "2.5.2"
resolved "http://npm.yohops.com/rewire/-/rewire-2.5.2.tgz#6427de7b7feefa7d36401507eb64a5385bc58dc7"
... ... @@ -7189,10 +7162,6 @@ signal-exit@^3.0.0:
version "3.0.2"
resolved "http://npm.yohops.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
simple-lru-cache@0.0.x:
version "0.0.2"
resolved "http://npm.yohops.com/simple-lru-cache/-/simple-lru-cache-0.0.2.tgz#d59cc3a193c1a5d0320f84ee732f6e4713e511dd"
sitemap@^1.12.1:
version "1.13.0"
resolved "http://npm.yohops.com/sitemap/-/sitemap-1.13.0.tgz#569cbe2180202926a62a266cd3de09c9ceb43f83"
... ... @@ -8701,9 +8670,9 @@ yoho-jquery@^1.12.4:
version "1.12.4"
resolved "http://npm.yohops.com/yoho-jquery/-/yoho-jquery-1.12.4.tgz#22499b325f293ee8b1d60559777348156494926d"
yoho-node-lib@=0.6.3:
version "0.6.3"
resolved "http://npm.yohops.com/yoho-node-lib/-/yoho-node-lib-0.6.3.tgz#d17a18a07127a961169746fe4391483542591f9b"
yoho-node-lib@=0.6.5:
version "0.6.5"
resolved "http://npm.yohops.com/yoho-node-lib/-/yoho-node-lib-0.6.5.tgz#ec9a4fefb03a1453316c085507595dbf2d50ebcc"
dependencies:
dnscache "^1.0.1"
handlebars "^4.0.5"
... ... @@ -8711,9 +8680,9 @@ yoho-node-lib@=0.6.3:
lodash "^4.13.1"
lru-cache "^4.1.1"
md5 "^2.1.0"
memcached "^2.2.2"
moment "^2.13.0"
pidusage "^1.1.6"
redis "^2.8.0"
request "^2.81.0"
uuid "^3.0.1"
walk "^2.3.9"
... ...