1- import Redis from "ioredis " ;
1+ import type { RedisClient as BunRedisClient } from "bun " ;
22import { logger } from "@utils" ;
33import { RedisClient } from "@database" ;
44
55class Cache {
6- private static redis : Redis | null = null ;
7-
8- private static getRedisClient ( ) : Redis {
9- if ( ! this . redis ) {
10- this . redis = RedisClient . getRedisClient ( ) ;
11- }
12-
13- return this . redis ;
6+ private static getClient ( ) : BunRedisClient {
7+ return RedisClient . getRedisClient ( ) ;
148 }
159
1610 static async get < T > ( key : string ) : Promise < T | null > {
1711 try {
18- const client = this . getRedisClient ( ) ;
19- const value = await client . get ( key ) ;
12+ const value = await this . getClient ( ) . get ( key ) ;
2013 return value ? ( JSON . parse ( value ) as T ) : null ;
2114 } catch ( error ) {
2215 logger . error ( error , `Error getting cache for key ${ key } :` ) ;
@@ -30,36 +23,37 @@ class Cache {
3023 ttl : number = 3600 ,
3124 ) : Promise < void > {
3225 try {
33- const client = this . getRedisClient ( ) ;
34- await client . set ( key , JSON . stringify ( value ) , "EX" , ttl ) ;
26+ await this . getClient ( ) . send ( "SET" , [
27+ key ,
28+ JSON . stringify ( value ) ,
29+ "EX" ,
30+ String ( ttl ) ,
31+ ] ) ;
3532 } catch ( error ) {
3633 logger . error ( error , `Error setting cache for key ${ key } :` ) ;
3734 }
3835 }
3936
4037 static async delete ( key : string ) : Promise < void > {
4138 try {
42- const client = this . getRedisClient ( ) ;
43- await client . del ( key ) ;
39+ await this . getClient ( ) . del ( key ) ;
4440 } catch ( error ) {
4541 logger . error ( error , `Error deleting cache for key ${ key } :` ) ;
4642 }
4743 }
4844
4945 static async flush ( ) : Promise < void > {
5046 try {
51- const client = this . getRedisClient ( ) ;
52- await client . flushdb ( ) ;
47+ await this . getClient ( ) . send ( "FLUSHDB" , [ ] ) ;
5348 } catch ( error ) {
5449 logger . error ( error , "Error flushing Redis cache:" ) ;
5550 }
5651 }
5752
5853 static async exists ( key : string ) : Promise < boolean > {
5954 try {
60- const client = this . getRedisClient ( ) ;
61- const exists = await client . exists ( key ) ;
62- return exists === 1 ;
55+ const exists = await this . getClient ( ) . exists ( key ) ;
56+ return exists === true ;
6357 } catch ( error ) {
6458 logger . error ( error , `Error checking existence of key ${ key } :` ) ;
6559 return false ;
@@ -87,21 +81,17 @@ class Cache {
8781
8882 static async getKeys ( pattern : string ) : Promise < string [ ] > {
8983 try {
90- const client = this . getRedisClient ( ) ;
91- const keys = await client . keys ( pattern ) ;
84+ const keys = await this . getClient ( ) . keys ( pattern ) ;
9285 return keys ;
9386 } catch ( error ) {
9487 logger . error ( error , `Error getting keys with pattern ${ pattern } :` ) ;
9588 return [ ] ;
9689 }
9790 }
9891
99- static async disconnect ( ) : Promise < void > {
92+ static disconnect ( ) : void {
10093 try {
101- if ( this . redis ) {
102- await this . redis . quit ( ) ;
103- this . redis = null ;
104- }
94+ this . getClient ( ) . close ( ) ;
10595 } catch ( error ) {
10696 logger . error ( error , "Error disconnecting from Redis:" ) ;
10797 }
0 commit comments