3
3
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services' ;
4
4
import { Logger } from 'pino' ;
5
5
import { Registry } from 'prom-client' ;
6
- import type { RedisClientType } from 'redis' ;
6
+ import { createClient } from 'redis' ;
7
7
8
8
import { Utils } from '../../../utils' ;
9
9
import { RedisCacheError } from '../../errors/RedisCacheError' ;
@@ -39,47 +39,20 @@ export class RedisCache implements IRedisCacheClient {
39
39
* The Redis client.
40
40
* @private
41
41
*/
42
- private readonly client : RedisClientType ;
43
-
44
- /**
45
- * Boolean showing if the connection to the Redis client has finished.
46
- * @private
47
- */
48
- private connected : Promise < boolean > ;
42
+ private readonly client : ReturnType < typeof createClient > ;
49
43
50
44
/**
51
45
* Creates an instance of `RedisCache`.
52
46
*
53
47
* @param {Logger } logger - The logger instance.
54
48
* @param {Registry } register - The metrics registry.
55
49
*/
56
- public constructor ( logger : Logger , register : Registry , client : RedisClientType ) {
50
+ public constructor ( logger : Logger , register : Registry , client : ReturnType < typeof createClient > ) {
57
51
this . logger = logger ;
58
52
this . register = register ;
59
53
this . client = client ;
60
- this . connected = this . client
61
- . connect ( )
62
- . then ( ( ) => true )
63
- . catch ( ( error ) => {
64
- this . logger . error ( error , 'Redis connection could not be established!' ) ;
65
- return false ;
66
- } ) ;
67
- this . client . on ( 'ready' , async ( ) => {
68
- this . connected = Promise . resolve ( true ) ;
69
- const connections = await this . getNumberOfConnections ( ) . catch ( ( error ) => {
70
- this . logger . error ( error ) ;
71
- return 0 ;
72
- } ) ;
73
- logger . info (
74
- `Connected to Redis server (${ ConfigService . get ( 'REDIS_URL' ) } ) successfully! Number of connections: ${ connections } ` ,
75
- ) ;
76
- } ) ;
77
- this . client . on ( 'end' , ( ) => {
78
- this . connected = Promise . resolve ( false ) ;
79
- logger . info ( 'Disconnected from Redis server!' ) ;
80
- } ) ;
54
+
81
55
this . client . on ( 'error' , ( error ) => {
82
- this . connected = Promise . resolve ( false ) ;
83
56
const redisError = new RedisCacheError ( error ) ;
84
57
if ( redisError . isSocketClosed ( ) ) {
85
58
logger . error ( `Error occurred with Redis Connection when closing socket: ${ redisError . message } ` ) ;
@@ -89,8 +62,8 @@ export class RedisCache implements IRedisCacheClient {
89
62
} ) ;
90
63
}
91
64
92
- async getConnectedClient ( ) : Promise < RedisClientType > {
93
- return this . isConnected ( ) . then ( ( ) => this . client ) ;
65
+ async getConnectedClient ( ) : Promise < ReturnType < typeof createClient > > {
66
+ return this . client . isConnected ( ) . then ( ( ) => this . client ) ;
94
67
}
95
68
96
69
/**
0 commit comments