Skip to content

Commit

Permalink
avoid public methods in config classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Roiocam committed Mar 27, 2024
1 parent dd4d643 commit 363d673
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import redis.clients.jedis.commands.StringBinaryCommands;
import redis.clients.jedis.commands.StringPipelineBinaryCommands;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.providers.ClusterConnectionProvider;
import redis.clients.jedis.util.Pool;

import java.io.Closeable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -49,6 +51,7 @@ public class RedisCache<K, V> extends AbstractExternalCache<K, V> {

Function<Object, byte[]> valueEncoder;
Function<byte[], Object> valueDecoder;
ClusterConnectionProvider provider = null;

private static ThreadLocalRandom random = ThreadLocalRandom.current();

Expand Down Expand Up @@ -84,6 +87,18 @@ public RedisCache(RedisCacheConfig<K, V> config) {
if (config.isExpireAfterAccess()) {
throw new CacheConfigException("expireAfterAccess is not supported");
}
UnifiedJedis jedis = config.getJedis();
if (jedis != null && jedis instanceof JedisCluster) {
try {
Field field = UnifiedJedis.class.getDeclaredField("provider");
boolean accessible = field.isAccessible();
field.setAccessible(true);
provider = (ClusterConnectionProvider) field.get(jedis);
field.setAccessible(accessible);
} catch (Exception ex) {
throw new IllegalStateException("can not get ConnectionProvider from JedisClient", ex);
}
}
}

private int slaveCount() {
Expand Down Expand Up @@ -282,13 +297,13 @@ protected CacheResult do_PUT_ALL(Map<? extends K, ? extends V> map, long expireA
try {
commands = (StringBinaryCommands) writeCommands();
StringPipelineBinaryCommands pipeline;
// JedisPooled, JedisCluster 都是用到连接池的连接,需要还回去
// The connection from JedisPooled or JedisCluster needs to be returned to the pool.
if (commands instanceof JedisPooled) {
Connection connection = ((JedisPooled) commands).getPool().getResource();
closeable = connection;
pipeline = new Pipeline(connection);
} else if (commands instanceof JedisCluster) {
ClusterPipeline clusterPipeline = new ClusterPipeline(config.getProvider());
ClusterPipeline clusterPipeline = new ClusterPipeline(provider);
closeable = clusterPipeline;
pipeline = clusterPipeline;
} else if (commands instanceof Jedis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import com.alicp.jetcache.external.ExternalCacheConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.providers.ClusterConnectionProvider;
import redis.clients.jedis.util.Pool;

import java.lang.reflect.Field;

/**
* Created on 2016/10/7.
*
Expand All @@ -22,7 +18,6 @@ public class RedisCacheConfig<K, V> extends ExternalCacheConfig<K, V> {
private UnifiedJedis[] slaves;
private boolean readFromSlave;
private int[] slaveReadWeights;
private ClusterConnectionProvider provider;

public Pool<Jedis> getJedisPool() {
return jedisPool;
Expand All @@ -46,18 +41,6 @@ public UnifiedJedis getJedis() {

public void setJedis(UnifiedJedis jedis) {
this.jedis = jedis;
if (jedis instanceof JedisCluster) {
Class<? extends UnifiedJedis> clz = jedis.getClass();
try {
Field field = clz.getField("provider");
boolean accessible = field.isAccessible();
field.setAccessible(true);
provider = (ClusterConnectionProvider) field.get(jedis);
field.setAccessible(accessible);
} catch (Exception ex) {
throw new IllegalStateException("can not get ConnectionProvider from JedisClient", ex);
}
}
}

public UnifiedJedis[] getSlaves() {
Expand All @@ -83,8 +66,4 @@ public int[] getSlaveReadWeights() {
public void setSlaveReadWeights(int... slaveReadWeights) {
this.slaveReadWeights = slaveReadWeights;
}

public ClusterConnectionProvider getProvider() {
return provider;
}
}

0 comments on commit 363d673

Please sign in to comment.