Skip to content

Commit 0fc5b24

Browse files
committed
update redis
1 parent 23d8e56 commit 0fc5b24

3 files changed

Lines changed: 68 additions & 38 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { RedisClientType } from '@redis/client';
2+
import { createClient } from '@redis/client';
3+
import { appInstance } from '../appInstance.ts';
4+
5+
let redisClient: RedisClientType | null = null;
6+
7+
export const getRedisClient = async (): Promise<RedisClientType> => {
8+
if (redisClient) {
9+
return redisClient;
10+
}
11+
const redisClientLocal = createNewRedisClient();
12+
await redisClientLocal.connect();
13+
14+
return redisClientLocal;
15+
};
16+
17+
/**
18+
* Synchronous way to get redis client. Make sure to call connect on it before usage
19+
*/
20+
export const getRedisClientSync = (): RedisClientType => {
21+
if (redisClient) {
22+
return redisClient;
23+
}
24+
const redisClientLocal = createNewRedisClient();
25+
// to make sure connection is established
26+
(async () => {
27+
await redisClientLocal.connect();
28+
})();
29+
30+
return redisClientLocal;
31+
};
32+
33+
const createNewRedisClient = (): RedisClientType => {
34+
if (redisClient) {
35+
return redisClient;
36+
}
37+
38+
const redisConfig = appInstance.getConfig('redis') as {
39+
url: string;
40+
};
41+
42+
redisClient = createClient({
43+
url: redisConfig.url,
44+
});
45+
46+
redisClient.on('error', (error, b, c) => {
47+
appInstance.logger?.error('Redis Client Error', error, b, c);
48+
});
49+
50+
redisClient.on('connect', () => {
51+
appInstance.logger?.info('Redis connection established');
52+
});
53+
54+
appInstance.events.on('shutdown', async () => {
55+
if (redisClient) {
56+
await redisClient.quit();
57+
redisClient = null;
58+
}
59+
});
60+
return redisClient;
61+
};

src/services/cache/Cache.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RedisClientType } from '@redis/client';
22
import type redisConfig from '../../config/redis.ts';
3+
import { getRedisClient } from '../../helpers/redis/redisConnection.ts';
34
import Base from '../../modules/Base.ts';
45
import type { IApp } from '../../server.ts';
56

@@ -22,23 +23,10 @@ class Cache extends Base {
2223
// at least memory and redis drivers should be presented
2324
// memory drives should works on master process level
2425
// we should support multiple cashe same time
25-
const { createClient } = await import('@redis/client');
26-
const conf = this.app.getConfig('redis') as typeof redisConfig;
27-
this.redisClient = createClient({
28-
url: conf.url,
29-
});
30-
31-
this.redisNamespace = conf.namespace;
32-
33-
this.redisClient.on('error', (error, b, c) => {
34-
this.logger?.error(error, b, c);
35-
});
36-
this.redisClient.on('connect', () => {
37-
this.logger?.info('Redis connection success');
38-
});
39-
this.app.events.on('shutdown', () => {
40-
this.redisClient.close();
41-
});
26+
const { namespace } = this.app.getConfig('redis') as typeof redisConfig;
27+
this.redisClient = await getRedisClient();
28+
29+
this.redisNamespace = namespace;
4230
}
4331

4432
/**

src/services/http/middleware/RateLimiter.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createClient } from '@redis/client';
21
import merge from 'deepmerge';
32
import type { NextFunction, Response } from 'express';
43
import mongoose from 'mongoose';
@@ -12,6 +11,7 @@ import {
1211
RateLimiterRedis,
1312
} from 'rate-limiter-flexible';
1413
import type rateLimiterConfig from '../../../config/rateLimiter.js';
14+
import { getRedisClientSync } from '../../../helpers/redis/redisConnection.ts';
1515
import type { IApp } from '../../../server.ts';
1616
import type { FrameworkRequest } from '../HttpServer.ts';
1717
import AbstractMiddleware from './AbstractMiddleware.ts';
@@ -59,26 +59,7 @@ class RateLimiter extends AbstractMiddleware {
5959
}
6060

6161
initRedisLimiter() {
62-
const redisConfig = this.app.getConfig('redis');
63-
const redisClient = createClient({
64-
url: redisConfig.url as string,
65-
});
66-
67-
// TODO: change it
68-
(async () => {
69-
await redisClient.connect();
70-
})();
71-
72-
redisClient.on('error', (error, b, c) => {
73-
this.logger?.error(error, b, c);
74-
});
75-
redisClient.on('connect', () => {
76-
this.logger?.info('Redis connection success');
77-
});
78-
79-
this.app.events.on('shutdown', async () => {
80-
await redisClient.close();
81-
});
62+
const redisClient = getRedisClientSync();
8263

8364
return new RateLimiterRedis({
8465
storeClient: redisClient,

0 commit comments

Comments
 (0)