32
32
import org .springframework .data .redis .connection .ReactiveStringCommands ;
33
33
import org .springframework .data .redis .connection .RedisConnection ;
34
34
import org .springframework .data .redis .connection .RedisConnectionFactory ;
35
+ import org .springframework .data .redis .connection .RedisStringCommands ;
35
36
import org .springframework .data .redis .connection .RedisStringCommands .SetOption ;
36
37
import org .springframework .data .redis .core .types .Expiration ;
37
38
import org .springframework .data .redis .util .ByteUtils ;
@@ -219,8 +220,10 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
219
220
220
221
return execute (name , connection -> {
221
222
223
+ boolean wasLocked = false ;
222
224
if (isLockingCacheWriter ()) {
223
225
doLock (name , key , value , connection );
226
+ wasLocked = true ;
224
227
}
225
228
226
229
try {
@@ -242,7 +245,7 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
242
245
return connection .stringCommands ().get (key );
243
246
244
247
} finally {
245
- if (isLockingCacheWriter ()) {
248
+ if (isLockingCacheWriter () && wasLocked ) {
246
249
doUnlock (name , connection );
247
250
}
248
251
}
@@ -319,15 +322,17 @@ void lock(String name) {
319
322
execute (name , connection -> doLock (name , name , null , connection ));
320
323
}
321
324
322
- @ Nullable
323
- protected Boolean doLock (String name , Object contextualKey , @ Nullable Object contextualValue ,
324
- RedisConnection connection ) {
325
+ boolean doLock (String name , Object contextualKey , @ Nullable Object contextualValue , RedisConnection connection ) {
325
326
327
+ RedisStringCommands commands = connection .stringCommands ();
326
328
Expiration expiration = Expiration .from (this .lockTtl .getTimeToLive (contextualKey , contextualValue ));
329
+ byte [] cacheLockKey = createCacheLockKey (name );
327
330
328
- while (!ObjectUtils .nullSafeEquals (connection .stringCommands ().set (createCacheLockKey (name ), new byte [0 ], expiration , SetOption .SET_IF_ABSENT ),true )) {
331
+ while (!ObjectUtils .nullSafeEquals (commands .set (cacheLockKey , new byte [0 ], expiration , SetOption .SET_IF_ABSENT ),
332
+ true )) {
329
333
checkAndPotentiallyWaitUntilUnlocked (name , connection );
330
334
}
335
+
331
336
return true ;
332
337
}
333
338
@@ -341,7 +346,7 @@ void unlock(String name) {
341
346
}
342
347
343
348
@ Nullable
344
- private Long doUnlock (String name , RedisConnection connection ) {
349
+ Long doUnlock (String name , RedisConnection connection ) {
345
350
return connection .keyCommands ().del (createCacheLockKey (name ));
346
351
}
347
352
@@ -489,8 +494,7 @@ public CompletableFuture<byte[]> retrieve(String name, byte[] key, @Nullable Dur
489
494
Mono <?> cacheLockCheck = isLockingCacheWriter () ? waitForLock (connection , name ) : Mono .empty ();
490
495
ReactiveStringCommands stringCommands = connection .stringCommands ();
491
496
492
- Mono <ByteBuffer > get = shouldExpireWithin (ttl )
493
- ? stringCommands .getEx (wrappedKey , Expiration .from (ttl ))
497
+ Mono <ByteBuffer > get = shouldExpireWithin (ttl ) ? stringCommands .getEx (wrappedKey , Expiration .from (ttl ))
494
498
: stringCommands .get (wrappedKey );
495
499
496
500
return cacheLockCheck .then (get ).map (ByteUtils ::getBytes ).toFuture ();
@@ -502,8 +506,7 @@ public CompletableFuture<Void> store(String name, byte[] key, byte[] value, @Nul
502
506
503
507
return doWithConnection (connection -> {
504
508
505
- Mono <?> mono = isLockingCacheWriter ()
506
- ? doStoreWithLocking (name , key , value , ttl , connection )
509
+ Mono <?> mono = isLockingCacheWriter () ? doStoreWithLocking (name , key , value , ttl , connection )
507
510
: doStore (key , value , ttl , connection );
508
511
509
512
return mono .then ().toFuture ();
@@ -531,7 +534,6 @@ private Mono<Boolean> doStore(byte[] cacheKey, byte[] value, @Nullable Duration
531
534
}
532
535
}
533
536
534
-
535
537
private Mono <Object > doLock (String name , Object contextualKey , @ Nullable Object contextualValue ,
536
538
ReactiveRedisConnection connection ) {
537
539
0 commit comments