rate-limit-redis 라이브러리는 express-rate-limit
의 memoryStore에 사용된다.
해당 라이브러리 코드를 보면 sendCommand
함수를 많이 사용하는데
sendCommand
는 store의 메서드이다.redisClient.sendCommand(command: string, args?: any[], callback?: Function): boolean;
sql의 프로시저와 비슷함.
const result = await this.sendCommand(
"SCRIPT",
"LOAD",
`
local totalHits = redis.call("INCR", KEYS[1])
local timeToExpire = redis.call("PTTL", KEYS[1])
if timeToExpire <= 0 or ARGV[1] == "1"
then
redis.call("PEXPIRE", KEYS[1], tonumber(ARGV[2]))
timeToExpire = tonumber(ARGV[2])
end
return { totalHits, timeToExpire }
`
// Ensure that code changes that affect whitespace do not affect
// the script contents.
.replace(/^\s+/gm, "")
.trim()
);
위 처럼 load가 완료 후
EVALSHA와 반환된 sha를 사용하면된다.
const results = await this.sendCommand(
"EVALSHA",
await this.loadedScriptSha1, // 여기가 위 로드된 sha
"1",
this.prefixKey(key),
this.resetExpiryOnChange ? "1" : "0",
this.windowMs.toString()
);
EVALSHA와 LOAD SCRIPT는 엔터프라이즈 2.6.0 이후 사용가능하다.
ARGV1은 lua 스크립트를 실행할 때 전달되는 두 번째 인자
위 코드에서는 this.prefixKey(key),
를 의미한다.