11import calculate from 'cluster-key-slot' ;
22import { REVALIDATED_TAGS_KEY } from '../constants.js' ;
3- import { createRedisTimeoutConfig } from '../helpers/create-redis-timeout-config.js' ;
43import { isTagImplicit } from '../helpers/is-tag-implicit.js' ;
54import type { CacheHandlerValue , Handler } from '../cache-handler.js' ;
65import type { CreateRedisStringsHandlerOptions } from '../common-types.js' ;
@@ -103,10 +102,9 @@ export default function createHandler({
103102 key ,
104103 { implicitTags } ,
105104 ) : 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 ) ;
110108
111109 if ( ! result ) {
112110 return null ;
@@ -124,21 +122,18 @@ export default function createHandler({
124122 return cacheValue ;
125123 }
126124
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 ) ) ;
132128
133129 for ( const timeString of revalidationTimes ) {
134130 if (
135131 timeString &&
136132 Number . parseInt ( timeString , 10 ) > cacheValue . lastModified
137133 ) {
138- await cluster . unlink (
139- createRedisTimeoutConfig ( timeoutMs ) ,
140- keyPrefix + key ,
141- ) ;
134+ await cluster
135+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
136+ . unlink ( keyPrefix + key ) ;
142137
143138 return null ;
144139 }
@@ -147,16 +142,17 @@ export default function createHandler({
147142 return cacheValue ;
148143 } ,
149144 async set ( key , cacheHandlerValue ) : Promise < void > {
150- const options = createRedisTimeoutConfig ( timeoutMs ) ;
145+ const options = {
146+ abortSignal : AbortSignal . timeout ( timeoutMs ) ,
147+ } ;
151148
152149 let setOperation : Promise < string | null > ;
153150
154- let expireOperation : Promise < boolean > | undefined ;
151+ let expireOperation : Promise < number > | undefined ;
155152
156153 switch ( keyExpirationStrategy ) {
157154 case 'EXAT' : {
158- setOperation = cluster . set (
159- options ,
155+ setOperation = cluster . withCommandOptions ( options ) . set (
160156 keyPrefix + key ,
161157 JSON . stringify ( cacheHandlerValue ) ,
162158 typeof cacheHandlerValue . lifespan ?. expireAt === 'number'
@@ -168,18 +164,20 @@ export default function createHandler({
168164 break ;
169165 }
170166 case 'EXPIREAT' : {
171- setOperation = cluster . set (
172- options ,
167+ setOperation = cluster . withCommandOptions ( options ) . set (
173168 keyPrefix + key ,
174169 JSON . stringify ( cacheHandlerValue ) ,
170+ typeof cacheHandlerValue . lifespan ?. expireAt === 'number'
171+ ? {
172+ EXAT : cacheHandlerValue . lifespan . expireAt ,
173+ }
174+ : undefined ,
175175 ) ;
176176
177177 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 )
183181 : undefined ;
184182 break ;
185183 }
@@ -192,12 +190,13 @@ export default function createHandler({
192190
193191 const setTagsOperation =
194192 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+ )
201200 : undefined ;
202201
203202 await Promise . all ( [ setOperation , expireOperation , setTagsOperation ] ) ;
@@ -206,34 +205,28 @@ export default function createHandler({
206205 // If the tag is an implicit tag, we need to mark it as revalidated.
207206 // The revalidation process is done by the CacheHandler class on the next get operation.
208207 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 ( ) ) ;
215211 }
216212
217213 const tagsMap : Map < string , string [ ] > = new Map ( ) ;
218214
219- let cursor = 0 ;
215+ let cursor = '0' ;
220216
221217 const hScanOptions = { COUNT : revalidateTagQuerySize } ;
222218
223219 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 ) ;
230223
231- for ( const { field, value } of remoteTagsPortion . tuples ) {
224+ for ( const { field, value } of remoteTagsPortion . entries ) {
232225 tagsMap . set ( field , JSON . parse ( value ) as string [ ] ) ;
233226 }
234227
235228 cursor = remoteTagsPortion . cursor ;
236- } while ( cursor !== 0 ) ;
229+ } while ( cursor !== '0' ) ;
237230
238231 const keysToDelete : string [ ] = [ ] ;
239232
@@ -256,27 +249,28 @@ export default function createHandler({
256249
257250 for ( const [ slot , keys ] of slotKeysMap ) {
258251 const targetMasterNode = cluster . slots [ slot ] ?. master ;
259- const client = await targetMasterNode ?. client ;
252+
253+ const client = targetMasterNode ?. client ;
260254
261255 if ( keys . length === 0 || ! client ) {
262256 continue ;
263257 }
264258
265259 unlinkPromises . push (
266- client . unlink ( createRedisTimeoutConfig ( timeoutMs ) , keys ) ,
260+ client . withAbortSignal ( AbortSignal . timeout ( timeoutMs ) ) . unlink ( keys ) ,
267261 ) ;
268262 }
269263
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 ) ;
275267
276268 await Promise . allSettled ( [ ...unlinkPromises , updateTagsOperation ] ) ;
277269 } ,
278270 async delete ( key ) : Promise < void > {
279- await cluster . unlink ( createRedisTimeoutConfig ( timeoutMs ) , key ) ;
271+ await cluster
272+ . withCommandOptions ( { abortSignal : AbortSignal . timeout ( timeoutMs ) } )
273+ . unlink ( key ) ;
280274 } ,
281275 } ;
282276}
0 commit comments