mirror of
https://github.com/Cameri/nostream.git
synced 2025-06-09 14:39:12 +02:00
feat: add cache client
This commit is contained in:
parent
42083a2f98
commit
bd79c938eb
15
src/cache/client.ts
vendored
Normal file
15
src/cache/client.ts
vendored
Normal 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
|
||||||
|
}
|
16
src/factories/rate-limiter-factory.ts
Normal file
16
src/factories/rate-limiter-factory.ts
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user