@@ -805,13 +805,11 @@ public void returnResourceForSpecificNode(RedisClusterNode node, Object client)
805805 */
806806 public static class JedisClusterTopologyProvider implements ClusterTopologyProvider {
807807
808- private long time = 0 ;
808+ private final JedisCluster cluster ;
809809
810810 private final long cacheTimeMs ;
811811
812- private @ Nullable ClusterTopology cached ;
813-
814- private final JedisCluster cluster ;
812+ private volatile @ Nullable JedisClusterTopology cached ;
815813
816814 /**
817815 * Create new {@link JedisClusterTopologyProvider}. Uses a default cache timeout of 100 milliseconds.
@@ -842,12 +840,12 @@ public JedisClusterTopologyProvider(JedisCluster cluster, Duration cacheTimeout)
842840 @ Override
843841 public ClusterTopology getTopology () {
844842
845- if (cached != null && shouldUseCachedValue ()) {
846- return cached ;
843+ JedisClusterTopology topology = cached ;
844+ if (shouldUseCachedValue (topology )) {
845+ return topology ;
847846 }
848847
849848 Map <String , Exception > errors = new LinkedHashMap <>();
850-
851849 List <Entry <String , ConnectionPool >> list = new ArrayList <>(cluster .getClusterNodes ().entrySet ());
852850
853851 Collections .shuffle (list );
@@ -856,13 +854,10 @@ public ClusterTopology getTopology() {
856854
857855 try (Connection connection = entry .getValue ().getResource ()) {
858856
859- time = System .currentTimeMillis ();
860857
861858 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 ;
866861
867862 } catch (Exception ex ) {
868863 errors .put (entry .getKey (), ex );
@@ -887,9 +882,38 @@ public ClusterTopology getTopology() {
887882 * topology.
888883 * @see #JedisClusterTopologyProvider(JedisCluster, Duration)
889884 * @since 2.2
885+ * @deprecated since 3.3.4, use {@link #shouldUseCachedValue(JedisClusterTopology)} instead.
890886 */
887+ @ Deprecated (since = "3.3.4" )
891888 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 ;
893917 }
894918 }
895919
0 commit comments