Skip to content

Commit 3ab037f

Browse files
artembilangaryrussell
authored andcommitted
Fix RedisUtils to detect UNLINK per RedisOps
This might be a fact that application doesn't use a single `RedisOperations`. Also the same JVM may be shared between several applications with different connections to different Redis servers * change plain `Boolean unlinkAvailable` property to the `Map<RedisOperations<?, ?>, Boolean>` and `computeIfAbsent()` per provided `RedisOperations`
1 parent da97794 commit 3ab037f

File tree

1 file changed

+13
-6
lines changed
  • spring-integration-redis/src/main/java/org/springframework/integration/redis/util

1 file changed

+13
-6
lines changed

spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisUtils.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.redis.util;
1818

19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
1921
import java.util.Properties;
2022

2123
import org.springframework.data.redis.core.RedisCallback;
@@ -34,9 +36,15 @@ public final class RedisUtils {
3436

3537
private static final String VERSION_PROPERTY = "redis_version";
3638

37-
private static final int MAJOR_VERSION_TO_COMPARE = 4;
39+
private static final Map<RedisOperations<?, ?>, Boolean> unlinkAvailable =
40+
new LinkedHashMap<RedisOperations<?, ?>, Boolean>() {
3841

39-
private static Boolean unlinkAvailable;
42+
@Override
43+
protected boolean removeEldestEntry(Map.Entry eldest) {
44+
return size() > 100;
45+
}
46+
47+
};
4048

4149
/**
4250
* Perform an {@code INFO} command on the provided {@link RedisOperations} to check
@@ -46,18 +54,17 @@ public final class RedisUtils {
4654
* @throws IllegalStateException when {@code INFO} returns null from the Redis.
4755
*/
4856
public static boolean isUnlinkAvailable(RedisOperations<?, ?> redisOperations) {
49-
if (unlinkAvailable == null) {
57+
return unlinkAvailable.computeIfAbsent(redisOperations, key -> {
5058
Properties info = redisOperations.execute(
5159
(RedisCallback<Properties>) connection -> connection.serverCommands().info(SECTION));
5260
if (info != null) {
5361
int majorVersion = Integer.parseInt(info.getProperty(VERSION_PROPERTY).split("\\.")[0]);
54-
unlinkAvailable = majorVersion >= MAJOR_VERSION_TO_COMPARE;
62+
return majorVersion >= 4;
5563
}
5664
else {
5765
throw new IllegalStateException("The INFO command cannot be used in pipeline/transaction.");
5866
}
59-
}
60-
return unlinkAvailable;
67+
});
6168
}
6269

6370
private RedisUtils() {

0 commit comments

Comments
 (0)