学习redis过程中,写的一个redis工具类。功能还有待改进,主要是用来学习的。
欢迎指正错误和优化
如果觉得代码太长,可以去最下方的项目地址阅读。
工具类代码:
package com.hyr.redis;
import com.hyr.redis.help.RedisHelper;
import com.hyr.redis.message.ResultMessage;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import redis.clients.jedis.*;
import redis.clients.util.JedisClusterCRC16;
import redis.clients.util.Slowlog;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
/*******************************************************************************
* @date 2018-02-28 下午 5:45
* @author: <a href=mailto:>黄跃然</a>
* @Description: 集群环境redis操作指令
******************************************************************************/
public class RedisClusterUtils {
private static Logger logger = Logger.getLogger(RedisClusterUtils.class);
private static RedisClusterProxy jedisCluster = null;
private RedisClusterUtils(RedisClusterProxy jedisCluster) {
RedisClusterUtils.jedisCluster = jedisCluster;
RedisClusterUtils.jedisCluster.close();
}
/**
* get redis cluster instance, the instance is single
*
* @param hostAndPortAddress split by ','
* @return
*/
public static synchronized RedisClusterProxy getRedisClusterInstance(String hostAndPortAddress) {
if (jedisCluster == null) {
if (StringUtils.isEmpty(hostAndPortAddress)) {
return null;
}
Set<HostAndPort> hostAndPorts = getHostAndPort(hostAndPortAddress);
jedisCluster = new RedisClusterProxy(hostAndPorts);
}
return jedisCluster;
}
/**
* @param hostAndPortAddress
* @return
*/
private static synchronized Set<HostAndPort> getHostAndPort(String hostAndPortAddress) {
Set<HostAndPort> hostAndPorts = new HashSet<HostAndPort>();
String[] hostAndPortArray = hostAndPortAddress.split(",");
for (String hostAndPort : hostAndPortArray) {
String[] hostWithPort = hostAndPort.split(":");
hostAndPorts.add(new HostAndPort(hostWithPort[0], Integer.parseInt(hostWithPort[1])));
}
return hostAndPorts;
}
/**
* command : keys
*
* @param jedisCluster
* @param pattern
* @return
*/
public static synchronized TreeSet<String> keys(RedisClusterProxy jedisCluster, String pattern) {
TreeSet<String> keys = new TreeSet<String>();
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
Jedis jedis = jedisPool.getResource();
try {
keys.addAll(jedis.keys(pattern));
} catch (Exception e) {
logger.error("cluster keys is error. ", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
return keys;
}
/**
* command : cluster info
*
* @param jedisCluster
* @return
*/
public static synchronized String info(RedisClusterProxy jedisCluster) {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
StringBuilder sb = new StringBuilder();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
Jedis jedis = jedisPool.getResource();
try {
String info = jedis.info();
sb.append(info).append("=====================================================\n").append("\n");
} catch (Exception e) {
logger.error("cluster info error!", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
return sb.toString();
}
/**
* command : cluster info by section
*
* @param jedisCluster
* @return
*/
public static synchronized String info(RedisClusterProxy jedisCluster, String section) {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
StringBuilder sb = new StringBuilder();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
Jedis jedis = jedisPool.getResource();
try {
String info = jedis.info(section);
sb.append(info).append("=====================================================\n").append("\n");
} catch (Exception e) {
logger.error("cluster info error!", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
return sb.toString();
}
/**
* command : get cluster server memory info
*
* @param jedisCluster
* @return
*/
public static synchronized String server(RedisClusterProxy jedisCluster) {
String section = "server";
return info(jedisCluster, section);
}
/**
* command : get cluster clients info
*
* @param jedisCluster
* @return
*/
public static synchronized String clients(RedisClusterProxy jedisCluster) {
String section = "clients";
return info(jedisCluster, section);
}
/**
* command : get cluster info by section
*
* @param jedisCluster
* @return
*/
public static synchronized String memory(RedisClusterProxy jedisCluster) {
String section = "memory";
return info(jedisCluster, section);
}
/**
* command : get cluster persistence info
*
* @param jedisCluster
* @return
*/
public static synchronized String persistence(RedisClusterProxy jedisCluster) {
String section = "persistence";
return info(jedisCluster, section);
}
/**
* command : get cluster state info
*
* @param jedisCluster
* @return
*/
public static synchronized String state(RedisClusterProxy jedisCluster) {
String section = "state";
return info(jedisCluster, section);
}
/**
* command : get cluster cpu info
*
* @param jedisCluster
* @return
*/
public static synchronized String cpu(RedisClusterProxy jedisCluster) {
String section = "cpu";
return info(jedisCluster, section);
}
/**
* command : get cluster cluster info
*
* @param jedisCluster
* @return
*/
public static synchronized String cluster(RedisClusterProxy jedisCluster) {
String section = "cluster";
return info(jedisCluster, section);
}
/**
* command : get cluster keyspace info
*
* @param jedisCluster
* @return
*/
public static synchronized String keyspace(RedisClusterProxy jedisCluster) {
String section = "keyspace";
return info(jedisCluster, section);
}
/**
* command : cluster nodes
*
* @param jedisCluster
* @return
*/
public static synchronized String nodes(RedisClusterProxy jedisCluster) {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
StringBuilder sb = new StringBuilder();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
Jedis jedis = jedisPool.getResource();
try {
String nodeInfo = jedis.clusterNodes();
for (String infoLine : nodeInfo.split("\n")) {
if (infoLine.contains("myself")) {
sb.append(infoLine.replace("myself,", "")).append("\n");
}
}
} catch (Exception e) {
logger.error("cluster nodes is error.", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
return sb.toString();
}
/**
* command : call
*
* @param jedisCluster
* @return
*/
public static synchronized String call(RedisClusterProxy jedisCluster, String script) {
try {
JedisSlotBasedConnectionHandlerProxy connectionHandler = jedisCluster.getConnectionHandler();
Jedis redis = connectionHandler.getConnection();
Object result = redis.eval(script);
if (result != null && result instanceof List) {
List list = (List) result;
return (String) list.get(0);
}
} catch (Exception e) {
logger.error("cluster call is error.", e);
}
return null;
}
/**
* command : multi exec
*
* @param jedisCluster
* @return
*/
public static synchronized boolean transaction(RedisClusterProxy jedisCluster, RedisCallBack redisCallBack) {
boolean result;
try {
// TODO 可以优化为动态代理
result = redisCallBack.MultiAndExec(jedisCluster);
} catch (Exception e) {
result = false;
e.printStackTrace();
} finally {
}
return result;
}
abstract public static class RedisCallBack {
boolean MultiAndExec(RedisClusterProxy jedisCluster) {
Jedis m$ = getMasterNode(jedisCluster);
if (null == m$ || !m$.isConnected()) {
return false;
}
boolean result = true;
try {
List<String> keys = setKey();
allotSlot(m$, jedisCluster, keys);
Transaction transaction = m$.multi();
OnMultiAndExecListener(transaction);
transaction.exec();
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
/**
* get a master node
*
* @param jedisCluster
* @return
*/
private Jedis getMasterNode(RedisClusterProxy jedisCluster) {
Map<String, JedisPool> jps$ = jedisCluster.getClusterNodes();
for (JedisPool i$ : jps$.values()) {
Jedis redis = i$.getResource();
String nodesInfo = redis.info("replication");
if (nodesInfo.contains("role:master")) {
return redis;
}
}
return null;
}
/**
* allot slot by key
*
* @param t$
* @param jedisCluster
* @param keys
*/
void allotSlot(Jedis t$, RedisClusterProxy jedisCluster, List<String> keys) {
try {
for (String key : keys) {
int slot = getSlotByKey(jedisCluster, key);
Map<String, JedisPool> jps$ = jedisCluster.getClusterNodes();
for (JedisPool jedisPool : jps$.values()) {
Jedis redis = jedisPool.getResource();
String result = redis.clusterSetSlotNode(slot, RedisHelper.getNodeId(t$.clusterNodes()));
logger.debug("ask node:" + RedisHelper.getNodeId(redis.clusterNodes()) + " , slot:" + slot + " to node:" + RedisHelper.getNodeId(t$.clusterNodes()) + " is " + result);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* set all keys to allot slot
*
* @return keys
*/
public abstract List<String> setKey();
public abstract void OnMultiAndExecListener(Transaction transaction);
}
/**
* command : ping
*
* @param jedisCluster
* @return
*/
public static synchronized ResultMessage ping(RedisClusterProxy jedisCluster) {
ResultMessage resultMessage = ResultMessage.buildOK();
boolean result = true;
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
List<String> errorInfo = new ArrayList<String>();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
Jedis jedis = jedisPool.getResource();
try {
if (!jedis.ping().equals("PONG")) {
result = false;
errorInfo.add(RedisHelper.getNodeId(jedis.clusterNodes()) + "is failed!");
}
} catch (Exception e) {
resultMessage.setResult(false);
resultMessage.setInfos(e.getMessage());
logger.error("cluster ping is error.", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
resultMessage.setResult(result);
if (!result) {
resultMessage.setInfos(errorInfo);
}
return resultMessage;
}
/**
* command : random key
*
* @param jedisCluster
* @return key
*/
public static synchronized String randomKey(RedisClusterProxy jedisCluster) {
JedisPool jedisPool = null;
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
Object[] hostAndPorts = clusterNodes.keySet().toArray();
int randNum = new Random().nextInt(clusterNodes.size() - 1);
int maxCount = clusterNodes.size() * 6;
int index = 0;
jedisPool = clusterNodes.get(hostAndPorts[randNum]);
Set<String> keys = jedisPool.getResource().keys("*");
while (!jedisPool.isClosed() && keys.size() == 0 && index < maxCount) {
index++;
randNum = new Random().nextInt(clusterNodes.size() - 1);
keys = jedisPool.getResource().keys("*");
jedisPool = clusterNodes.get(hostAndPorts[randNum]);
}
return jedisPool.getResource().randomKey();
} catch (Exception e) {
logger.error("cluster randomKey is error. ", e);
}
return null;
}
/**
* test known node
*
* @param redisClusterProxy
* @param nodeId
* @return result
*/
public static synchronized boolean isKnownNode(RedisClusterProxy redisClusterProxy, String nodeId) {
boolean result = false;
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
String nodesInfo = jedisPool.getResource().clusterNodes();
for (String infoLine : nodesInfo.split("\n")) {
for (String info : infoLine.split(" ")) {
if (info.equals(nodeId)) {
result = true;
break;
}
}
}
}
}
return result;
}
/**
* test known node
*
* @param jedisCluster
* @param host
* @param port
* @return
*/
public static synchronized boolean isKnownNode(RedisClusterProxy jedisCluster, String host, int port) {
Jedis redis = new Jedis(host, port);
String nodeId = RedisHelper.getNodeId(redis.clusterNodes());
return isKnownNode(jedisCluster, nodeId);
}
/**
* command : flushdb
*
* @param redisClusterProxy
* @return
*/
public static synchronized boolean flushDB(RedisClusterProxy redisClusterProxy) {
boolean result = true;
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
Jedis redis = null;
try {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
redis = jedisPool.getResource();
String nodesInfo = redis.info("replication");
if (nodesInfo.contains("role:master")) {
redis.flushAll();
}
}
} catch (Exception e) {
result = false;
logger.error("cluster flushDB is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
}
return result;
}
/**
* command : save
*
* @param redisClusterProxy
* @return
*/
public static synchronized boolean save(RedisClusterProxy redisClusterProxy) {
boolean result = true;
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
Jedis redis = null;
try {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
redis = jedisPool.getResource();
redis.save();
}
} catch (Exception e) {
logger.error("cluster save is error.", e);
result = false;
} finally {
if (redis != null) {
redis.close();
}
}
}
return result;
}
/**
* command : last save
*
* @param redisClusterProxy
* @return
*/
public static synchronized String lastSave(RedisClusterProxy redisClusterProxy) {
StringBuilder sb = new StringBuilder();
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
Jedis redis = null;
try {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
redis = jedisPool.getResource();
Long lastSaveTime = redis.lastsave();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(new Date(lastSaveTime));
for (String infoLine : redis.clusterNodes().split("\n")) {
if (infoLine.contains("myself")) {
String[] infos = infoLine.split(" ");
sb.append(infos[0]).append("\t").append(infos[1]).append("\t").append(time).append("\n");
}
}
}
} catch (Exception e) {
logger.error("cluster lastSave is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
}
return sb.toString();
}
/**
* command : background rewrite aof file
*
* @param redisClusterProxy
* @return
*/
public static synchronized boolean bgRewriteAof(RedisClusterProxy redisClusterProxy) {
boolean result = true;
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
Jedis redis = null;
try {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
redis = jedisPool.getResource();
redis.bgrewriteaof();
}
} catch (Exception e) {
result = false;
logger.error("cluster bgRewriteAof is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
}
return result;
}
/**
* canceling the primary server Association, close slave copy. it not allowed in cluster .
*
* @param redisClusterProxy
* @return
*/
@Deprecated
public static synchronized boolean slaveOfNoOne(RedisClusterProxy redisClusterProxy) {
boolean result = true;
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
Jedis redis = null;
try {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
redis = jedisPool.getResource();
redis.slaveofNoOne();
}
} catch (Exception e) {
result = false;
logger.error("cluster slaveOfNoOne is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
}
return result;
}
/**
* command : background save
*
* @param redisClusterProxy
* @return
*/
public static synchronized boolean bgSave(RedisClusterProxy redisClusterProxy) {
boolean result = true;
Map<String, JedisPool> clusterNodes = redisClusterProxy.getClusterNodes();
for (String node : clusterNodes.keySet()) {
Jedis redis = null;
try {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
redis = jedisPool.getResource();
redis.bgsave();
}
} catch (Exception e) {
result = false;
logger.error("cluster bgSave is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
}
return result;
}
/**
* command : debug
*
* @param redisClusterProxy
* @param pattern
* @return
*/
public static synchronized String debug(RedisClusterProxy redisClusterProxy, String pattern) {
String result;
Jedis redis = null;
try {
JedisSlotBasedConnectionHandlerProxy connectionHandler = redisClusterProxy.getConnectionHandler();
int slot = JedisClusterCRC16.getSlot(pattern);
redis = connectionHandler.getConnectionFromSlot(slot);
result = redis.debug(DebugParams.OBJECT(pattern));
} catch (Exception e) {
result = "debug fail ! " + e.getMessage();
logger.error("cluster debug is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
return result;
}
/**
* command : delslots
*
* @param redisClusterProxy
* @param slots
* @return
*/
public static synchronized ResultMessage delSlots(RedisClusterProxy redisClusterProxy, Integer... slots) {
ResultMessage result = ResultMessage.buildOK();
Jedis target = null;
JedisSlotBasedConnectionHandlerProxy connectionHandler = redisClusterProxy.getConnectionHandler();
if (slots == null || slots.length < 1) {
return ResultMessage.build(false, "slots is null");
}
List<String> errorInfo = new ArrayList<String>();
for (Integer slot : slots) {
try {
target = connectionHandler.getConnectionFromSlot(slot);
target.clusterDelSlots(slot);
} catch (Exception e) {
errorInfo.add(slot + " is failed !\t" + e.getMessage());
logger.error("cluster delSlots is error.", e);
} finally {
if (target != null && target.isConnected()) {
target.close();
}
}
}
if (errorInfo.size() > 0) {
result.setResult(false);
result.setInfos(errorInfo);
}
return result;
}
/**
* command : addSlots
*
* @param redisClusterProxy
* @param slots
* @return
*/
public static synchronized ResultMessage addSlots(RedisClusterProxy redisClusterProxy, Integer... slots) {
ResultMessage result = ResultMessage.buildOK();
Jedis target = null;
JedisSlotBasedConnectionHandlerProxy connectionHandler = redisClusterProxy.getConnectionHandler();
if (slots == null || slots.length < 1) {
return ResultMessage.build(false, "slots is null");
}
List<String> errorInfo = new ArrayList<String>();
for (Integer slot : slots) {
try {
target = connectionHandler.getConnectionFromSlot(slot);
target.clusterAddSlots(slot);
} catch (Exception e) {
errorInfo.add(slot + " is failed !\t" + e.getMessage());
logger.error("cluster addSlots is error.", e);
} finally {
if (target != null && target.isConnected()) {
target.close();
}
}
}
if (errorInfo.size() > 0) {
result.setResult(false);
result.setInfos(errorInfo);
}
return result;
}
/**
* command : meet
*
* @param redisClusterProxy
* @param host
* @param port
* @return
*/
public static synchronized ResultMessage meet(RedisClusterProxy redisClusterProxy, String host, Integer port) {
ResultMessage resultMessage = ResultMessage.buildOK();
Jedis target = null;
try {
JedisSlotBasedConnectionHandlerProxy connectionHandler = redisClusterProxy.getConnectionHandler();
Map<String, JedisPool> jedisPools = connectionHandler.getNodes();
Iterator<JedisPool> i$ = jedisPools.values().iterator();
for (; i$.hasNext(); ) {
JedisPool jedisPool = i$.next();
target = jedisPool.getResource();
target.clusterMeet(host, port);
target.close();
}
} catch (Exception e) {
if (target != null) {
target.close();
}
resultMessage.setResult(false);
resultMessage.setInfos(e.getMessage());
logger.error("cluster meet is error.", e);
}
return resultMessage;
}
/**
* command : forget
*
* @param redisClusterProxy
* @param nodeId
* @return
*/
public static synchronized ResultMessage forget(RedisClusterProxy redisClusterProxy, String nodeId) {
ResultMessage resultMessage = ResultMessage.buildOK();
Jedis target = null;
try {
JedisSlotBasedConnectionHandlerProxy connectionHandler = redisClusterProxy.getConnectionHandler();
Map<String, JedisPool> jedisPools = connectionHandler.getNodes();
Iterator<JedisPool> i$ = jedisPools.values().iterator();
for (; i$.hasNext(); ) {
JedisPool jedisPool = i$.next();
if (!RedisHelper.getNodeId(jedisPool.getResource().clusterNodes()).equals(nodeId)) { // not del selt
target = jedisPool.getResource();
target.clusterForget(nodeId);
}
}
} catch (Exception e) {
resultMessage.setResult(false);
resultMessage.setInfos(e.getMessage());
logger.error("cluster forget is error.", e);
} finally {
if (target != null) {
target.close();
}
}
return resultMessage;
}
/**
* command : dbSize
*
* @param jedisCluster
* @return
*/
public static synchronized long dbSize(RedisClusterProxy jedisCluster) {
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
long dbSize = 0;
for (String node : clusterNodes.keySet()) {
JedisPool i$ = clusterNodes.get(node);
if (i$ == null || i$.isClosed()) {
logger.warn("Pool is null or Could not get a resource from the pool !");
return -1;
}
Jedis redis = i$.getResource();
OutputStream a = new OutputStream() {
@Override
public void write(int b) throws IOException {
}
};
String nodesInfo = redis.info("replication");
if (nodesInfo.contains("role:master")) {
dbSize += redis.dbSize();
}
}
return dbSize;
} catch (Exception e) {
logger.error("cluster dbSize error. ", e);
}
return -1;
}
/**
* command : importing migrating
* It is best to scan the content of all key before the migration
*
* @param jedisCluster
* @param slot
* @param targetAddress ip:port
* @return
*/
public static synchronized boolean moveSlot(RedisClusterProxy jedisCluster, int slot, String targetAddress) {
boolean result = true;
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
Jedis s$ = jedisCluster.getConnectionHandler().getConnectionFromSlot(slot);
JedisPool t$ = clusterNodes.get(targetAddress);
if (s$ == null || !s$.isConnected() || t$ == null || t$.isClosed()) {
logger.warn(s$ + " is cloesd ! or" + t$ + " is closed !");
return false;
}
String r1$ = t$.getResource().clusterSetSlotImporting(slot, RedisHelper.getNodeId(s$.clusterNodes()));
logger.debug("importing is " + r1$ + "!");
String r2$ = s$.clusterSetSlotMigrating(slot, RedisHelper.getNodeId(t$.getResource().clusterNodes()));
logger.debug("migrating is " + r2$ + "!");
} catch (Exception e) {
result = false;
logger.error("cluster moveSlot is error.", e);
}
return result;
}
/**
* command : clusterReplicate
*
* @param jedisCluster
* @param sourceAddress
* @param targetAddress
* @return
*/
public static synchronized boolean replicate(RedisClusterProxy jedisCluster, String sourceAddress, String targetAddress) {
boolean result = true;
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
JedisPool s$ = clusterNodes.get(sourceAddress);
JedisPool t$ = clusterNodes.get(targetAddress);
if (s$ == null || s$.isClosed() || t$ == null || t$.isClosed()) {
logger.warn(s$ + " is cloesd ! or" + t$ + " is closed !");
return false;
}
t$.getResource().clusterReplicate(RedisHelper.getNodeId(s$.getResource().clusterNodes()));
} catch (Exception e) {
result = false;
logger.error("cluster replicate is error.", e);
}
return result;
}
/**
* command : slots
*
* @param jedisCluster
* @return
*/
public static synchronized List<NodeSlots> slots(RedisClusterProxy jedisCluster) {
List<NodeSlots> nodeSlots = new ArrayList<NodeSlots>();
try {
JedisSlotBasedConnectionHandlerProxy connectionHandler = jedisCluster.getConnectionHandler();
Jedis j$ = connectionHandler.getConnection();
List<Object> clusterSlots = j$.clusterSlots();
for (Object clusterSlot : clusterSlots) {
List<Object> list = (List<Object>) clusterSlot;
List<Object> master = (List<Object>) list.get(2);
nodeSlots.add(new NodeSlots((Long) list.get(0), (Long) list.get(1), new String((byte[]) master.get(0)), (Long) master.get(1)));
}
} catch (Exception e) {
logger.error("cluster slots is error.", e);
return null;
}
return nodeSlots;
}
/**
* command : setslot stable
*
* @param jedisCluster
* @return
*/
public static synchronized boolean slotsStable(RedisClusterProxy jedisCluster, Integer... slots) {
boolean result = true;
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for (String node : clusterNodes.keySet()) {
JedisPool i$ = clusterNodes.get(node);
Jedis redis = i$.getResource();
for (Integer slot : slots) {
redis.clusterSetSlotStable(slot);
}
}
} catch (Exception e) {
result = false;
logger.error("cluster setslots stable is error.", e);
}
return result;
}
/**
* command : cluster getKeysinslot
*
* @param jedisCluster
* @param slot
* @param count
* @return
*/
public static synchronized List<String> getKeysInSlot(RedisClusterProxy jedisCluster, Integer slot, Integer count) {
try {
Jedis r$ = jedisCluster.getConnectionHandler().getConnectionFromSlot(slot);
return r$.clusterGetKeysInSlot(slot, count);
} catch (Exception e) {
logger.error("cluster getKeysInSlot is error.", e);
}
return null;
}
/**
* get slot by key
*
* @param jedisCluster
* @param key
* @return
*/
public static int getSlotByKey(RedisClusterProxy jedisCluster, String key) {
Map<String, JedisPool> jps$ = jedisCluster.getClusterNodes();
for (JedisPool j$ : jps$.values()) {
Long slot = j$.getResource().clusterKeySlot(key);
return slot.intValue();
}
return -1;
}
/**
* command : dump
* Get the value after the serialization of the specified Key
*
* @param jedisCluster
* @param key
* @return
*/
public static byte[] dump(RedisClusterProxy jedisCluster, String key) {
Jedis r$ = null;
try {
int slot = getSlotByKey(jedisCluster, key);
r$ = jedisCluster.getConnectionHandler().getConnectionFromSlot(slot);
return r$.dump(key);
} catch (Exception e) {
logger.error("cluster dump is error. key:" + key, e);
} finally {
if (null != r$) {
r$.close();
}
}
return null;
}
/**
* command : restore
* De serialize a given serialized value and associate it with a given key.
*
* @param jedisCluster
* @param key
* @return
*/
public static boolean restore(RedisClusterProxy jedisCluster, String key, byte[] bytes) {
boolean result = true;
Jedis r$ = null;
try {
int slot = getSlotByKey(jedisCluster, key);
r$ = jedisCluster.getConnectionHandler().getConnectionFromSlot(slot);
String r = r$.restore(key, 0, bytes);
logger.debug("cluster restore is" + r);
} catch (Exception e) {
result = false;
logger.error("cluster restore is error. key:" + key, e);
} finally {
if (null != r$) {
r$.close();
}
}
return result;
}
/**
* command : slowlogGet
*
* @param jedisCluster
* @return
*/
public static List<Slowlog> slowlogGet(RedisClusterProxy jedisCluster) {
List<Slowlog> result = new ArrayList<Slowlog>();
Jedis redis = null;
try {
Map<String, JedisPool> jps$ = jedisCluster.getClusterNodes();
for (JedisPool j$ : jps$.values()) {
redis = j$.getResource();
List<Slowlog> slowlogs = redis.slowlogGet();
result.addAll(slowlogs);
}
logger.debug("cluster slowlogGet is OK. size:" + result.size());
return result;
} catch (Exception e) {
logger.error("cluster slowlogGet is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
return null;
}
/**
* command : slowlogLen
*
* @param jedisCluster
* @return
*/
public static long slowlogLen(RedisClusterProxy jedisCluster) {
long result = 0;
Jedis redis = null;
try {
Map<String, JedisPool> jps$ = jedisCluster.getClusterNodes();
for (JedisPool j$ : jps$.values()) {
redis = j$.getResource();
result += redis.slowlogLen();
}
logger.debug("cluster slowlogLen is OK.");
return result;
} catch (Exception e) {
logger.error("cluster slowlogLen is error.", e);
} finally {
if (redis != null) {
redis.close();
}
}
return -1;
}
/**
* command : sunion
*
* @param jedisCluster
* @return
*/
public static Set<String> sunion(RedisClusterProxy jedisCluster, String... keys) {
Set<String> result = new HashSet<String>();
try {
for (String key : keys) {
Set<String> m$ = jedisCluster.smembers(key);
result.addAll(m$);
}
logger.debug("cluster sunion is OK.");
} catch (Exception e) {
logger.error("cluster sunion is error. keys:" + Arrays.toString(keys), e);
}
return result;
}
/**
* command : sunionstore
*
* @param jedisCluster
* @param targetKey
* @param sourceKeys
* @return
*/
public static boolean sunionstore(RedisClusterProxy jedisCluster, String targetKey, String... sourceKeys) {
boolean result = true;
try {
Set<String> values = sunion(jedisCluster, sourceKeys);
Long count = jedisCluster.sadd(targetKey, values.toArray(new String[]{}));
logger.debug("cluster sunionstore is OK. count:" + count);
} catch (Exception e) {
result = false;
logger.error("cluster sunionstore is error. targetKey:" + targetKey + ",sourceKeys:" + Arrays.toString(sourceKeys), e);
}
return result;
}
/**
* command : monitor
*
* @param jedisCluster
* @return
*/
public static synchronized void monitor(RedisClusterProxy jedisCluster, final JedisMonitor monitor, long time) {
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for (String node : clusterNodes.keySet()) {
JedisPool i$ = clusterNodes.get(node);
final Jedis redis = i$.getResource();
new Thread(new Runnable() {
public void run() {
redis.monitor(monitor);
}
}).start();
System.out.println(node);
}
Thread.sleep(time);
} catch (Exception e) {
logger.error("cluster monitor is error.", e);
}
}
/**
* command : monitor
*
* @param jedisCluster
* @param time
* @return
*/
public static synchronized void monitor(final RedisClusterProxy jedisCluster, long time) {
final JedisMonitorProxy monitor = new JedisMonitorProxy() {
public void onCommand(String command, String hostAndPort) {
logger.info(hostAndPort + "--" + command);
}
};
try {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for (String node : clusterNodes.keySet()) {
JedisPool i$ = clusterNodes.get(node);
final Jedis redis = i$.getResource();
new Thread(new Runnable() {
public void run() {
redis.monitor(monitor);
}
}).start();
}
Thread.sleep(time);
} catch (Exception e) {
logger.error("cluster monitor is error.", e);
}
}
// time
}
NodeSlots:
package com.hyr.redis;
/*******************************************************************************
* @date 2018-03-02 下午 4:55
* @author: <a href=mailto:>黄跃然</a>
* @Description: cluster node slots info
******************************************************************************/
public class NodeSlots {
private Long start;
private Long end;
private String ip;
private Long port;
public NodeSlots(Long start, Long end, String ip, Long port) {
this.start = start;
this.end = end;
this.ip = ip;
this.port = port;
}
public Long getStart() {
return start;
}
public Long getEnd() {
return end;
}
public String getIp() {
return ip;
}
public Long getPort() {
return port;
}
@Override
public String toString() {
return "NodeSlots{" +
"start=" + start +
", end=" + end +
", ip='" + ip + '\'' +
", port=" + port +
'}';
}
}
monitor代理:
package com.hyr.redis;
/*******************************************************************************
* @date 2018-03-20 上午 11:35
* @author: <a href=mailto:>黄跃然</a>
* @Description: monitor代理
******************************************************************************/
import redis.clients.jedis.Client;
import redis.clients.jedis.JedisMonitor;
public abstract class JedisMonitorProxy extends JedisMonitor {
protected Client client;
public JedisMonitorProxy() {
}
public void proceed(Client client) {
this.client = client;
this.client.setTimeoutInfinite();
do {
String command = client.getBulkReply();
String hostAndPort = client.getHost() + ":" + client.getPort();
this.onCommand(command, hostAndPort);
} while (client.isConnected());
}
public void onCommand(String s) {
}
public abstract void onCommand(String command, String hostAndPort);
}
RedisHelper:
package com.hyr.redis.help;
/*******************************************************************************
* @date 2018-03-01 下午 2:11
* @author: <a href=mailto:>黄跃然</a>
* @Description:
******************************************************************************/
public class RedisHelper {
/**
* get node id string
*
* @param nodeInfos
* @return
*/
public static String getNodeId(String nodeInfos) {
for (String infoLine : nodeInfos.split("\n")) {
if (infoLine.contains("myself")) {
return infoLine.split(" ")[0];
}
}
return "";
}
}
ResultMessage:
package com.hyr.redis.message;
import java.util.Arrays;
import java.util.List;
/*******************************************************************************
* @date 2018-03-01 下午 3:08
* @author: <a href=mailto:>黄跃然</a>
* @Description:
******************************************************************************/
public class ResultMessage {
private boolean result;
private List<String> infos;
public ResultMessage(boolean result, List<String> infos) {
this.result = result;
this.infos = infos;
}
public ResultMessage(boolean result, String... infos) {
this.result = result;
this.infos = Arrays.asList(infos);
}
public ResultMessage() {
}
public boolean isResult() {
return result;
}
public void setResult(boolean result) {
this.result = result;
}
public List<String> getInfos() {
return infos;
}
public void setInfos(List<String> infos) {
this.infos = infos;
}
public void setInfos(String... infos) {
this.infos = Arrays.asList(infos);
}
public static ResultMessage build() {
return new ResultMessage();
}
public static ResultMessage buildOK() {
return new ResultMessage(true, Arrays.asList("ok!"));
}
public static ResultMessage build(Boolean result, List<String> infos) {
return new ResultMessage(result, infos);
}
public static ResultMessage build(Boolean result, String... infos) {
return new ResultMessage(result, infos);
}
@Override
public String toString() {
return "ResultMessage{" +
"result=" + result +
", infos=" + infos +
'}';
}
}
RedisClusterProxy:
package com.hyr.redis;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import java.io.Closeable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/*******************************************************************************
* @date 2018-02-28 下午 5:45
* @author: <a href=mailto:>黄跃然</a>
* @Description:
******************************************************************************/
public class RedisClusterProxy implements JedisCommands, BasicCommands, Closeable {
public static final short HASHSLOTS = 16384;
private static final int DEFAULT_TIMEOUT = 2000;
private static final int DEFAULT_MAX_REDIRECTIONS = 5;
private int maxRedirections;
private JedisSlotBasedConnectionHandlerProxy connectionHandler;
public RedisClusterProxy(Set<HostAndPort> nodes, int timeout) {
this(nodes, timeout, 5);
}
public RedisClusterProxy(Set<HostAndPort> nodes) {
this(nodes, 2000);
}
public RedisClusterProxy(Set<HostAndPort> nodes, int timeout, int maxRedirections) {
this(nodes, timeout, maxRedirections, new GenericObjectPoolConfig());
}
public RedisClusterProxy(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig) {
this(nodes, 2000, 5, poolConfig);
}
public RedisClusterProxy(Set<HostAndPort> nodes, int timeout, GenericObjectPoolConfig poolConfig) {
this(nodes, timeout, 5, poolConfig);
}
public RedisClusterProxy(Set<HostAndPort> jedisClusterNode, int timeout, int maxRedirections, GenericObjectPoolConfig poolConfig) {
this.connectionHandler = new JedisSlotBasedConnectionHandlerProxy(jedisClusterNode, poolConfig, timeout);
this.maxRedirections = maxRedirections;
}
public void close() {
if (this.connectionHandler != null) {
Iterator i$ = this.connectionHandler.getNodes().values().iterator();
while (i$.hasNext()) {
JedisPool pool = (JedisPool) i$.next();
try {
if (pool != null) {
pool.destroy();
}
} catch (Exception var4) {
;
}
}
}
}
public String set(final String key, final String value) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.set(key, value);
}
}).run(key);
}
public String set(final String key, final String value, final String nxxx, final String expx, final long time) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.set(key, value, nxxx, expx, time);
}
}).run(key);
}
public String get(final String key) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.get(key);
}
}).run(key);
}
public Boolean exists(final String key) {
return (Boolean) (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxRedirections) {
public Boolean execute(Jedis connection) {
return connection.exists(key);
}
}).run(key);
}
public Long persist(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.persist(key);
}
}).run(key);
}
public String type(final String key) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.type(key);
}
}).run(key);
}
public Long expire(final String key, final int seconds) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.expire(key, seconds);
}
}).run(key);
}
public Long pexpire(final String key, final long milliseconds) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.pexpire(key, milliseconds);
}
}).run(key);
}
public Long expireAt(final String key, final long unixTime) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.expireAt(key, unixTime);
}
}).run(key);
}
public Long pexpireAt(final String key, final long millisecondsTimestamp) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.pexpireAt(key, millisecondsTimestamp);
}
}).run(key);
}
public Long ttl(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.ttl(key);
}
}).run(key);
}
public Boolean setbit(final String key, final long offset, final boolean value) {
return (Boolean) (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxRedirections) {
public Boolean execute(Jedis connection) {
return connection.setbit(key, offset, value);
}
}).run(key);
}
public Boolean setbit(final String key, final long offset, final String value) {
return (Boolean) (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxRedirections) {
public Boolean execute(Jedis connection) {
return connection.setbit(key, offset, value);
}
}).run(key);
}
public Boolean getbit(final String key, final long offset) {
return (Boolean) (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxRedirections) {
public Boolean execute(Jedis connection) {
return connection.getbit(key, offset);
}
}).run(key);
}
public Long setrange(final String key, final long offset, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.setrange(key, offset, value);
}
}).run(key);
}
public String getrange(final String key, final long startOffset, final long endOffset) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.getrange(key, startOffset, endOffset);
}
}).run(key);
}
public String getSet(final String key, final String value) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.getSet(key, value);
}
}).run(key);
}
public Long setnx(final String key, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.setnx(key, value);
}
}).run(key);
}
public String setex(final String key, final int seconds, final String value) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.setex(key, seconds, value);
}
}).run(key);
}
public Long decrBy(final String key, final long integer) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.decrBy(key, integer);
}
}).run(key);
}
public Long decr(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.decr(key);
}
}).run(key);
}
public Long incrBy(final String key, final long integer) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.incrBy(key, integer);
}
}).run(key);
}
public Long incr(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.incr(key);
}
}).run(key);
}
public Long append(final String key, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.append(key, value);
}
}).run(key);
}
public String substr(final String key, final int start, final int end) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.substr(key, start, end);
}
}).run(key);
}
public Long hset(final String key, final String field, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.hset(key, field, value);
}
}).run(key);
}
public String hget(final String key, final String field) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.hget(key, field);
}
}).run(key);
}
public Long hsetnx(final String key, final String field, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.hsetnx(key, field, value);
}
}).run(key);
}
public String hmset(final String key, final Map<String, String> hash) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.hmset(key, hash);
}
}).run(key);
}
public List<String> hmget(final String key, final String... fields) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.hmget(key, fields);
}
}).run(key);
}
public Long hincrBy(final String key, final String field, final long value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.hincrBy(key, field, value);
}
}).run(key);
}
public Boolean hexists(final String key, final String field) {
return (Boolean) (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxRedirections) {
public Boolean execute(Jedis connection) {
return connection.hexists(key, field);
}
}).run(key);
}
public Long hdel(final String key, final String... field) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.hdel(key, field);
}
}).run(key);
}
public Long hlen(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.hlen(key);
}
}).run(key);
}
public Set<String> hkeys(final String key) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.hkeys(key);
}
}).run(key);
}
public List<String> hvals(final String key) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.hvals(key);
}
}).run(key);
}
public Map<String, String> hgetAll(final String key) {
return (Map) (new JedisClusterCommand<Map<String, String>>(this.connectionHandler, this.maxRedirections) {
public Map<String, String> execute(Jedis connection) {
return connection.hgetAll(key);
}
}).run(key);
}
public Long rpush(final String key, final String... string) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.rpush(key, string);
}
}).run(key);
}
public Long lpush(final String key, final String... string) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.lpush(key, string);
}
}).run(key);
}
public Long llen(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.llen(key);
}
}).run(key);
}
public List<String> lrange(final String key, final long start, final long end) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.lrange(key, start, end);
}
}).run(key);
}
public String ltrim(final String key, final long start, final long end) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.ltrim(key, start, end);
}
}).run(key);
}
public String lindex(final String key, final long index) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.lindex(key, index);
}
}).run(key);
}
public String lset(final String key, final long index, final String value) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.lset(key, index, value);
}
}).run(key);
}
public Long lrem(final String key, final long count, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.lrem(key, count, value);
}
}).run(key);
}
public String lpop(final String key) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.lpop(key);
}
}).run(key);
}
public String rpop(final String key) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.rpop(key);
}
}).run(key);
}
public Long sadd(final String key, final String... member) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.sadd(key, member);
}
}).run(key);
}
public Set<String> smembers(final String key) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.smembers(key);
}
}).run(key);
}
public Long srem(final String key, final String... member) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.srem(key, member);
}
}).run(key);
}
public String spop(final String key) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.spop(key);
}
}).run(key);
}
public Long scard(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.scard(key);
}
}).run(key);
}
public Boolean sismember(final String key, final String member) {
return (Boolean) (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxRedirections) {
public Boolean execute(Jedis connection) {
return connection.sismember(key, member);
}
}).run(key);
}
public String srandmember(final String key) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.srandmember(key);
}
}).run(key);
}
public List<String> srandmember(final String key, final int count) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.srandmember(key, count);
}
}).run(key);
}
public Long strlen(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.strlen(key);
}
}).run(key);
}
public Long zadd(final String key, final double score, final String member) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zadd(key, score, member);
}
}).run(key);
}
public Long zadd(final String key, final Map<String, Double> scoreMembers) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zadd(key, scoreMembers);
}
}).run(key);
}
public Set<String> zrange(final String key, final long start, final long end) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrange(key, start, end);
}
}).run(key);
}
public Long zrem(final String key, final String... member) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zrem(key, member);
}
}).run(key);
}
public Double zincrby(final String key, final double score, final String member) {
return (Double) (new JedisClusterCommand<Double>(this.connectionHandler, this.maxRedirections) {
public Double execute(Jedis connection) {
return connection.zincrby(key, score, member);
}
}).run(key);
}
public Long zrank(final String key, final String member) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zrank(key, member);
}
}).run(key);
}
public Long zrevrank(final String key, final String member) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zrevrank(key, member);
}
}).run(key);
}
public Set<String> zrevrange(final String key, final long start, final long end) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrevrange(key, start, end);
}
}).run(key);
}
public Set<Tuple> zrangeWithScores(final String key, final long start, final long end) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrangeWithScores(key, start, end);
}
}).run(key);
}
public Set<Tuple> zrevrangeWithScores(final String key, final long start, final long end) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrevrangeWithScores(key, start, end);
}
}).run(key);
}
public Long zcard(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zcard(key);
}
}).run(key);
}
public Double zscore(final String key, final String member) {
return (Double) (new JedisClusterCommand<Double>(this.connectionHandler, this.maxRedirections) {
public Double execute(Jedis connection) {
return connection.zscore(key, member);
}
}).run(key);
}
public List<String> sort(final String key) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.sort(key);
}
}).run(key);
}
public List<String> sort(final String key, final SortingParams sortingParameters) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.sort(key, sortingParameters);
}
}).run(key);
}
public Long zcount(final String key, final double min, final double max) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zcount(key, min, max);
}
}).run(key);
}
public Long zcount(final String key, final String min, final String max) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zcount(key, min, max);
}
}).run(key);
}
public Set<String> zrangeByScore(final String key, final double min, final double max) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrangeByScore(key, min, max);
}
}).run(key);
}
public Set<String> zrangeByScore(final String key, final String min, final String max) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrangeByScore(key, min, max);
}
}).run(key);
}
public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrevrangeByScore(key, max, min);
}
}).run(key);
}
public Set<String> zrangeByScore(final String key, final double min, final double max, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrangeByScore(key, min, max, offset, count);
}
}).run(key);
}
public Set<String> zrevrangeByScore(final String key, final String max, final String min) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrevrangeByScore(key, max, min);
}
}).run(key);
}
public Set<String> zrangeByScore(final String key, final String min, final String max, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrangeByScore(key, min, max, offset, count);
}
}).run(key);
}
public Set<String> zrevrangeByScore(final String key, final double max, final double min, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrevrangeByScore(key, max, min, offset, count);
}
}).run(key);
}
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrangeByScoreWithScores(key, min, max);
}
}).run(key);
}
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrevrangeByScoreWithScores(key, max, min);
}
}).run(key);
}
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrangeByScoreWithScores(key, min, max, offset, count);
}
}).run(key);
}
public Set<String> zrevrangeByScore(final String key, final String max, final String min, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrevrangeByScore(key, max, min, offset, count);
}
}).run(key);
}
public Set<Tuple> zrangeByScoreWithScores(final String key, final String min, final String max) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrangeByScoreWithScores(key, min, max);
}
}).run(key);
}
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final String max, final String min) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrevrangeByScoreWithScores(key, max, min);
}
}).run(key);
}
public Set<Tuple> zrangeByScoreWithScores(final String key, final String min, final String max, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrangeByScoreWithScores(key, min, max, offset, count);
}
}).run(key);
}
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
}).run(key);
}
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final String max, final String min, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<Tuple>>(this.connectionHandler, this.maxRedirections) {
public Set<Tuple> execute(Jedis connection) {
return connection.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
}).run(key);
}
public Long zremrangeByRank(final String key, final long start, final long end) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zremrangeByRank(key, start, end);
}
}).run(key);
}
public Long zremrangeByScore(final String key, final double start, final double end) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zremrangeByScore(key, start, end);
}
}).run(key);
}
public Long zremrangeByScore(final String key, final String start, final String end) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zremrangeByScore(key, start, end);
}
}).run(key);
}
public Long zlexcount(final String key, final String min, final String max) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zlexcount(key, min, max);
}
}).run(key);
}
public Set<String> zrangeByLex(final String key, final String min, final String max) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrangeByLex(key, min, max);
}
}).run(key);
}
public Set<String> zrangeByLex(final String key, final String min, final String max, final int offset, final int count) {
return (Set) (new JedisClusterCommand<Set<String>>(this.connectionHandler, this.maxRedirections) {
public Set<String> execute(Jedis connection) {
return connection.zrangeByLex(key, min, max, offset, count);
}
}).run(key);
}
public Long zremrangeByLex(final String key, final String min, final String max) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.zremrangeByLex(key, min, max);
}
}).run(key);
}
public Long linsert(final String key, final LIST_POSITION where, final String pivot, final String value) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.linsert(key, where, pivot, value);
}
}).run(key);
}
public Long lpushx(final String key, final String... string) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.lpushx(key, string);
}
}).run(key);
}
public Long rpushx(final String key, final String... string) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.rpushx(key, string);
}
}).run(key);
}
/**
* @deprecated
*/
@Deprecated
public List<String> blpop(final String arg) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.blpop(arg);
}
}).run(arg);
}
/**
* @deprecated
*/
@Deprecated
public List<String> brpop(final String arg) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.brpop(arg);
}
}).run(arg);
}
public Long del(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.del(key);
}
}).run(key);
}
public String echo(final String string) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.echo(string);
}
}).run((String) null);
}
public Long move(final String key, final int dbIndex) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.move(key, dbIndex);
}
}).run(key);
}
public Long bitcount(final String key) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.bitcount(key);
}
}).run(key);
}
public Long bitcount(final String key, final long start, final long end) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.bitcount(key, start, end);
}
}).run(key);
}
/**
* @deprecated
*/
@Deprecated
public String ping() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.ping();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String quit() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.quit();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String flushDB() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.flushDB();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public Long dbSize() {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.dbSize();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String select(final int index) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.select(index);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String flushAll() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.flushAll();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String auth(final String password) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.auth(password);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String save() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.save();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String bgsave() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.bgsave();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String bgrewriteaof() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.bgrewriteaof();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public Long lastsave() {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.lastsave();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String shutdown() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.shutdown();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String info() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.info();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String info(final String section) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.info(section);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String slaveof(final String host, final int port) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.slaveof(host, port);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String slaveofNoOne() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.slaveofNoOne();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public Long getDB() {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.getDB();
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String debug(final DebugParams params) {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.debug(params);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public String configResetStat() {
return (String) (new JedisClusterCommand<String>(this.connectionHandler, this.maxRedirections) {
public String execute(Jedis connection) {
return connection.configResetStat();
}
}).run((String) null);
}
public Map<String, JedisPool> getClusterNodes() {
return this.connectionHandler.getNodes();
}
/**
* @deprecated
*/
@Deprecated
public Long waitReplicas(int replicas, long timeout) {
return null;
}
/**
* @deprecated
*/
@Deprecated
public ScanResult<Entry<String, String>> hscan(final String key, final int cursor) {
return (ScanResult) (new JedisClusterCommand<ScanResult<Entry<String, String>>>(this.connectionHandler, this.maxRedirections) {
public ScanResult<Entry<String, String>> execute(Jedis connection) {
return connection.hscan(key, cursor);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public ScanResult<String> sscan(final String key, final int cursor) {
return (ScanResult) (new JedisClusterCommand<ScanResult<String>>(this.connectionHandler, this.maxRedirections) {
public ScanResult<String> execute(Jedis connection) {
return connection.sscan(key, cursor);
}
}).run((String) null);
}
/**
* @deprecated
*/
@Deprecated
public ScanResult<Tuple> zscan(final String key, final int cursor) {
return (ScanResult) (new JedisClusterCommand<ScanResult<Tuple>>(this.connectionHandler, this.maxRedirections) {
public ScanResult<Tuple> execute(Jedis connection) {
return connection.zscan(key, cursor);
}
}).run((String) null);
}
public ScanResult<Entry<String, String>> hscan(final String key, final String cursor) {
return (ScanResult) (new JedisClusterCommand<ScanResult<Entry<String, String>>>(this.connectionHandler, this.maxRedirections) {
public ScanResult<Entry<String, String>> execute(Jedis connection) {
return connection.hscan(key, cursor);
}
}).run(key);
}
public ScanResult<String> sscan(final String key, final String cursor) {
return (ScanResult) (new JedisClusterCommand<ScanResult<String>>(this.connectionHandler, this.maxRedirections) {
public ScanResult<String> execute(Jedis connection) {
return connection.sscan(key, cursor);
}
}).run(key);
}
public ScanResult<Tuple> zscan(final String key, final String cursor) {
return (ScanResult) (new JedisClusterCommand<ScanResult<Tuple>>(this.connectionHandler, this.maxRedirections) {
public ScanResult<Tuple> execute(Jedis connection) {
return connection.zscan(key, cursor);
}
}).run(key);
}
public Long pfadd(final String key, final String... elements) {
return (Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.pfadd(key, elements);
}
}).run(key);
}
public long pfcount(final String key) {
return ((Long) (new JedisClusterCommand<Long>(this.connectionHandler, this.maxRedirections) {
public Long execute(Jedis connection) {
return connection.pfcount(key);
}
}).run(key)).longValue();
}
public List<String> blpop(final int timeout, final String key) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.blpop(timeout, key);
}
}).run(key);
}
public List<String> brpop(final int timeout, final String key) {
return (List) (new JedisClusterCommand<List<String>>(this.connectionHandler, this.maxRedirections) {
public List<String> execute(Jedis connection) {
return connection.brpop(timeout, key);
}
}).run(key);
}
public static enum Reset {
SOFT,
HARD;
private Reset() {
}
}
/**
* get handler
*
* @return
*/
public JedisSlotBasedConnectionHandlerProxy getConnectionHandler() {
return connectionHandler;
}
}
JedisSlotBasedConnectionHandlerProxy:
package com.hyr.redis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisSlotBasedConnectionHandler;
import java.util.Set;
/*******************************************************************************
* @date 2018-03-01 上午 9:57
* @author: <a href=mailto:>黄跃然</a>
* @Description:
******************************************************************************/
public class JedisSlotBasedConnectionHandlerProxy extends JedisSlotBasedConnectionHandler{
public JedisSlotBasedConnectionHandlerProxy(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig, int timeout) {
super(nodes, poolConfig, timeout);
}
}
maven依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.3</version>
</dependency>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
测试用的代码:
import com.hyr.redis.JedisMonitorProxy;
import com.hyr.redis.NodeSlots;
import com.hyr.redis.RedisClusterProxy;
import com.hyr.redis.RedisClusterUtils;
import com.hyr.redis.message.ResultMessage;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.*;
import redis.clients.util.Slowlog;
import java.util.*;
/*******************************************************************************
* @date 2018-02-28 下午 5:45
* @author: <a href=mailto>黄跃然</a>
* @Description:
******************************************************************************/
public class TestRedisHelper {
private static Logger log = Logger.getLogger(TestRedisHelper.class);
RedisClusterProxy jedisCluster = null;
@Before
public synchronized void before() {
// jedisCluster = RedisClusterUtils.getRedisClusterInstance("127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006");
jedisCluster = RedisClusterUtils.getRedisClusterInstance("192.168.0.193:7001,192.168.0.193:7002,192.168.0.193:7003");
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
Set<String> hostAndPorts = clusterNodes.keySet();
log.info(hostAndPorts);
}
@After
public void after() {
if (jedisCluster != null) {
jedisCluster.close();
}
}
@Test
public void TEST() {
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for (String node : clusterNodes.keySet()) {
JedisPool jedisPool = clusterNodes.get(node);
if (jedisPool != null && !jedisPool.isClosed()) {
do {
Jedis jedis=null;
Pipeline pipelined=null;
try {
jedis = jedisPool.getResource();
jedis.quit();
pipelined = jedis.pipelined();
pipelined.set("hyra1,", "hyra");
pipelined.set("hyra2,", "hyra");
pipelined.set("hyra3,", "hyra");
pipelined.set("hyra1,", "hyra");
pipelined.set("hyra2,", "hyra");
pipelined.set("hyra3,", "hyra");
pipelined.sync();
} catch (Exception e) {
e.printStackTrace();
}finally {
jedis.close();
}
}while (true);
}
break;
}
}
@Test
public void testKeys() {
TreeSet<String> keys = RedisClusterUtils.keys(jedisCluster, "*");
System.out.println(keys);
}
@Test
public void testInfos() {
String result = RedisClusterUtils.info(jedisCluster);
System.out.println(result);
}
@Test
public void testInfo() {
String result = RedisClusterUtils.info(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.server(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.clients(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.memory(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.persistence(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.state(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.cpu(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.cluster(jedisCluster);
System.out.println(result);
result = RedisClusterUtils.keyspace(jedisCluster);
System.out.println(result);
}
@Test
public void testInfoSection() {
String section = "memory"; // server clients memory persistence state cpu cluster keyspace
String result = RedisClusterUtils.info(jedisCluster, section);
System.out.println(result);
}
@Test
public void testNodes() {
String result = RedisClusterUtils.nodes(jedisCluster);
System.out.println(result);
}
@Test
public void testTransaction() {
boolean result = RedisClusterUtils.transaction(jedisCluster, new RedisClusterUtils.RedisCallBack() {
public List<String> setKey() {
List<String> keys = new ArrayList<String>(); // set all keys to allot slot
keys.add("a4");
keys.add("a5");
keys.add("a6");
return keys;
}
public void OnMultiAndExecListener(Transaction transaction) {
try {
transaction.set("a4", "b5");
transaction.set("a5", "b2");
transaction.set("a6", "b3");
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.println(result);
}
@Test
public void testCall() {
String result = RedisClusterUtils.call(jedisCluster, "return redis.call('TIME')");
System.out.println(result);
}
@Test
public void testPing() {
ResultMessage resultMessage = RedisClusterUtils.ping(jedisCluster);
System.out.println(resultMessage.toString());
}
@Test
public void testRandomKey() {
String key = RedisClusterUtils.randomKey(jedisCluster);
System.out.println(key);
}
@Test
public void testIsKnownNode() {
boolean result = RedisClusterUtils.isKnownNode(jedisCluster, "127.0.0.1", 7001);
System.out.println(result);
boolean result1 = RedisClusterUtils.isKnownNode(jedisCluster, "41baecb1e1b940a869e2a65bc202cbceaed38904");
System.out.println(result1);
boolean result2 = RedisClusterUtils.isKnownNode(jedisCluster, "41baecb1e1b9403a86qwfqwf8904");
System.out.println(result2);
}
@Test
public void testFlushDB() {
boolean result = RedisClusterUtils.flushDB(jedisCluster);
System.out.println(result);
}
@Test
public void testSave() {
boolean result = RedisClusterUtils.save(jedisCluster);
System.out.println(result);
}
@Test
public void testLastSave() {
String result = RedisClusterUtils.lastSave(jedisCluster);
System.out.println(result);
}
@Test
public void testBgRewriteAof() {
boolean result = RedisClusterUtils.bgRewriteAof(jedisCluster);
System.out.println(result);
}
@Test
public void testSlaveOfNoOne() {
boolean result = RedisClusterUtils.slaveOfNoOne(jedisCluster);
System.out.println(result);
}
@Test
public void testBgSave() {
boolean result = RedisClusterUtils.bgSave(jedisCluster);
System.out.println(result);
}
@Test
public void testDebug() {
String result = RedisClusterUtils.debug(jedisCluster, "a1");
System.out.println(result);
}
@Test
public void testDelSlots() {
ResultMessage resultMessage = RedisClusterUtils.delSlots(jedisCluster, 0, 65535);
System.out.println(resultMessage);
}
@Test
public void testAddSlots() {
ResultMessage resultMessage = RedisClusterUtils.addSlots(jedisCluster, 0, 65535);
System.out.println(resultMessage);
}
@Test
public void testMeet() {
ResultMessage result = RedisClusterUtils.meet(jedisCluster, "127.0.0.1", 7005);
System.out.println(result);
}
@Test
public void testForget() {
ResultMessage result = RedisClusterUtils.forget(jedisCluster, "3af9bc9fff7c87f6b9b5753b504d55a5c2307383");
System.out.println(result);
}
@Test
public void testDbSize() {
long result = RedisClusterUtils.dbSize(jedisCluster);
System.out.println(result);
}
@Test
public void testMoveSlot() {
boolean result = RedisClusterUtils.moveSlot(jedisCluster, 7785, "127.0.0.1:7002");
System.out.println(result);
}
@Test
public void testClusterReplicate() {
boolean result = RedisClusterUtils.replicate(jedisCluster, "127.0.0.1:7003", "127.0.0.1:7004");
System.out.println(result);
}
@Test
public void testClusterSlots() {
List<NodeSlots> result = RedisClusterUtils.slots(jedisCluster);
System.out.println(result);
}
@Test
public void testSlotsStable() {
boolean result = RedisClusterUtils.slotsStable(jedisCluster, 7785);
System.out.println(result);
}
@Test
public void testGetKeysInSlot() {
List<String> keysInSlot = RedisClusterUtils.getKeysInSlot(jedisCluster, 7785, 100);
System.out.println(keysInSlot);
}
@Test
public void testDump() {
byte[] result = RedisClusterUtils.dump(jedisCluster, "a1");
assert result != null;
System.out.println(new String(result));
}
@Test
public void testRestore() {
boolean result = RedisClusterUtils.restore(jedisCluster, "gg1", RedisClusterUtils.dump(jedisCluster, "a1"));
System.out.println(result);
}
@Test
public void testSlowlogGet() {
List<Slowlog> result = RedisClusterUtils.slowlogGet(jedisCluster);
assert result != null;
for (Slowlog slowlog : result) {
System.out.println(slowlog.getId() + "-" + slowlog.getTimeStamp() + "-" + slowlog.getExecutionTime() + "-" + slowlog.getArgs());
}
}
@Test
public void testSlowlogLen() {
long result = RedisClusterUtils.slowlogLen(jedisCluster);
System.out.println(result);
}
@Test
public void testSunion() {
Set<String> result = RedisClusterUtils.sunion(jedisCluster, "sb2", "sb3", "sb1", "sa1");
System.out.println(result);
}
@Test
public void testSunionstore() {
boolean result = RedisClusterUtils.sunionstore(jedisCluster, "sunb1", "sb3", "sb1", "sb2");
System.out.println(result);
}
@Test
public void testMonitor() {
JedisMonitor monitor = new JedisMonitor() {
@Override
public void onCommand(String s) {
System.out.println(s);
}
};
JedisMonitorProxy monitorProxy = new JedisMonitorProxy() {
public void onCommand(String command, String hostAndPort) {
System.out.println(hostAndPort + "--" + command);
}
};
//RedisClusterUtils.monitor(jedisCluster,monitorProxy,1000000);
RedisClusterUtils.monitor(jedisCluster, 1000000);
}
}
项目地址:https://github.com/huangyueranbbc/RedisUtils