Skip to content

Commit cf156be

Browse files
authored
Merge pull request #8 from Mindtraveller/master
Added support for decrement method (see https://github.com/nfriedly/e…
2 parents 4f5779b + 8ce18b1 commit cf156be

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.idea

lib/redis-store.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ var RedisStore = function(options) {
1313
// create the client if one isn't provided
1414
options.client = options.client || redis.createClient();
1515

16+
var setExpire = function(replies, rdskey) {
17+
// if this is new or has no expiry
18+
if (replies[0] === 1 || replies[1] === -1) {
19+
// then expire it after the timeout
20+
options.client.pexpire(rdskey, expiryMs);
21+
}
22+
};
23+
24+
var processReplies = function(replies) {
25+
// in ioredis, every reply consists of an array [err, value].
26+
// We don't need the error here, and if we aren't dealing with an array,
27+
// nothing is changed.
28+
return replies.map(function(val) {
29+
if (Array.isArray(val) && val.length >= 2) {
30+
return val[1];
31+
}
32+
33+
return val;
34+
});
35+
};
36+
1637
this.incr = function(key, cb) {
1738
var rdskey = options.prefix + key;
1839

@@ -24,27 +45,29 @@ var RedisStore = function(options) {
2445
return cb(err);
2546
}
2647

27-
// in ioredis, every reply consists of an array [err, value].
28-
// We don't need the error here, and if we aren't dealing with an array,
29-
// nothing is changed.
30-
replies = replies.map(function(val) {
31-
if (Array.isArray(val) && val.length >= 2) {
32-
return val[1];
33-
}
34-
35-
return val;
36-
});
37-
38-
// if this is new or has no expiry
39-
if (replies[0] === 1 || replies[1] === -1) {
40-
// then expire it after the timeout
41-
options.client.pexpire(rdskey, expiryMs);
42-
}
48+
replies = processReplies(replies);
49+
setExpire(replies, rdskey);
4350

4451
cb(null, replies[0]);
4552
});
4653
};
4754

55+
this.decrement = function(key) {
56+
var rdskey = options.prefix + key;
57+
58+
options.client.multi()
59+
.decr(rdskey)
60+
.pttl(rdskey)
61+
.exec(function(err, replies) {
62+
if (err) {
63+
return;
64+
}
65+
66+
replies = processReplies(replies);
67+
setExpire(replies, rdskey);
68+
});
69+
};
70+
4871
this.resetKey = function(key) {
4972
var rdskey = options.prefix + key;
5073

test/test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ describe("rate-limit-redis node module", function() {
2323
return this;
2424
};
2525

26+
this.decr = function(key) {
27+
opts.push(function() {
28+
if (keys[key]) {
29+
keys[key].value--;
30+
} else {
31+
keys[key] = { value: 0 };
32+
}
33+
34+
return keys[key].value;
35+
});
36+
37+
return this;
38+
};
39+
2640
this.pttl = function(key) {
2741
opts.push(function() {
2842
if (keys[key] && keys[key].ttl) {
@@ -115,6 +129,28 @@ describe("rate-limit-redis node module", function() {
115129
});
116130
});
117131

132+
it("decrements the key for the store each decrement", function(done) {
133+
var store = new RedisStore({
134+
client: new MockRedisClient()
135+
});
136+
var key = "test-store-decrement";
137+
138+
store.incr(key, function() {
139+
store.decrement(key);
140+
store.incr(key, function(err, value) {
141+
if (err) {
142+
done(err);
143+
} else {
144+
if (value === 1) {
145+
done();
146+
} else {
147+
done(new Error("decrement did not decrement the store"));
148+
}
149+
}
150+
});
151+
});
152+
});
153+
118154
it("resets the key for the store when used with resetKey", function(done) {
119155
var store = new RedisStore({
120156
client: new MockRedisClient()

0 commit comments

Comments
 (0)