Skip to content

Commit bd79c93

Browse files
committed
feat: add cache client
1 parent 42083a2 commit bd79c93

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/cache/client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createClient, RedisClientOptions } from 'redis'
2+
import { Cache } from '../@types/cache'
3+
4+
export const getCacheConfig = (): RedisClientOptions => ({
5+
url: `redis://${process.env.REDIS_USER}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
6+
password: process.env.REDIS_PASSWORD,
7+
})
8+
9+
export const getCacheClient = (): Cache => {
10+
const config = getCacheConfig()
11+
12+
const client = createClient(config)
13+
14+
return client
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getCacheClient } from '../cache/client'
2+
import { ICacheAdapter } from '../@types/adapters'
3+
import { IRateLimiter } from '../@types/utils'
4+
import { RedisAdapter } from '../adapters/redis-adapter'
5+
import { SlidingWindowRateLimiter } from '../utils/sliding-window-rate-limiter'
6+
7+
let instance: IRateLimiter = undefined
8+
9+
export const slidingWindowRateLimiterFactory = () => {
10+
if (!instance) {
11+
const cache: ICacheAdapter = new RedisAdapter(getCacheClient())
12+
instance = new SlidingWindowRateLimiter(cache)
13+
}
14+
15+
return instance
16+
}

0 commit comments

Comments
 (0)