@@ -805,13 +805,11 @@ public void returnResourceForSpecificNode(RedisClusterNode node, Object client)
805
805
*/
806
806
public static class JedisClusterTopologyProvider implements ClusterTopologyProvider {
807
807
808
- private long time = 0 ;
808
+ private final JedisCluster cluster ;
809
809
810
810
private final long cacheTimeMs ;
811
811
812
- private @ Nullable ClusterTopology cached ;
813
-
814
- private final JedisCluster cluster ;
812
+ private volatile @ Nullable JedisClusterTopology cached ;
815
813
816
814
/**
817
815
* Create new {@link JedisClusterTopologyProvider}. Uses a default cache timeout of 100 milliseconds.
@@ -842,12 +840,12 @@ public JedisClusterTopologyProvider(JedisCluster cluster, Duration cacheTimeout)
842
840
@ Override
843
841
public ClusterTopology getTopology () {
844
842
845
- if (cached != null && shouldUseCachedValue ()) {
846
- return cached ;
843
+ JedisClusterTopology topology = cached ;
844
+ if (shouldUseCachedValue (topology )) {
845
+ return topology ;
847
846
}
848
847
849
848
Map <String , Exception > errors = new LinkedHashMap <>();
850
-
851
849
List <Entry <String , ConnectionPool >> list = new ArrayList <>(cluster .getClusterNodes ().entrySet ());
852
850
853
851
Collections .shuffle (list );
@@ -856,13 +854,10 @@ public ClusterTopology getTopology() {
856
854
857
855
try (Connection connection = entry .getValue ().getResource ()) {
858
856
859
- time = System .currentTimeMillis ();
860
857
861
858
Set <RedisClusterNode > nodes = Converters .toSetOfRedisClusterNodes (new Jedis (connection ).clusterNodes ());
862
-
863
- cached = new ClusterTopology (nodes );
864
-
865
- return cached ;
859
+ topology = cached = new JedisClusterTopology (nodes , System .currentTimeMillis ());
860
+ return topology ;
866
861
867
862
} catch (Exception ex ) {
868
863
errors .put (entry .getKey (), ex );
@@ -887,9 +882,38 @@ public ClusterTopology getTopology() {
887
882
* topology.
888
883
* @see #JedisClusterTopologyProvider(JedisCluster, Duration)
889
884
* @since 2.2
885
+ * @deprecated since 3.3.4, use {@link #shouldUseCachedValue(JedisClusterTopology)} instead.
890
886
*/
887
+ @ Deprecated (since = "3.3.4" )
891
888
protected boolean shouldUseCachedValue () {
892
- return time + cacheTimeMs > System .currentTimeMillis ();
889
+ return false ;
890
+ }
891
+
892
+ /**
893
+ * Returns whether {@link #getTopology()} should return the cached {@link JedisClusterTopology}. Uses a time-based
894
+ * caching.
895
+ *
896
+ * @return {@literal true} to use the cached {@link ClusterTopology}; {@literal false} to fetch a new cluster
897
+ * topology.
898
+ * @see #JedisClusterTopologyProvider(JedisCluster, Duration)
899
+ * @since 3.3.4
900
+ */
901
+ protected boolean shouldUseCachedValue (@ Nullable JedisClusterTopology topology ) {
902
+ return topology != null && topology .getTime () + cacheTimeMs > System .currentTimeMillis ();
903
+ }
904
+ }
905
+
906
+ protected static class JedisClusterTopology extends ClusterTopology {
907
+
908
+ private final long time ;
909
+
910
+ public JedisClusterTopology (Set <RedisClusterNode > nodes , long time ) {
911
+ super (nodes );
912
+ this .time = time ;
913
+ }
914
+
915
+ public long getTime () {
916
+ return time ;
893
917
}
894
918
}
895
919
0 commit comments