Skip to content

Commit aea748d

Browse files
committed
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 spring-projects#2589
1 parent 044ab7f commit aea748d

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java

+40-34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection.jedis;
1717

1818
import redis.clients.jedis.Jedis;
19+
import redis.clients.jedis.params.RestoreParams;
1920
import redis.clients.jedis.params.ScanParams;
2021
import redis.clients.jedis.resps.ScanResult;
2122

@@ -366,33 +367,35 @@ public Long pTtl(byte[] key) {
366367

367368
Assert.notNull(key, "Key must not be null");
368369

369-
return connection.getClusterCommandExecutor()
370-
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.pttl(key),
371-
connection.clusterGetNodeForKey(key))
372-
.getValue();
370+
try {
371+
return connection.getCluster().pttl(key);
372+
} catch (Exception ex) {
373+
throw convertJedisAccessException(ex);
374+
}
373375
}
374376

375377
@Override
376378
public Long pTtl(byte[] key, TimeUnit timeUnit) {
377379

378380
Assert.notNull(key, "Key must not be null");
379381

380-
return connection.getClusterCommandExecutor()
381-
.executeCommandOnSingleNode(
382-
(JedisClusterCommandCallback<Long>) client -> Converters.millisecondsToTimeUnit(client.pttl(key), timeUnit),
383-
connection.clusterGetNodeForKey(key))
384-
.getValue();
382+
try {
383+
return Converters.millisecondsToTimeUnit(connection.getCluster().pttl(key), timeUnit);
384+
} catch (Exception ex) {
385+
throw convertJedisAccessException(ex);
386+
}
385387
}
386388

387389
@Override
388390
public byte[] dump(byte[] key) {
389391

390392
Assert.notNull(key, "Key must not be null");
391393

392-
return connection.getClusterCommandExecutor()
393-
.executeCommandOnSingleNode((JedisClusterCommandCallback<byte[]>) client -> client.dump(key),
394-
connection.clusterGetNodeForKey(key))
395-
.getValue();
394+
try {
395+
return connection.getCluster().dump(key);
396+
} catch (Exception ex) {
397+
throw convertJedisAccessException(ex);
398+
}
396399
}
397400

398401
@Override
@@ -401,16 +404,16 @@ public void restore(byte[] key, long ttlInMillis, byte[] serializedValue, boolea
401404
Assert.notNull(key, "Key must not be null");
402405
Assert.notNull(serializedValue, "Serialized value must not be null");
403406

404-
connection.getClusterCommandExecutor().executeCommandOnSingleNode((JedisClusterCommandCallback<String>) client -> {
405-
406-
if (!replace) {
407-
return client.restore(key, ttlInMillis, serializedValue);
408-
}
409-
410-
return JedisConverters.toString(this.connection.execute("RESTORE", key,
411-
Arrays.asList(JedisConverters.toBytes(ttlInMillis), serializedValue, JedisConverters.toBytes("REPLACE"))));
407+
RestoreParams restoreParams = RestoreParams.restoreParams();
412408

413-
}, connection.clusterGetNodeForKey(key));
409+
if (replace) {
410+
restoreParams = restoreParams.replace();
411+
}
412+
try {
413+
connection.getCluster().restore(key, ttlInMillis, serializedValue, restoreParams);
414+
} catch (Exception ex) {
415+
throw convertJedisAccessException(ex);
416+
}
414417
}
415418

416419
@Override
@@ -471,10 +474,11 @@ public ValueEncoding encodingOf(byte[] key) {
471474

472475
Assert.notNull(key, "Key must not be null");
473476

474-
return connection.getClusterCommandExecutor()
475-
.executeCommandOnSingleNode((JedisClusterCommandCallback<byte[]>) client -> client.objectEncoding(key),
476-
connection.clusterGetNodeForKey(key))
477-
.mapValue(JedisConverters::toEncoding);
477+
try {
478+
return JedisConverters.toEncoding(connection.getCluster().objectEncoding(key));
479+
} catch (Exception ex) {
480+
throw convertJedisAccessException(ex);
481+
}
478482
}
479483

480484
@Nullable
@@ -483,10 +487,11 @@ public Duration idletime(byte[] key) {
483487

484488
Assert.notNull(key, "Key must not be null");
485489

486-
return connection.getClusterCommandExecutor()
487-
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.objectIdletime(key),
488-
connection.clusterGetNodeForKey(key))
489-
.mapValue(Converters::secondsToDuration);
490+
try {
491+
return Converters.secondsToDuration(connection.getCluster().objectIdletime(key));
492+
} catch (Exception ex) {
493+
throw convertJedisAccessException(ex);
494+
}
490495
}
491496

492497
@Nullable
@@ -495,10 +500,11 @@ public Long refcount(byte[] key) {
495500

496501
Assert.notNull(key, "Key must not be null");
497502

498-
return connection.getClusterCommandExecutor()
499-
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.objectRefcount(key),
500-
connection.clusterGetNodeForKey(key))
501-
.getValue();
503+
try {
504+
return connection.getCluster().objectRefcount(key);
505+
} catch (Exception ex) {
506+
throw convertJedisAccessException(ex);
507+
}
502508

503509
}
504510

0 commit comments

Comments
 (0)