24
24
import java .util .concurrent .CompletableFuture ;
25
25
import java .util .concurrent .TimeUnit ;
26
26
import java .util .concurrent .atomic .AtomicLong ;
27
+ import java .util .function .Consumer ;
27
28
import java .util .function .Function ;
28
29
import java .util .function .Supplier ;
29
30
@@ -177,10 +178,8 @@ public byte[] get(String name, byte[] key, Supplier<byte[]> valueLoader, @Nullab
177
178
178
179
return execute (name , connection -> {
179
180
180
- boolean wasLocked = false ;
181
181
if (isLockingCacheWriter ()) {
182
182
doLock (name , key , null , connection );
183
- wasLocked = true ;
184
183
}
185
184
186
185
try {
@@ -195,7 +194,7 @@ public byte[] get(String name, byte[] key, Supplier<byte[]> valueLoader, @Nullab
195
194
doPut (connection , name , key , value , ttl );
196
195
return value ;
197
196
} finally {
198
- if (isLockingCacheWriter () && wasLocked ) {
197
+ if (isLockingCacheWriter ()) {
199
198
doUnlock (name , connection );
200
199
}
201
200
}
@@ -274,10 +273,8 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
274
273
275
274
return execute (name , connection -> {
276
275
277
- boolean wasLocked = false ;
278
276
if (isLockingCacheWriter ()) {
279
277
doLock (name , key , value , connection );
280
- wasLocked = true ;
281
278
}
282
279
283
280
try {
@@ -299,7 +296,7 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
299
296
return connection .stringCommands ().get (key );
300
297
301
298
} finally {
302
- if (isLockingCacheWriter () && wasLocked ) {
299
+ if (isLockingCacheWriter ()) {
303
300
doUnlock (name , connection );
304
301
}
305
302
}
@@ -324,12 +321,9 @@ public void clean(String name, byte[] pattern) {
324
321
325
322
execute (name , connection -> {
326
323
327
- boolean wasLocked = false ;
328
-
329
324
try {
330
325
if (isLockingCacheWriter ()) {
331
326
doLock (name , name , pattern , connection );
332
- wasLocked = true ;
333
327
}
334
328
335
329
long deleteCount = batchStrategy .cleanCache (connection , name , pattern );
@@ -342,7 +336,7 @@ public void clean(String name, byte[] pattern) {
342
336
statistics .incDeletesBy (name , (int ) deleteCount );
343
337
344
338
} finally {
345
- if (wasLocked && isLockingCacheWriter ()) {
339
+ if (isLockingCacheWriter ()) {
346
340
doUnlock (name , connection );
347
341
}
348
342
}
@@ -373,10 +367,10 @@ public RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheSt
373
367
* @param name the name of the cache to lock.
374
368
*/
375
369
void lock (String name ) {
376
- execute (name , connection -> doLock (name , name , null , connection ));
370
+ executeWithoutResult (name , connection -> doLock (name , name , null , connection ));
377
371
}
378
372
379
- boolean doLock (String name , Object contextualKey , @ Nullable Object contextualValue , RedisConnection connection ) {
373
+ void doLock (String name , Object contextualKey , @ Nullable Object contextualValue , RedisConnection connection ) {
380
374
381
375
RedisStringCommands commands = connection .stringCommands ();
382
376
Expiration expiration = Expiration .from (this .lockTtl .getTimeToLive (contextualKey , contextualValue ));
@@ -386,8 +380,6 @@ boolean doLock(String name, Object contextualKey, @Nullable Object contextualVal
386
380
true )) {
387
381
checkAndPotentiallyWaitUntilUnlocked (name , connection );
388
382
}
389
-
390
- return true ;
391
383
}
392
384
393
385
/**
@@ -412,6 +404,14 @@ private <T> T execute(String name, Function<RedisConnection, T> callback) {
412
404
}
413
405
}
414
406
407
+ private void executeWithoutResult (String name , Consumer <RedisConnection > callback ) {
408
+
409
+ try (RedisConnection connection = this .connectionFactory .getConnection ()) {
410
+ checkAndPotentiallyWaitUntilUnlocked (name , connection );
411
+ callback .accept (connection );
412
+ }
413
+ }
414
+
415
415
private <T > T executeLockFree (Function <RedisConnection , T > callback ) {
416
416
417
417
try (RedisConnection connection = this .connectionFactory .getConnection ()) {
0 commit comments