@@ -45,13 +45,21 @@ export class RedisCache implements ICache {
45
45
return this . redisClient . disconnect ( ) ;
46
46
}
47
47
48
-
48
+ /**
49
+ * It sets the tags for the cache.
50
+ * @param {string[] } keys - A list of keys to be used as tags.
51
+ * @returns RedisCache.
52
+ */
49
53
public tags ( keys : string [ ] ) : RedisCache {
50
54
this . _tags = keys ;
51
55
return this ;
52
56
}
53
57
54
-
58
+ public enable ( enable : boolean ) : RedisCache {
59
+ this . _enable = enable ;
60
+ return this ;
61
+ }
62
+
55
63
/**
56
64
* If the key is not already in the cache, set it to the value and set the expiration time
57
65
* @param {string } key - The key to set.
@@ -60,7 +68,7 @@ export class RedisCache implements ICache {
60
68
* @returns The return value is a boolean indicating whether the operation was successful.
61
69
*/
62
70
async set ( key : string , value : any , ttl : number = this . _ttl ) : Promise < boolean > {
63
- if ( ! this . _enable ) return false ;
71
+ if ( ! this . _enable ) return false ;
64
72
65
73
try {
66
74
key = this . _setKeyPrefix ( key ) ;
@@ -69,7 +77,7 @@ export class RedisCache implements ICache {
69
77
NX : true ,
70
78
} ) ;
71
79
72
- if ( result !== 'OK' ) return false
80
+ if ( result !== 'OK' ) return false ;
73
81
74
82
if ( this . _tags . length ) {
75
83
await this . _addTags ( key ) ;
@@ -87,8 +95,8 @@ export class RedisCache implements ICache {
87
95
* @param {string } key - The key to get the value for.
88
96
* @returns A promise.
89
97
*/
90
- async get ( key : string ) {
91
- if ( ! this . _enable ) return null ;
98
+ async get ( key : string ) : Promise < any > {
99
+ if ( ! this . _enable ) return null ;
92
100
93
101
try {
94
102
key = this . _setKeyPrefix ( key ) ;
@@ -98,7 +106,6 @@ export class RedisCache implements ICache {
98
106
}
99
107
}
100
108
101
-
102
109
/**
103
110
* Set the value of the key if it doesn't exist, and keep it forever.
104
111
* @param {string } key - The key to set.
@@ -109,7 +116,6 @@ export class RedisCache implements ICache {
109
116
return this . set ( key , value , 0 ) ;
110
117
}
111
118
112
-
113
119
/**
114
120
* If the key is not in the cache, call the callback and store the result in the cache
115
121
* @param {string } key - The key to store the value under.
@@ -140,7 +146,7 @@ export class RedisCache implements ICache {
140
146
* @returns A boolean value.
141
147
*/
142
148
async has ( key : string ) : Promise < boolean > {
143
- if ( ! this . _enable ) return false ;
149
+ if ( ! this . _enable ) return false ;
144
150
145
151
try {
146
152
return Boolean ( await this . redisClient . exists ( this . _setKeyPrefix ( key ) ) ) ;
@@ -149,14 +155,13 @@ export class RedisCache implements ICache {
149
155
}
150
156
}
151
157
152
-
153
158
/**
154
159
* It deletes the key from the cache and removes the key from the tags.
155
160
* @param {string } key - The key to be deleted.
156
161
* @returns The result of the `del` command.
157
162
*/
158
163
async destroy ( key : string ) : Promise < boolean > {
159
- if ( ! this . _enable ) return false ;
164
+ if ( ! this . _enable ) return false ;
160
165
161
166
key = this . _setKeyPrefix ( key ) ;
162
167
const destroyPromises = [ this . redisClient . del ( key ) ] ;
@@ -179,6 +184,8 @@ export class RedisCache implements ICache {
179
184
* @returns The return value is a boolean indicating whether the flush operation was successful.
180
185
*/
181
186
async flush ( ) : Promise < boolean > {
187
+ if ( ! this . _enable ) return false ;
188
+
182
189
if ( this . _tags . length ) {
183
190
const keys = await this . redisClient . sUnion ( this . _tags ) ;
184
191
if ( ! keys . length ) return false ;
@@ -239,7 +246,6 @@ export class RedisCache implements ICache {
239
246
this . _tags = [ ] ;
240
247
}
241
248
242
-
243
249
/**
244
250
* Set the key prefix to the value of the _prefix property
245
251
* @param {string } key - The key to set.
@@ -248,4 +254,4 @@ export class RedisCache implements ICache {
248
254
private _setKeyPrefix ( key : string ) : string {
249
255
return `${ this . _prefix } :${ key } ` ;
250
256
}
251
- }
257
+ }
0 commit comments