From 1a85c2571690ba592ac5183d5eadaf9846fe532b Mon Sep 17 00:00:00 2001 From: Factiven Date: Mon, 25 Sep 2023 00:44:40 +0700 Subject: Update v4.1.0 (#79) * Update v4.1.0 * Update pages/_app.js --- lib/redis.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'lib/redis.js') 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 }; -- cgit v1.2.3