Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 26 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ Limiter.prototype.get = function (fn) {
.set([reset, ex, 'PX', duration, 'NX'])
.exec(function (err, res) {
if (err) return fn(err);
// If the request has failed, it means the values already
// exist in which case we need to get the latest values.
if (!res || !res[0]) return mget();

// If the request has failed, it means the values already
// exist in which case we need to get the latest values.
if (isFirstReplyNull(res)) return mget();

fn(null, {
total: max,
Expand Down Expand Up @@ -105,7 +106,7 @@ Limiter.prototype.get = function (fn) {
.set([count, n - 1, 'PX', ex * 1000 - Date.now(), 'XX'])
.exec(function (err, res) {
if (err) return fn(err);
if (!res || !res[0]) return mget();
if (isFirstReplyNull(res)) return mget();
n = n - 1;
done();
});
Expand All @@ -125,3 +126,24 @@ Limiter.prototype.get = function (fn) {

mget();
};

/**
* Check whether the first item of multi replies is null,
* works with ioredis and node_redis
*
* @param {Array} replies
* @return {Boolean}
* @api private
*/

function isFirstReplyNull(replies) {
if (!replies) {
return true;
}

return Array.isArray(replies[0]) ?
// ioredis
!replies[0][1] :
// node_redis
!replies[0];
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
],
"dependencies": {},
"devDependencies": {
"ioredis": "^1.9.0",
"mocha": "*",
"should": "*",
"redis": "0.10.2"
"redis": "0.10.2",
"should": "*"
},
"license": "MIT",
"contributors": [
Expand Down
Loading