feat: add cache client

This commit is contained in:
Ricardo Arturo Cabral Mejía 2022-11-14 23:01:15 -05:00
parent 42083a2f98
commit bd79c938eb
2 changed files with 31 additions and 0 deletions

15
src/cache/client.ts vendored Normal file
View File

@ -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
}

View File

@ -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
}