Skip to content

Commit

Permalink
feat: add cache client
Browse files Browse the repository at this point in the history
  • Loading branch information
cameri committed Nov 16, 2022
1 parent 42083a2 commit bd79c93
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cache/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createClient, RedisClientOptions } from 'redis'
import { Cache } from '../@types/cache'

export const getCacheConfig = (): RedisClientOptions => ({
url: `redis://${process.env.REDIS_USER}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
password: process.env.REDIS_PASSWORD,
})

export const getCacheClient = (): Cache => {
const config = getCacheConfig()

const client = createClient(config)

return client
}
16 changes: 16 additions & 0 deletions src/factories/rate-limiter-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getCacheClient } from '../cache/client'
import { ICacheAdapter } from '../@types/adapters'
import { IRateLimiter } from '../@types/utils'
import { RedisAdapter } from '../adapters/redis-adapter'
import { SlidingWindowRateLimiter } from '../utils/sliding-window-rate-limiter'

let instance: IRateLimiter = undefined

export const slidingWindowRateLimiterFactory = () => {
if (!instance) {
const cache: ICacheAdapter = new RedisAdapter(getCacheClient())
instance = new SlidingWindowRateLimiter(cache)
}

return instance
}

0 comments on commit bd79c93

Please sign in to comment.