Skip to content

Commit fd31e43

Browse files
Polishing.
Move maxTime calculation for topology refresh to and delegate shouldUseCachedValue() to newly introduced overload. Original Pull Request: #2989
1 parent 1514461 commit fd31e43

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,8 @@ public ClusterTopology getTopology() {
854854

855855
try (Connection connection = entry.getValue().getResource()) {
856856

857-
858857
Set<RedisClusterNode> nodes = Converters.toSetOfRedisClusterNodes(new Jedis(connection).clusterNodes());
859-
topology = cached = new JedisClusterTopology(nodes, System.currentTimeMillis());
858+
topology = cached = new JedisClusterTopology(nodes, System.currentTimeMillis(), cacheTimeMs);
860859
return topology;
861860

862861
} catch (Exception ex) {
@@ -884,9 +883,9 @@ public ClusterTopology getTopology() {
884883
* @since 2.2
885884
* @deprecated since 3.3.4, use {@link #shouldUseCachedValue(JedisClusterTopology)} instead.
886885
*/
887-
@Deprecated(since = "3.3.4")
886+
@Deprecated(since = "3.3.4", forRemoval = true)
888887
protected boolean shouldUseCachedValue() {
889-
return false;
888+
return shouldUseCachedValue(cached);
890889
}
891890

892891
/**
@@ -899,22 +898,38 @@ protected boolean shouldUseCachedValue() {
899898
* @since 3.3.4
900899
*/
901900
protected boolean shouldUseCachedValue(@Nullable JedisClusterTopology topology) {
902-
return topology != null && topology.getTime() + cacheTimeMs > System.currentTimeMillis();
901+
return topology != null && topology.getMaxTime() > System.currentTimeMillis();
903902
}
904903
}
905904

906905
protected static class JedisClusterTopology extends ClusterTopology {
907906

908907
private final long time;
908+
private final long timeoutMs;
909909

910-
public JedisClusterTopology(Set<RedisClusterNode> nodes, long time) {
910+
JedisClusterTopology(Set<RedisClusterNode> nodes, long creationTimeMs, long timeoutMs) {
911911
super(nodes);
912-
this.time = time;
912+
this.time = creationTimeMs;
913+
this.timeoutMs = timeoutMs;
913914
}
914915

916+
/**
917+
* Get the time in ms when the {@link ClusterTopology} was captured.
918+
*
919+
* @return ClusterTopology time.
920+
*/
915921
public long getTime() {
916922
return time;
917923
}
924+
925+
/**
926+
* Get the maximum time in ms the {@link ClusterTopology} should be used before a refresh is required.
927+
*
928+
* @return ClusterTopology maximum age.
929+
*/
930+
long getMaxTime() {
931+
return time + timeoutMs;
932+
}
918933
}
919934

920935
protected JedisCluster getCluster() {

src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,8 +2965,8 @@ void shouldUseCachedTopology() {
29652965
assertThat(topology).isInstanceOf(JedisClusterConnection.JedisClusterTopology.class);
29662966

29672967
assertThat(provider.shouldUseCachedValue(null)).isFalse();
2968-
assertThat(provider.shouldUseCachedValue(new JedisClusterConnection.JedisClusterTopology(Set.of(), 0))).isFalse();
2968+
assertThat(provider.shouldUseCachedValue(new JedisClusterConnection.JedisClusterTopology(Set.of(), System.currentTimeMillis() - 101, 100))).isFalse();
29692969
assertThat(provider.shouldUseCachedValue(
2970-
new JedisClusterConnection.JedisClusterTopology(Set.of(), System.currentTimeMillis() + 100))).isTrue();
2970+
new JedisClusterConnection.JedisClusterTopology(Set.of(), System.currentTimeMillis() + 100, 100))).isTrue();
29712971
}
29722972
}

0 commit comments

Comments
 (0)