From 3ba75d2aad1e8e1f3cbd521a17ca83ce4ed4e690 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 30 May 2023 11:55:11 +0200 Subject: [PATCH] Use OBJECT, PTTL, and RESTORE `JedisCluster` methods directly. We now use directly JedisCluster methods instead of using our command executor routing as JedisCluster exposes the methods and we no longer require our own command routing. Closes #2589 --- .../jedis/JedisClusterKeyCommands.java | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java index 9b9f3c3e2..3525381d4 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java @@ -16,6 +16,7 @@ package org.springframework.data.redis.connection.jedis; import redis.clients.jedis.Jedis; +import redis.clients.jedis.params.RestoreParams; import redis.clients.jedis.params.ScanParams; import redis.clients.jedis.resps.ScanResult; @@ -366,10 +367,11 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); - return connection.getClusterCommandExecutor() - .executeCommandOnSingleNode((JedisClusterCommandCallback) client -> client.pttl(key), - connection.clusterGetNodeForKey(key)) - .getValue(); + try { + return connection.getCluster().pttl(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } @Override @@ -377,11 +379,11 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); - return connection.getClusterCommandExecutor() - .executeCommandOnSingleNode( - (JedisClusterCommandCallback) client -> Converters.millisecondsToTimeUnit(client.pttl(key), timeUnit), - connection.clusterGetNodeForKey(key)) - .getValue(); + try { + return Converters.millisecondsToTimeUnit(connection.getCluster().pttl(key), timeUnit); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } @Override @@ -389,10 +391,11 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); - return connection.getClusterCommandExecutor() - .executeCommandOnSingleNode((JedisClusterCommandCallback) client -> client.dump(key), - connection.clusterGetNodeForKey(key)) - .getValue(); + try { + return connection.getCluster().dump(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } @Override @@ -401,16 +404,16 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); Assert.notNull(serializedValue, "Serialized value must not be null"); - connection.getClusterCommandExecutor().executeCommandOnSingleNode((JedisClusterCommandCallback) client -> { + RestoreParams restoreParams = RestoreParams.restoreParams(); - if (!replace) { - return client.restore(key, ttlInMillis, serializedValue); - } - - return JedisConverters.toString(this.connection.execute("RESTORE", key, - Arrays.asList(JedisConverters.toBytes(ttlInMillis), serializedValue, JedisConverters.toBytes("REPLACE")))); - - }, connection.clusterGetNodeForKey(key)); + if (replace) { + restoreParams = restoreParams.replace(); + } + try { + connection.getCluster().restore(key, ttlInMillis, serializedValue, restoreParams); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } @Override @@ -471,10 +474,11 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); - return connection.getClusterCommandExecutor() - .executeCommandOnSingleNode((JedisClusterCommandCallback) client -> client.objectEncoding(key), - connection.clusterGetNodeForKey(key)) - .mapValue(JedisConverters::toEncoding); + try { + return JedisConverters.toEncoding(connection.getCluster().objectEncoding(key)); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } @Nullable @@ -483,10 +487,11 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); - return connection.getClusterCommandExecutor() - .executeCommandOnSingleNode((JedisClusterCommandCallback) client -> client.objectIdletime(key), - connection.clusterGetNodeForKey(key)) - .mapValue(Converters::secondsToDuration); + try { + return Converters.secondsToDuration(connection.getCluster().objectIdletime(key)); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } @Nullable @@ -495,10 +500,11 @@ class JedisClusterKeyCommands implements RedisKeyCommands { Assert.notNull(key, "Key must not be null"); - return connection.getClusterCommandExecutor() - .executeCommandOnSingleNode((JedisClusterCommandCallback) client -> client.objectRefcount(key), - connection.clusterGetNodeForKey(key)) - .getValue(); + try { + return connection.getCluster().objectRefcount(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } }