1
1
import { createPool , Pool } from "generic-pool" ;
2
2
import { Redis } from "ioredis" ;
3
3
import { logger } from "./util" ;
4
- import { gzipSync , gunzipSync } from ' zlib' ;
4
+ import { gzipSync , gunzipSync } from " zlib" ;
5
5
6
6
let globalRedisCachePromise : Promise < RedisCacheService > | null = null ;
7
7
@@ -45,7 +45,7 @@ const DEFAULT_REDIS_PORT = 6379;
45
45
const DEFAULT_REDIS_PASSWORD = "MDikUKnhRHlURlnORexvVztDTrNCUBze" ;
46
46
47
47
export class RedisCacheService {
48
- constructor ( public redisPool : RedisPool ) { }
48
+ constructor ( public redisPool : RedisPool ) { }
49
49
50
50
async isPoolReady ( ) : Promise < boolean > {
51
51
try {
@@ -78,7 +78,7 @@ export class RedisCacheService {
78
78
}
79
79
80
80
getKey ( key : string ) {
81
- if ( ! key ) throw new Error ( ' Redis key must be a non-empty string' ) ;
81
+ if ( ! key ) throw new Error ( " Redis key must be a non-empty string" ) ;
82
82
const prefix = `${ process . env . NETWORK } :` ;
83
83
if ( key . startsWith ( prefix ) ) {
84
84
return key ;
@@ -201,20 +201,21 @@ export class RedisCacheService {
201
201
const raw = Buffer . from ( JSON . stringify ( obj ) , "utf8" ) ;
202
202
const comp = gzipSync ( raw ) ;
203
203
204
- await this . redisPool . useClient ( async client => {
205
- await client . set ( this . getKey ( key ) , comp ) ;
204
+ await this . redisPool . useClient ( async ( client ) => {
206
205
if ( ttlInSeconds ) {
207
- await client . expire ( this . getKey ( key ) , ttlInSeconds ) ;
206
+ await client . set ( this . getKey ( key ) , comp , "EX" , ttlInSeconds ) ;
207
+ } else {
208
+ await client . set ( this . getKey ( key ) , comp ) ;
208
209
}
209
210
} ) ;
210
211
}
211
212
async getCompressed < T > ( key : string ) : Promise < T | null > {
212
- const buf : Buffer | null = await this . redisPool . useClient ( c =>
213
+ const buf : Buffer | null = await this . redisPool . useClient ( ( c ) =>
213
214
c . getBuffer ( this . getKey ( key ) )
214
215
) ;
215
216
if ( ! buf ) return null ;
216
217
217
- const json = gunzipSync ( buf ) . toString ( ' utf8' ) ;
218
+ const json = gunzipSync ( buf ) . toString ( " utf8" ) ;
218
219
return JSON . parse ( json ) as T ;
219
220
}
220
221
@@ -354,7 +355,6 @@ export class RedisPool {
354
355
max : options . max || 500 ,
355
356
min : options . min || 200 ,
356
357
idleTimeoutMillis : options . idleTimeoutMillis || 60_000 ,
357
-
358
358
} ;
359
359
360
360
logger . info (
@@ -368,7 +368,7 @@ export class RedisPool {
368
368
host : this . options . host ,
369
369
port : this . options . port ,
370
370
password : this . options . password || undefined , // Pass undefined if no password
371
- retryStrategy : attempts => Math . min ( attempts * 50 , 2000 ) ,
371
+ retryStrategy : ( attempts ) => Math . min ( attempts * 50 , 2000 ) ,
372
372
maxRetriesPerRequest : 3 ,
373
373
connectTimeout : 3000 ,
374
374
enableReadyCheck : true ,
0 commit comments