package com.alibaba.otter.canal.example;
import java.util.HashSet;
//import java.util.LinkedHashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
//import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
//import redis.clients.jedis.JedisPool;
//import redis.clients.jedis.JedisPoolConfig;
public class ClusterUtil {
// 访问密码
// private static String AUTH = "admin";
// 过期时间
protected static int expireTime = 660 * 660 * 24;
// private static String host="10.0.76.192";
private static String host="127.0.0.1";
private static JedisCluster cluster;
static {
// 只给集群里一个实例就可以
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort(host, 7000));
jedisClusterNodes.add(new HostAndPort(host, 7001));
jedisClusterNodes.add(new HostAndPort(host, 7002));
jedisClusterNodes.add(new HostAndPort(host, 7003));
jedisClusterNodes.add(new HostAndPort(host, 7004));
jedisClusterNodes.add(new HostAndPort(host, 7005));
cluster = new JedisCluster(jedisClusterNodes);
}
/**
* 获取jedis实例
*/
protected static synchronized JedisCluster getJedis() {
return cluster;
}
/**
* 释放jedis资源
*
* @param jedis
* @param isBroken
*/
protected static void closeResource(JedisCluster cluster, boolean isBroken) {
}
/**
* 是否存在key
*
* @param key
*/
public static boolean existKey(String key) {
JedisCluster jedis = null;
boolean isBroken = false;
try {
jedis = getJedis();
jedis.select(0);
return jedis.exists(key);
} catch (Exception e) {
isBroken = true;
} finally {
closeResource(jedis, isBroken);
}
return false;
}
/**
* 删除key
*
* @param key
*/
public static void delKey(String key) {
JedisCluster jedis = null;
boolean isBroken = false;
try {
jedis = getJedis();
jedis.select(0);
jedis.del(key);
} catch (Exception e) {
isBroken = true;
} finally {
closeResource(jedis, isBroken);
}
}
/**
* 取得key的值
*
* @param key
*/
public static String stringGet(String key) {
JedisCluster jedis = null;
boolean isBroken = false;
String lastVal = null;
try {
jedis = getJedis();
// jedis.select(0);
lastVal = jedis.get(key);
jedis.expire(key, expireTime);
} catch (Exception e) {
isBroken = true;
} finally {
// closeResource(jedis, isBroken);
}
return lastVal;
}
/**
* 添加string数据
*
* @param key
* @param value
*/
public static String stringSet(String key, String value) {
JedisCluster jedis = null;
boolean isBroken = false;
String lastVal = null;
try {
jedis = getJedis();
// jedis.select(0);
lastVal = jedis.set(key, value);
jedis.expire(key, expireTime);
} catch (Exception e) {
e.printStackTrace();
isBroken = true;
} finally {
closeResource(jedis, isBroken);
}
return lastVal;
}
/**
* 添加hash数据
*
* @param key
* @param field
* @param value
*/
public static void hashSet(String key, String field, String value) {
boolean isBroken = false;
JedisCluster jedis = null;
try {
jedis = getJedis();
if (jedis != null) {
//jedis.select(0);
jedis.hset(key, field, value);
jedis.expire(key, expireTime);
}
} catch (Exception e) {
isBroken = true;
} finally {
//closeResource(jedis, isBroken);
}
}
}