Polishing.

Remove no longer needed wasLocked flag from CacheWriter.

Original Pull Request: #2948
This commit is contained in:
Christoph Strobl
2024-08-22 15:09:04 +02:00
parent 49542eccee
commit 0f45851190
3 changed files with 46 additions and 18 deletions

View File

@@ -24,6 +24,7 @@ import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -177,10 +178,8 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
return execute(name, connection -> {
boolean wasLocked = false;
if (isLockingCacheWriter()) {
doLock(name, key, null, connection);
wasLocked = true;
}
try {
@@ -195,7 +194,7 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
doPut(connection, name, key, value, ttl);
return value;
} finally {
if (isLockingCacheWriter() && wasLocked) {
if (isLockingCacheWriter()) {
doUnlock(name, connection);
}
}
@@ -274,10 +273,8 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
return execute(name, connection -> {
boolean wasLocked = false;
if (isLockingCacheWriter()) {
doLock(name, key, value, connection);
wasLocked = true;
}
try {
@@ -299,7 +296,7 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
return connection.stringCommands().get(key);
} finally {
if (isLockingCacheWriter() && wasLocked) {
if (isLockingCacheWriter()) {
doUnlock(name, connection);
}
}
@@ -324,12 +321,9 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
execute(name, connection -> {
boolean wasLocked = false;
try {
if (isLockingCacheWriter()) {
doLock(name, name, pattern, connection);
wasLocked = true;
}
long deleteCount = batchStrategy.cleanCache(connection, name, pattern);
@@ -342,7 +336,7 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
statistics.incDeletesBy(name, (int) deleteCount);
} finally {
if (wasLocked && isLockingCacheWriter()) {
if (isLockingCacheWriter()) {
doUnlock(name, connection);
}
}
@@ -373,10 +367,10 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
* @param name the name of the cache to lock.
*/
void lock(String name) {
execute(name, connection -> doLock(name, name, null, connection));
executeWithoutResult(name, connection -> doLock(name, name, null, connection));
}
boolean doLock(String name, Object contextualKey, @Nullable Object contextualValue, RedisConnection connection) {
void doLock(String name, Object contextualKey, @Nullable Object contextualValue, RedisConnection connection) {
RedisStringCommands commands = connection.stringCommands();
Expiration expiration = Expiration.from(this.lockTtl.getTimeToLive(contextualKey, contextualValue));
@@ -386,8 +380,6 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
true)) {
checkAndPotentiallyWaitUntilUnlocked(name, connection);
}
return true;
}
/**
@@ -412,6 +404,14 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
}
}
private void executeWithoutResult(String name, Consumer<RedisConnection> callback) {
try (RedisConnection connection = this.connectionFactory.getConnection()) {
checkAndPotentiallyWaitUntilUnlocked(name, connection);
callback.accept(connection);
}
}
private <T> T executeLockFree(Function<RedisConnection, T> callback) {
try (RedisConnection connection = this.connectionFactory.getConnection()) {