Skip to content

Commit 2103dcc

Browse files
committed
disabled zremrangebyrank by default
1 parent af76ed4 commit 2103dcc

File tree

3 files changed

+300
-9
lines changed

3 files changed

+300
-9
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ limit.get(function(err, limit){
7575
- `db` - redis connection instance
7676
- `max` - max requests within `duration` [2500]
7777
- `duration` - of limit in milliseconds [3600000]
78+
- `tidy` - limit the records count, no greater than `max` [false]
7879

7980
# License
8081

index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = Limiter;
2424
function Limiter(opts) {
2525
this.id = opts.id;
2626
this.db = opts.db;
27+
this.tidy = opts.tidy || false;
2728
assert(this.id, '.id required');
2829
assert(this.db, '.db required');
2930
this.max = opts.max || 2500;
@@ -58,20 +59,24 @@ Limiter.prototype.inspect = function() {
5859

5960
Limiter.prototype.get = function (fn) {
6061
var db = this.db;
62+
var tidy = this.key;
6163
var duration = this.duration;
6264
var key = this.key;
6365
var max = this.max;
6466
var now = microtime.now();
6567
var start = now - duration * 1000;
66-
67-
db.multi()
68-
.zremrangebyscore([key, 0, start])
69-
.zcard([key])
70-
.zadd([key, now, now])
71-
.zrange([key, 0, 0])
72-
.zrange([key, -max, -max])
73-
.zremrangebyrank([key, 0, -(max + 1)])
74-
.pexpire([key, duration])
68+
var operations = [
69+
['zremrangebyscore', key, 0, start],
70+
['zcard', key],
71+
['zadd', key, now, now],
72+
['zrange', key, 0, 0],
73+
['zrange', key, -max, -max],
74+
['pexpire', key, duration],
75+
]
76+
if (tidy) {
77+
operations.splice(5, 0, ['zremrangebyrank', key, 0, -(max + 1)])
78+
}
79+
db.multi(operations)
7580
.exec(function (err, res) {
7681
if (err) return fn(err);
7782

0 commit comments

Comments
 (0)