2424import java .util .concurrent .CompletableFuture ;
2525import java .util .concurrent .TimeUnit ;
2626import java .util .concurrent .atomic .AtomicLong ;
27+ import java .util .function .Consumer ;
2728import java .util .function .Function ;
2829import java .util .function .Supplier ;
2930
@@ -177,10 +178,8 @@ public byte[] get(String name, byte[] key, Supplier<byte[]> valueLoader, @Nullab
177178
178179 return execute (name , connection -> {
179180
180- boolean wasLocked = false ;
181181 if (isLockingCacheWriter ()) {
182182 doLock (name , key , null , connection );
183- wasLocked = true ;
184183 }
185184
186185 try {
@@ -195,7 +194,7 @@ public byte[] get(String name, byte[] key, Supplier<byte[]> valueLoader, @Nullab
195194 doPut (connection , name , key , value , ttl );
196195 return value ;
197196 } finally {
198- if (isLockingCacheWriter () && wasLocked ) {
197+ if (isLockingCacheWriter ()) {
199198 doUnlock (name , connection );
200199 }
201200 }
@@ -274,10 +273,8 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
274273
275274 return execute (name , connection -> {
276275
277- boolean wasLocked = false ;
278276 if (isLockingCacheWriter ()) {
279277 doLock (name , key , value , connection );
280- wasLocked = true ;
281278 }
282279
283280 try {
@@ -299,7 +296,7 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
299296 return connection .stringCommands ().get (key );
300297
301298 } finally {
302- if (isLockingCacheWriter () && wasLocked ) {
299+ if (isLockingCacheWriter ()) {
303300 doUnlock (name , connection );
304301 }
305302 }
@@ -324,12 +321,9 @@ public void clean(String name, byte[] pattern) {
324321
325322 execute (name , connection -> {
326323
327- boolean wasLocked = false ;
328-
329324 try {
330325 if (isLockingCacheWriter ()) {
331326 doLock (name , name , pattern , connection );
332- wasLocked = true ;
333327 }
334328
335329 long deleteCount = batchStrategy .cleanCache (connection , name , pattern );
@@ -342,7 +336,7 @@ public void clean(String name, byte[] pattern) {
342336 statistics .incDeletesBy (name , (int ) deleteCount );
343337
344338 } finally {
345- if (wasLocked && isLockingCacheWriter ()) {
339+ if (isLockingCacheWriter ()) {
346340 doUnlock (name , connection );
347341 }
348342 }
@@ -373,10 +367,10 @@ public RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheSt
373367 * @param name the name of the cache to lock.
374368 */
375369 void lock (String name ) {
376- execute (name , connection -> doLock (name , name , null , connection ));
370+ executeWithoutResult (name , connection -> doLock (name , name , null , connection ));
377371 }
378372
379- boolean doLock (String name , Object contextualKey , @ Nullable Object contextualValue , RedisConnection connection ) {
373+ void doLock (String name , Object contextualKey , @ Nullable Object contextualValue , RedisConnection connection ) {
380374
381375 RedisStringCommands commands = connection .stringCommands ();
382376 Expiration expiration = Expiration .from (this .lockTtl .getTimeToLive (contextualKey , contextualValue ));
@@ -386,8 +380,6 @@ boolean doLock(String name, Object contextualKey, @Nullable Object contextualVal
386380 true )) {
387381 checkAndPotentiallyWaitUntilUnlocked (name , connection );
388382 }
389-
390- return true ;
391383 }
392384
393385 /**
@@ -412,6 +404,14 @@ private <T> T execute(String name, Function<RedisConnection, T> callback) {
412404 }
413405 }
414406
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+
415415 private <T > T executeLockFree (Function <RedisConnection , T > callback ) {
416416
417417 try (RedisConnection connection = this .connectionFactory .getConnection ()) {
0 commit comments