1
1
import calculate from 'cluster-key-slot' ;
2
2
import { REVALIDATED_TAGS_KEY } from '../constants.js' ;
3
- import { createRedisTimeoutConfig } from '../helpers/create-redis-timeout-config.js' ;
4
3
import { isTagImplicit } from '../helpers/is-tag-implicit.js' ;
5
4
import type { CacheHandlerValue , Handler } from '../cache-handler.js' ;
6
5
import type { CreateRedisStringsHandlerOptions } from '../common-types.js' ;
@@ -103,10 +102,9 @@ export default function createHandler({
103
102
key ,
104
103
{ implicitTags } ,
105
104
) : Promise < CacheHandlerValue | null | undefined > {
106
- const result = await cluster . get (
107
- createRedisTimeoutConfig ( timeoutMs ) ,
108
- keyPrefix + key ,
109
- ) ;
105
+ const result = await cluster
106
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
107
+ . get ( keyPrefix + key ) ;
110
108
111
109
if ( ! result ) {
112
110
return null ;
@@ -124,21 +122,18 @@ export default function createHandler({
124
122
return cacheValue ;
125
123
}
126
124
127
- const revalidationTimes = await cluster . hmGet (
128
- createRedisTimeoutConfig ( timeoutMs ) ,
129
- revalidatedTagsKey ,
130
- Array . from ( combinedTags ) ,
131
- ) ;
125
+ const revalidationTimes = await cluster
126
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
127
+ . hmGet ( revalidatedTagsKey , Array . from ( combinedTags ) ) ;
132
128
133
129
for ( const timeString of revalidationTimes ) {
134
130
if (
135
131
timeString &&
136
132
Number . parseInt ( timeString , 10 ) > cacheValue . lastModified
137
133
) {
138
- await cluster . unlink (
139
- createRedisTimeoutConfig ( timeoutMs ) ,
140
- keyPrefix + key ,
141
- ) ;
134
+ await cluster
135
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
136
+ . unlink ( keyPrefix + key ) ;
142
137
143
138
return null ;
144
139
}
@@ -147,16 +142,17 @@ export default function createHandler({
147
142
return cacheValue ;
148
143
} ,
149
144
async set ( key , cacheHandlerValue ) : Promise < void > {
150
- const options = createRedisTimeoutConfig ( timeoutMs ) ;
145
+ const options = {
146
+ abortSignal : AbortSignal . timeout ( timeoutMs ) ,
147
+ } ;
151
148
152
149
let setOperation : Promise < string | null > ;
153
150
154
- let expireOperation : Promise < boolean > | undefined ;
151
+ let expireOperation : Promise < number > | undefined ;
155
152
156
153
switch ( keyExpirationStrategy ) {
157
154
case 'EXAT' : {
158
- setOperation = cluster . set (
159
- options ,
155
+ setOperation = cluster . withCommandOptions ( options ) . set (
160
156
keyPrefix + key ,
161
157
JSON . stringify ( cacheHandlerValue ) ,
162
158
typeof cacheHandlerValue . lifespan ?. expireAt === 'number'
@@ -168,18 +164,20 @@ export default function createHandler({
168
164
break ;
169
165
}
170
166
case 'EXPIREAT' : {
171
- setOperation = cluster . set (
172
- options ,
167
+ setOperation = cluster . withCommandOptions ( options ) . set (
173
168
keyPrefix + key ,
174
169
JSON . stringify ( cacheHandlerValue ) ,
170
+ typeof cacheHandlerValue . lifespan ?. expireAt === 'number'
171
+ ? {
172
+ EXAT : cacheHandlerValue . lifespan . expireAt ,
173
+ }
174
+ : undefined ,
175
175
) ;
176
176
177
177
expireOperation = cacheHandlerValue . lifespan
178
- ? cluster . expireAt (
179
- options ,
180
- keyPrefix + key ,
181
- cacheHandlerValue . lifespan . expireAt ,
182
- )
178
+ ? cluster
179
+ . withCommandOptions ( options )
180
+ . expireAt ( keyPrefix + key , cacheHandlerValue . lifespan . expireAt )
183
181
: undefined ;
184
182
break ;
185
183
}
@@ -192,12 +190,13 @@ export default function createHandler({
192
190
193
191
const setTagsOperation =
194
192
cacheHandlerValue . tags . length > 0
195
- ? cluster . hSet (
196
- options ,
197
- keyPrefix + sharedTagsKey ,
198
- key ,
199
- JSON . stringify ( cacheHandlerValue . tags ) ,
200
- )
193
+ ? cluster
194
+ . withCommandOptions ( options )
195
+ . hSet (
196
+ keyPrefix + sharedTagsKey ,
197
+ key ,
198
+ JSON . stringify ( cacheHandlerValue . tags ) ,
199
+ )
201
200
: undefined ;
202
201
203
202
await Promise . all ( [ setOperation , expireOperation , setTagsOperation ] ) ;
@@ -206,34 +205,28 @@ export default function createHandler({
206
205
// If the tag is an implicit tag, we need to mark it as revalidated.
207
206
// The revalidation process is done by the CacheHandler class on the next get operation.
208
207
if ( isTagImplicit ( tag ) ) {
209
- await cluster . hSet (
210
- createRedisTimeoutConfig ( timeoutMs ) ,
211
- revalidatedTagsKey ,
212
- tag ,
213
- Date . now ( ) ,
214
- ) ;
208
+ await cluster
209
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
210
+ . hSet ( revalidatedTagsKey , tag , Date . now ( ) ) ;
215
211
}
216
212
217
213
const tagsMap : Map < string , string [ ] > = new Map ( ) ;
218
214
219
- let cursor = 0 ;
215
+ let cursor = '0' ;
220
216
221
217
const hScanOptions = { COUNT : revalidateTagQuerySize } ;
222
218
223
219
do {
224
- const remoteTagsPortion = await cluster . hScan (
225
- createRedisTimeoutConfig ( timeoutMs ) ,
226
- keyPrefix + sharedTagsKey ,
227
- cursor ,
228
- hScanOptions ,
229
- ) ;
220
+ const remoteTagsPortion = await cluster
221
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
222
+ . hScan ( keyPrefix + sharedTagsKey , cursor , hScanOptions ) ;
230
223
231
- for ( const { field, value } of remoteTagsPortion . tuples ) {
224
+ for ( const { field, value } of remoteTagsPortion . entries ) {
232
225
tagsMap . set ( field , JSON . parse ( value ) as string [ ] ) ;
233
226
}
234
227
235
228
cursor = remoteTagsPortion . cursor ;
236
- } while ( cursor !== 0 ) ;
229
+ } while ( cursor !== '0' ) ;
237
230
238
231
const keysToDelete : string [ ] = [ ] ;
239
232
@@ -256,27 +249,28 @@ export default function createHandler({
256
249
257
250
for ( const [ slot , keys ] of slotKeysMap ) {
258
251
const targetMasterNode = cluster . slots [ slot ] ?. master ;
259
- const client = await targetMasterNode ?. client ;
252
+
253
+ const client = targetMasterNode ?. client ;
260
254
261
255
if ( keys . length === 0 || ! client ) {
262
256
continue ;
263
257
}
264
258
265
259
unlinkPromises . push (
266
- client . unlink ( createRedisTimeoutConfig ( timeoutMs ) , keys ) ,
260
+ client . withAbortSignal ( AbortSignal . timeout ( timeoutMs ) ) . unlink ( keys ) ,
267
261
) ;
268
262
}
269
263
270
- const updateTagsOperation = cluster . hDel (
271
- { isolated : true , ...createRedisTimeoutConfig ( timeoutMs ) } ,
272
- keyPrefix + sharedTagsKey ,
273
- tagsToDelete ,
274
- ) ;
264
+ const updateTagsOperation = cluster
265
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
266
+ . hDel ( keyPrefix + sharedTagsKey , tagsToDelete ) ;
275
267
276
268
await Promise . allSettled ( [ ...unlinkPromises , updateTagsOperation ] ) ;
277
269
} ,
278
270
async delete ( key ) : Promise < void > {
279
- await cluster . unlink ( createRedisTimeoutConfig ( timeoutMs ) , key ) ;
271
+ await cluster
272
+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
273
+ . unlink ( key ) ;
280
274
} ,
281
275
} ;
282
276
}
0 commit comments