diff options
| author | Factiven <[email protected]> | 2023-09-25 00:44:40 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-25 00:44:40 +0700 |
| commit | 1a85c2571690ba592ac5183d5eadaf9846fe532b (patch) | |
| tree | 3f3552c00cd49c0eeab5275275cf5cf5666e5027 /lib/redis.js | |
| parent | Delete .github/workflows/deploy.yml (diff) | |
| download | moopa-4.1.0.tar.xz moopa-4.1.0.zip | |
Update v4.1.0 (#79)v4.1.0
* Update v4.1.0
* Update pages/_app.js
Diffstat (limited to 'lib/redis.js')
| -rw-r--r-- | lib/redis.js | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/redis.js b/lib/redis.js index ed8b8c5..713b5d9 100644 --- a/lib/redis.js +++ b/lib/redis.js @@ -1,13 +1,36 @@ import { Redis } from "ioredis"; +import { RateLimiterRedis } from "rate-limiter-flexible"; const REDIS_URL = process.env.REDIS_URL; let redis; +let rateLimiterRedis; +let rateLimitStrict; if (REDIS_URL) { redis = new Redis(REDIS_URL); + redis.on("error", (err) => { + console.error("Redis error: ", err); + }); + + const opt = { + storeClient: redis, + keyPrefix: "rateLimit", + points: 50, + duration: 1, + }; + + const optStrict = { + storeClient: redis, + keyPrefix: "rateLimitStrict", + points: 20, + duration: 1, + }; + + rateLimiterRedis = new RateLimiterRedis(opt); + rateLimitStrict = new RateLimiterRedis(optStrict); } else { console.warn("REDIS_URL is not defined. Redis caching will be disabled."); } -export default redis; +export { redis, rateLimiterRedis, rateLimitStrict }; |