@@ -24,6 +24,7 @@ module.exports = Limiter;
2424function 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
5960Limiter . 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