Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmem.l5): use instances cache instead of poolsize #173

Merged
merged 1 commit into from
Jul 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions bin/tsw/pool/cmem.l5.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ if (!cache) {

module.exports = function(opt) {
/**
* 这里像这样写的目的主要是为了进行测试
因为在使用sinon.js时, 如果你exports的是一个function,你就无法进行stub,
*/
* 这里像这样写的目的主要是为了进行测试
因为在使用sinon.js时, 如果你exports的是一个function,你就无法进行stub,
*/
return module.exports.getCmem(opt);
};

Expand Down Expand Up @@ -57,14 +57,29 @@ module.exports.getCmem = function(opt) {
return null;
}

return fromCache(opt);
};

const fromCache = (opt) => {
const key = [opt.modid, opt.cmd, opt.host].join(':');

if (!cache[key]) {
const Memcached = require('memcached');
cache[key] = queueWrap(new Memcached(opt.host, opt));
const poolSize = opt.poolSize || 1;
const queueWrapList = [];
const option = Object.assign({}, opt, {
poolSize: 1
});
for (let i = 0; i < poolSize; i++) {
queueWrapList.push(queueWrap(new Memcached(opt.host, option)));
}
queueWrapList.curr = 0;
cache[key] = queueWrapList;
} else {
cache[key].curr = (cache[key].curr + 1) % cache[key].length;
}

return cache[key];
return cache[key][cache[key].curr];
};


Expand Down