@@ -11,58 +11,79 @@ export interface ICacheService {
1111export class CacheService implements ICacheService {
1212 readonly #env: Environment
1313 readonly #cacheType: string
14- #client: RedisClientType | KVNamespace
14+ #client: RedisClientType | KVNamespace | null = null
15+ #connecting = false
1516
1617 // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
1718 constructor ( env : Env ) {
1819 this . #env = env
1920 if ( this . #env. EFP_DATA_CACHE === undefined ) {
2021 this . #cacheType = 'redis'
21- this . #client = this . createRedisClient ( )
2222 } else {
2323 this . #cacheType = 'kv'
2424 this . #client = this . #env. EFP_DATA_CACHE as KVNamespace
2525 }
2626 }
2727
28- createRedisClient ( ) : RedisClientType {
28+ async createRedisClient ( ) : Promise < RedisClientType > {
29+ if ( this . #connecting) {
30+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
31+ return this . #client as RedisClientType
32+ }
33+
34+ this . #connecting = true
35+
2936 const client : RedisClientType = createClient ( {
3037 url : this . #env. REDIS_URL
3138 } )
39+
3240 client . on ( 'error' , ( err : Error ) => {
33- console . log ( `Error: ${ err } ` )
34- client . quit ( )
41+ console . error ( `Redis Error: ${ err . message } ` )
3542 } )
36- client . connect ( )
43+
44+ try {
45+ await client . connect ( )
46+ this . #client = client
47+ } catch ( err ) {
48+ console . error ( `Failed to connect to Redis: ${ err } ` )
49+ this . #client = null
50+ } finally {
51+ this . #connecting = false
52+ }
53+
3754 return client
3855 }
3956
40- closeClient ( ) : void {
41- ; ( this . #client as RedisClientType ) . quit ( )
57+ async getRedisClient ( ) : Promise < RedisClientType > {
58+ if ( ! this . #client) {
59+ return await this . createRedisClient ( )
60+ }
61+ return this . #client as RedisClientType
62+ }
63+
64+ async closeClient ( ) : Promise < void > {
65+ if ( this . #cacheType === 'redis' && this . #client) {
66+ await ( this . #client as RedisClientType ) . quit ( )
67+ this . #client = null
68+ }
4269 }
4370
4471 async get ( key : string ) : Promise < { } | null > {
4572 if ( this . #cacheType === 'redis' ) {
46- if ( ! this . #client) {
47- this . #client = this . createRedisClient ( )
48- }
49- const result = await ( this . #client as RedisClientType ) . get ( key )
50- return JSON . parse ( result as string ) as any
73+ const client = await this . getRedisClient ( )
74+ const result = await client . get ( key )
75+ return result ? ( JSON . parse ( result as string ) as { } ) : null
5176 }
5277 return this . #env. EFP_DATA_CACHE . get ( key , 'json' )
5378 }
5479
5580 async put ( key : string , value : string , ttl = this . #env. CACHE_TTL ) : Promise < void > {
5681 if ( this . #cacheType === 'redis' ) {
57- if ( ! this . #client) {
58- this . #client = this . createRedisClient ( )
59- }
82+ const client = await this . getRedisClient ( )
6083 if ( ttl === 0 ) {
61- await ( this . # client as RedisClientType ) . set ( key , value , { } as any )
84+ await client . set ( key , value , { } as any )
6285 } else {
63- await ( this . #client as RedisClientType ) . set ( key , value , {
64- EX : ttl
65- } as any )
86+ await client . set ( key , value , { EX : ttl } as any )
6687 }
6788 } else if ( ttl === 0 ) {
6889 await this . #env. EFP_DATA_CACHE . put ( key , value , { } )
0 commit comments