@ratelock/redis
Version:
Redis storage backend for RateLock rate limiting system
1 lines • 2.41 kB
JavaScript
const e=`local a=KEYS[1]local b=tonumber(ARGV[1])local c=tonumber(redis.call("GET",a)or"0")local d=math.max(b,c-1)if d<=b then redis.call("DEL",a)return tostring(b)else redis.call("SET",a,tostring(d))return tostring(d)end`,t=`local a=KEYS[1]local b=tonumber(ARGV[1])local c=tonumber(ARGV[2])local d=tonumber(ARGV[3])local e=math.floor(d/b)*b;redis.call("SET",a,0,"PX",b,"NX")local f=redis.call("INCR",a)local g=redis.call("PTTL",a)if g==-1 then redis.call("PEXPIRE",a,b)g=b end;local h=f<=c;local i=math.max(0,c-f)return{h and 1 or 0,f,i,g}`,n=`local a=KEYS[1]local b=tonumber(ARGV[1])local c=tonumber(ARGV[2])local d=tonumber(redis.call("GET",a)or"0")if d<b then local e=d+1;redis.call("SET",a,tostring(e))if d==0 and c>0 then redis.call("PEXPIRE",a,c)end;return{tostring(e),1}else return{tostring(d),0}end`,r=`local a=KEYS[1]local b=tonumber(ARGV[1])local c=redis.call("INCR",a)if c==1 and b>0 then redis.call("PEXPIRE",a,b)end;return tostring(c)`,i=`local a=KEYS[1]local b=KEYS[2]local c=tonumber(ARGV[1])local d=tonumber(ARGV[2])local e=tonumber(ARGV[3])local f=redis.call("GET",a)if not f then f=e;redis.call("SET",a,f,"PX",c)else f=tonumber(f)if e>=f+c then f=e;redis.call("SET",a,f,"PX",c)redis.call("DEL",b)end end;local g=f+c-e;if g<=0 then g=1 end;local h=redis.call("INCR",b)if h==1 then redis.call("PEXPIRE",b,g)end;local i=h<=d;if not i then redis.call("DECR",b)h=h-1 end;return{i and 1 or 0,h,math.max(0,d-h),f+c}`,a=`local a=KEYS[1]local b=tonumber(ARGV[1])local c=tonumber(ARGV[2])local d=tonumber(ARGV[3])local e=d-b;redis.call("ZREMRANGEBYSCORE",a,"-inf",e)local f=redis.call("ZCARD",a)local g=f<c;local h=math.max(0,c-f-(g and 1 or 0))if g then redis.call("ZADD",a,d,d)redis.call("PEXPIRE",a,b)end;local i=redis.call("PTTL",a)if i==-1 or i==-2 then i=b end;return{g and 1 or 0,f+(g and 1 or 0),h,i}`,o=`local a=KEYS[1]local b=tonumber(ARGV[1])local c=tonumber(ARGV[2])local d=tonumber(ARGV[3])local e=redis.call("HMGET",a,"tokens","last_refill")local f=tonumber(e[1])or b;local g=tonumber(e[2])or d;local h=(d-g)/1000;local i=h*c;f=math.min(b,f+i)local j=f>=1;if j then f=f-1 end;redis.call("HMSET",a,"tokens",f,"last_refill",d)redis.call("PEXPIRE",a,3600000)local k=0;if f<1 then k=math.ceil((1-f)/c*1000)end;return{j and 1 or 0,math.floor(f),k}`;export{e as DECREMENT,t as FIXED_WINDOW,r as INCREMENT,n as INCREMENT_IF,i as INDIVIDUAL_FIXED_WINDOW,a as SLIDING_WINDOW,o as TOKEN_BUCKET};