From 3ab037f1ebea2d34d62641ee244a37b5051ecff1 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 26 Jul 2018 14:07:51 -0400 Subject: [PATCH] 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, Boolean>` and `computeIfAbsent()` per provided `RedisOperations` --- .../integration/redis/util/RedisUtils.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisUtils.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisUtils.java index a5559e23df..f2bbabeb37 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisUtils.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisUtils.java @@ -16,6 +16,8 @@ package org.springframework.integration.redis.util; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Properties; import org.springframework.data.redis.core.RedisCallback; @@ -34,9 +36,15 @@ public final class RedisUtils { private static final String VERSION_PROPERTY = "redis_version"; - private static final int MAJOR_VERSION_TO_COMPARE = 4; + private static final Map, Boolean> unlinkAvailable = + new LinkedHashMap, Boolean>() { - private static Boolean unlinkAvailable; + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > 100; + } + + }; /** * Perform an {@code INFO} command on the provided {@link RedisOperations} to check @@ -46,18 +54,17 @@ public final class RedisUtils { * @throws IllegalStateException when {@code INFO} returns null from the Redis. */ public static boolean isUnlinkAvailable(RedisOperations redisOperations) { - if (unlinkAvailable == null) { + return unlinkAvailable.computeIfAbsent(redisOperations, key -> { Properties info = redisOperations.execute( (RedisCallback) connection -> connection.serverCommands().info(SECTION)); if (info != null) { int majorVersion = Integer.parseInt(info.getProperty(VERSION_PROPERTY).split("\\.")[0]); - unlinkAvailable = majorVersion >= MAJOR_VERSION_TO_COMPARE; + return majorVersion >= 4; } else { throw new IllegalStateException("The INFO command cannot be used in pipeline/transaction."); } - } - return unlinkAvailable; + }); } private RedisUtils() {