`

JedisClientUtil.java

 
阅读更多

/**************************************JedisClient.java **************************************/

package com.avit.cache.redis;

 

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

 

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

import redis.clients.jedis.ScanParams;

import redis.clients.jedis.ScanResult;

 

import com.avit.util.SysConfig;

 

public class JedisClient {

public static Logger log = LogManager.getLogger(JedisClient.class);

private static JedisPool  jc;

 

public final static ExecutorService batchAddRedisCache = Executors.newFixedThreadPool(40);

 

static {

String nodes = SysConfig.getSystemConfig("redis.user.group.node.ip", "10.18.24.29");

String port = SysConfig.getSystemConfig("redis.user.group.node.port", "6870");

int maxTotal = Integer.parseInt(SysConfig.getSystemConfig("redis.maxTotal", "100"));

int maxIdle = Integer.parseInt(SysConfig.getSystemConfig("redis.maxIdle", "20"));

int minIdle = Integer.parseInt(SysConfig.getSystemConfig("redis.minIdle", "10"));

int maxWaitMillis = Integer.parseInt(SysConfig.getSystemConfig("redis.MaxWaitMillis", "-1"));

 

 

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

// 资源池中最大连接数

jedisPoolConfig.setMaxTotal(maxTotal);

// 资源池允许最大空闲的连接数

jedisPoolConfig.setMaxIdle(maxIdle);

// 资源池确保最少空闲的连接数

jedisPoolConfig.setMinIdle(minIdle);

// 当资源池用尽后,调用者是否要等待。只有当为true时,下面的maxWaitMillis才会生效

jedisPoolConfig.setBlockWhenExhausted(true);

// 当资源池连接用尽后,调用者的最大等待时间(单位为毫秒)

jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

// 向资源池借用连接时是否做连接有效性检测(ping),无效连接会被移除

jedisPoolConfig.setTestOnBorrow(true);

// 向资源池归还连接时是否做连接有效性检测(ping),无效连接会被移除

jedisPoolConfig.setTestOnReturn(false);

 

// 是否开启空闲资源监测

jedisPoolConfig.setTestWhileIdle(true);

// 空闲资源的检测周期(单位为毫秒),-1:不检测

jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30 * 1000);

// 资源池中资源最小空闲时间(单位为毫秒),达到此值后空闲资源将被移除

jedisPoolConfig.setMinEvictableIdleTimeMillis(60 * 1000);

// 做空闲资源检测时,每次的采样数

// 可根据自身应用连接数进行微调,如果设置为-1,就是对所有连接做空闲监测

jedisPoolConfig.setNumTestsPerEvictionRun(-1);

 

jc = new JedisPool(jedisPoolConfig,nodes, Integer.parseInt(port), maxWaitMillis);

}

 

/**

* 将给定集合的并集存储在指定的集合

*/

public static long sunionstore(String dstkey, String... keys) {

Jedis jedis = null;

try {

jedis = jc.getResource();

 

return jedis.sunionstore(dstkey, keys);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

return 0;

}

/*

* 将给定集合的差集存储在指定的集合

*/

public static long sdiffstore(String dstkey, String... keys) {

Jedis jedis = null;

try {

jedis = jc.getResource();

return jedis.sdiffstore(dstkey, keys);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

 

jedis.close();

}

}

return 0;

}

 

/*

* 返回第一个集合中独有的元素

*/

public static Set<String> sdiff(String... keys) {

Jedis jedis = null;

try {

jedis = jc.getResource();

return jedis.sdiff(keys);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

return new HashSet<String>();

}

 

public static void sadd(String key, String member) {

Jedis jedis = null;

try {

jedis = jc.getResource();

jedis.sadd(key, member);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

 

}

 

/*

* 返回集合中的所有的成员

*/

public static Set<String> smembers(String key) {

Jedis jedis = null;

try {

jedis = jc.getResource();

return jedis.smembers(key);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

return new HashSet<String>();

}

/*

* 给主键设置过期时间

*/

public static void expire(String key, int seconds) {

Jedis jedis = null;

try {

jedis = jc.getResource();

jedis.expire(key,seconds);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

}

 

public static Long delete(String... keys) {

Jedis jedis = null;

try {

jedis = jc.getResource();

return jedis.del(keys);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

 

return 0L;

}

 

public static void sadd(String key, String... member) {

Jedis jedis = null;

try {

jedis = jc.getResource();

jedis.sadd(key, member);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

}

 

/*

* 返回集合中元素的数量

*/

public static long scard(String dstkey) {

Jedis jedis = null;

try {

 

jedis = jc.getResource();

Long log1 =  jedis.scard(dstkey);

return log1;

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

return 0L;

}

 

/*

* 返回主键是否存在

*/

public static boolean exists(String msgKey) {

Jedis jedis = null;

try {

jedis = jc.getResource();

return jedis.exists(msgKey);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

 

return false;

}

/*

* 移除集合中的一个或多个成员元素

*/

public static void srem(String key, String value) {

Jedis jedis = null;

try {

jedis = jc.getResource();

jedis.srem(key, value);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

}

/*

* 修改主键名称

*/

public static void rename(String oldKey , String newKey) {

 

Jedis jedis = null;

try {

jedis = jc.getResource();

jedis.rename(oldKey, newKey);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

}

 

/*

* 批量添加,一次插入不超过1024

*/

public static void batchSaddCache(String[] dstUsers, final String key)  {

 

//一次添加上限1024

if(dstUsers.length > 1024)  {

 

List<String> arrays = Arrays.asList(dstUsers);

int pagesize = 1024;

int totalcount = arrays.size();

int pagecount = 0;//总页数

int m = totalcount % pagesize;

if(m > 0){

pagecount = totalcount / pagesize + 1;

}else{

pagecount = totalcount / pagesize;

}

 

try {

 

final CountDownLatch countDownLatch = new CountDownLatch(pagecount);

 

for(int i = 1; i <= pagecount; i++) {

 

List<String> subList = null;

if (m == 0) {

subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

}else{

if(i == pagecount){

subList = arrays.subList((i - 1) * pagesize, totalcount);

}else{

subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

}

}

final List<String> finalSubList = subList;

batchAddRedisCache.submit(new  Runnable() {

public void run() {

try {

String[] strings = new String[finalSubList.size()];

 

finalSubList.toArray(strings);

JedisClient.sadd(key,strings);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

countDownLatch.countDown();

}

 

}

});

}

countDownLatch.await();

} catch (Exception ex) {

log.error("",ex);

 

} else {

 

if(dstUsers.length <= 0) {

 

} else {

JedisClient.sadd(key,dstUsers);

}

}

}

/*

* 如果超过10000条数据,需要循环读取

*/

public static Set<String> scan(String key) {

 

Jedis jedis = null;

try {

 

jedis = jc.getResource();

Long count = jedis.scard(key);

    if(count > 10000) {

      Set<String> result = new HashSet<String>();

      ScanParams scanParams = new ScanParams();

  scanParams.count(2000);

  String counter = ScanParams.SCAN_POINTER_START;

 

  while(true){

  

  ScanResult<String> ret = jedis.sscan(key, counter, scanParams);

  counter = ret.getStringCursor();

  result.addAll(ret.getResult());

  

  if(result.size() >= count || counter.equals("0")) {//遍历完成

  break;

  }

  }

  return result;

    } else {

    return jedis.smembers(key);

    }

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

return new HashSet<String>();

}

/*

* 将给定集合的交集存储在指定的集合

*/

public static Long sinterstore(String key, String... keys) {

Jedis jedis = null;

try {

 

jedis = jc.getResource();

return jedis.sinterstore(key, keys);

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

 

return 0L;

}

/*

* 元素是否是集合的成员

*/

public static boolean sismember(String key, String member) {

 

 

Jedis jedis = null;

try {

jedis = jc.getResource();

return jedis.sismember(key, member);

 

} catch (Exception ex) {

log.error("",ex);

} finally {

if(jedis != null) {

jedis.close();

}

}

 

return false;

}

 

public static void main(String[] args) {

/*

String dstKey = "dstTestKey";

 

 

JedisClient.sadd("testKey1", "nihao");

JedisClient.sadd("testKey1", "hello");

JedisClient.sadd("testKey1", "word");

 

JedisClient.sadd("testKey3", "nihao");

JedisClient.sadd("testKey3", "this is text");

JedisClient.sadd("testKey3", "wo");

 

 

JedisClient.sunionstore(dstKey, new String[] {"testKey1", "testKey3"});

 

Set<String> reuslt1 = JedisClient.smembers(dstKey);

System.out.println(reuslt1);

 

JedisClient.sadd("testKey4", "key4");

JedisClient.sadd("testKey4", "key4 hahah");

JedisClient.sadd("testKey4", "key4 hahawo");

 

        JedisClient.sunionstore(dstKey, new String[] {"testKey4"});

 

Set<String> reuslt3 = JedisClient.smembers(dstKey);

System.out.println(reuslt3);

 

JedisClient.sadd("testKey2", "word");

JedisClient.sadd("testKey2", "xxx");

JedisClient.sadd("testKey2", "test");

 

JedisClient.sdiffstore(dstKey, new String[] {dstKey, "testKey2"});

 

Set<String> reuslt2 = JedisClient.smembers(dstKey);

System.out.println(reuslt2);*/

 

Set<String> set = new HashSet<String>();

set.add("USER_GROUP_CACHE_KEY_A3601099727_0");

set.add("USER_GROUP_CACHE_KEY_A110163759_0");

String bd = "USER_GROUP_CACHE_KEY_"+"A110163759";

for (String a : set) {

boolean flag = a.indexOf(bd) != -1;

System.out.println(flag);

}

System.out.println("jja")

}

}

 

 

 /****************************************JedisClusterClient.java***************************************************/

package com.avit.cache.redis;

 

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

import java.util.concurrent.CountDownLatch;

 

import com.avit.common.queue.TaskConsumer;

import com.avit.util.SysConfig;

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 JedisClusterClient {

 

private static JedisCluster jc;

 

static {

String nodes = SysConfig.getSystemConfig("redis.nodes", "");

int maxTotal = Integer.parseInt(SysConfig.getSystemConfig("redis.maxTotal", "100"));

int maxIdle = Integer.parseInt(SysConfig.getSystemConfig("redis.maxIdle", "20"));

int minIdle = Integer.parseInt(SysConfig.getSystemConfig("redis.minIdle", "10"));

int maxWaitMillis = Integer.parseInt(SysConfig.getSystemConfig("redis.MaxWaitMillis", "-1"));

 

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

for (String node : nodes.split(",")) {

String host = node.split(":")[0];

int port = Integer.parseInt(node.split(":")[1]);

jedisClusterNodes.add(new HostAndPort(host, port));

}

 

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

// 资源池中最大连接数

jedisPoolConfig.setMaxTotal(maxTotal);

// 资源池允许最大空闲的连接数

jedisPoolConfig.setMaxIdle(maxIdle);

// 资源池确保最少空闲的连接数

jedisPoolConfig.setMinIdle(minIdle);

// 当资源池用尽后,调用者是否要等待。只有当为true时,下面的maxWaitMillis才会生效

jedisPoolConfig.setBlockWhenExhausted(true);

// 当资源池连接用尽后,调用者的最大等待时间(单位为毫秒)

jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

// 向资源池借用连接时是否做连接有效性检测(ping),无效连接会被移除

jedisPoolConfig.setTestOnBorrow(true);

// 向资源池归还连接时是否做连接有效性检测(ping),无效连接会被移除

jedisPoolConfig.setTestOnReturn(false);

 

// 是否开启空闲资源监测

jedisPoolConfig.setTestWhileIdle(true);

// 空闲资源的检测周期(单位为毫秒),-1:不检测

jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30 * 1000);

// 资源池中资源最小空闲时间(单位为毫秒),达到此值后空闲资源将被移除

jedisPoolConfig.setMinEvictableIdleTimeMillis(60 * 1000);

// 做空闲资源检测时,每次的采样数

// 可根据自身应用连接数进行微调,如果设置为-1,就是对所有连接做空闲监测

jedisPoolConfig.setNumTestsPerEvictionRun(-1);

 

jc = new JedisCluster(jedisClusterNodes, jedisPoolConfig);

}

 

public static String buildKey(String keyPre, String code) {

        StringBuffer sb = new StringBuffer();

        sb.append(keyPre).append("_").append(code);

        return sb.toString();

    }

 

public static void set(String key, String value) {

jc.set(key, value);

}

 

public static String get(String key) {

String value = jc.get(key);

return value;

}

 

public static void sadd(String key, String member) {

jc.sadd(key, member);

}

 

public static void sadd(String key, String... member) {

jc.sadd(key, member);

}

 

public static void srem(String key, String member) {

jc.srem(key, member);

}

 

public static void srem(String key, String[] member) {

jc.srem(key, member);

}

 

public static Set<String> smembers(String key) {

return jc.smembers(key);

}

 

public static void expire(String key, int seconds) {

jc.expire(key, seconds);

}

 

public static String spop(String key) {

return jc.spop(key);

}

 

public static boolean sismember(String key, String member) {

return jc.sismember(key, member);

}

 

public static String srandmember(String key) {

 

return jc.srandmember(key);

}

 

 

public static Long lpush(String key, String content) {

 

return jc.lpush(key, content);

 

public static String rpop (String key) {

return jc.rpop(key);

}

 

public static long incr(String key) {

return jc.incr(key);

}

 

public static Long rpush(String key, String... content) {

 

return jc.rpush(key, content);

 

public static JedisClusterPipeline getJedisClusterPipeline()  {

return JedisClusterPipeline.pipelined(jc);

}

 

public static long sunionstore(String dstkey , String... keys) {

return jc.sunionstore(dstkey, keys);

}

 

public static long sdiffstore(String dstkey , String... keys) {

return jc.sdiffstore(dstkey, keys);

}

 

public static long scard(String dstkey) {

return jc.scard(dstkey);

}

 

public static long zadd(String key,double score, String member) {

return jc.zadd(key, score, member);

}

public static long zcard(String key) {

return jc.zcard(key);

}

public static long zremrangeByScore(String key, String start, String end) {

return jc.zremrangeByScore(key, start, end);

}

 

 

/**

* 判断集群是否有节点挂掉

* @return

*/

public static boolean isNodesWell(){

Map<String, JedisPool> clusterNodes = jc.getClusterNodes();

try {

for (Entry<String, JedisPool> enty : clusterNodes.entrySet()) {

Jedis jd = enty.getValue().getResource();

if (jd.isConnected()) jd.close();

}

} catch (Exception e) {

return false ;

}

return true ;

}

 

/**

* 判断key是否存在

*/

public static Boolean exists(String key) {

return jc.exists(key);

}

 

public static Long delete(String... keys) {

return jc.del(keys);

}

 

 

public static void batchSaddCache(String[] dstUsers, final String key)  {

 

if(dstUsers.length > 1024)  {//一次添加超过1024个元素 ,循环插入

List<String> arrays = Arrays.asList(dstUsers);

int pagesize = 1024;

int totalcount = arrays.size();

int pagecount = 0;

int m = totalcount % pagesize;

if(m > 0){

pagecount = totalcount / pagesize + 1;

}else{

pagecount = totalcount / pagesize;

 

}

 

 

try {

final CountDownLatch countDownLatch = new CountDownLatch(pagecount);

for(int i = 1; i <= pagecount; i++) {

 

 

List<String> subList = null;

if (m == 0) {

subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

}else{

if(i == pagecount){

subList = arrays.subList((i - 1) * pagesize, totalcount);

}else{

subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

}

}

final List<String> finalSubList = subList;

TaskConsumer.batchAddRedisCache.submit(new  Runnable() {

public void run() {

try {

String[] strings = new String[finalSubList.size()];

 

finalSubList.toArray(strings);

JedisClusterClient.sadd(key,strings);

 

} catch (Exception ex) {

System.out.println(ex);

} finally {

countDownLatch.countDown();

}

 

}

});

 

}

countDownLatch.await();

} catch (Exception ex) {

 

 

 

} else {

if(dstUsers.length <= 0) {

 

} else {

JedisClusterClient.sadd(key,dstUsers);

}

 

}

}

 

 

 

public static void batchSaddCache(String[] dstUsers, String key, int dstUserNumOneKey) {

 

 

if(dstUsers.length > 1024)  {//一次添加超过1024个元素 ,循环插入

List<String> arrays = Arrays.asList(dstUsers);

int pagesize = 1024;

int totalcount = arrays.size();

int pagecount = 0;

int m = totalcount % pagesize;

if(m > 0){

pagecount = totalcount / pagesize + 1;

}else{

pagecount = totalcount / pagesize;

}

for(int i = 1; i <= pagecount; i++) {

if (m == 0) {

List<String> subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

 

String[] strings = new String[subList.size()];

 

subList.toArray(strings);

JedisClusterClient.sadd(key,strings);

}else{

if(i == pagecount){

List<String> subList = arrays.subList((i - 1) * pagesize, totalcount);

String[] strings = new String[subList.size()];

 

subList.toArray(strings);

JedisClusterClient.sadd(key,strings);

 

}else{

List<String> subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

String[] strings = new String[subList.size()];

 

subList.toArray(strings);

JedisClusterClient.sadd(key,strings);

 

}

}

}

 

} else {

JedisClusterClient.sadd(key,dstUsers);

}

}

 

 

public static void batchSrem(String offMsgKey, String[] dstUsers) {

if(dstUsers.length > 1024)  {//一次添加超过1024个元素 ,循环插入

List<String> arrays = Arrays.asList(dstUsers);

int pagesize = 1024;

int totalcount = arrays.size();

int pagecount = 0;

int m = totalcount % pagesize;

if(m > 0){

pagecount = totalcount / pagesize + 1;

}else{

pagecount = totalcount / pagesize;

}

for(int i = 1; i <= pagecount; i++) {

if (m == 0) {

List<String> subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

 

String[] strings = new String[subList.size()];

 

subList.toArray(strings);

JedisClusterClient.srem(offMsgKey,strings);

}else{

if(i == pagecount){

List<String> subList = arrays.subList((i - 1) * pagesize, totalcount);

String[] strings = new String[subList.size()];

 

subList.toArray(strings);

JedisClusterClient.srem(offMsgKey,strings);

 

}else{

List<String> subList = arrays.subList((i - 1) * pagesize, pagesize * (i));

String[] strings = new String[subList.size()];

 

subList.toArray(strings);

JedisClusterClient.srem(offMsgKey,strings);

 

}

}

}

 

} else {

JedisClusterClient.srem(offMsgKey,dstUsers);

}

 

}

 

public static void main(String[] args) {

 

 

JedisClusterClient.set("you","testMSg hello");

String foo1 = JedisClusterClient.get("you");

System.out.println(foo1);

 

String foo = JedisClusterClient.get("foo");

System.out.println(foo);

 

String umrKey = "UMR_0__VodS04194";

long t1 = System.currentTimeMillis();

Set<String> msgCodes = JedisClusterClient.smembers(umrKey);

long t2 = System.currentTimeMillis();

System.out.println("set size is " + msgCodes.size() + " cost " + (t2 - t1) + "ms");

 

long t3 = System.currentTimeMillis();

String code = JedisClusterClient.srandmember(umrKey);

long t4 = System.currentTimeMillis();

System.out.println("get random value "+ code + " cost " + (t4 - t3) + "ms");

 

}

/*************************************************************JedisClusterPipeline.java*************/

package com.avit.cache.redis;

 

import java.io.Closeable;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.Queue;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import redis.clients.jedis.BinaryJedisCluster;

import redis.clients.jedis.Client;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisCluster;

import redis.clients.jedis.JedisClusterConnectionHandler;

import redis.clients.jedis.JedisClusterInfoCache;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisSlotBasedConnectionHandler;

import redis.clients.jedis.PipelineBase;

import redis.clients.jedis.exceptions.JedisMovedDataException;

import redis.clients.jedis.exceptions.JedisRedirectionException;

import redis.clients.util.JedisClusterCRC16;

import redis.clients.util.SafeEncoder;

 

/**

 * 在集群模式下提供批量操作的功能。 <br/>

 * 由于集群模式存在节点的动态添加删除,且client不能实时感知(只有在执行命令时才可能知道集群发生变更),

 * 因此,该实现不保证一定成功,建议在批量操作之前调用 refreshCluster() 方法重新获取集群信息。<br />

 * 应用需要保证不论成功还是失败都会调用close() 方法,否则可能会造成泄露。<br/>

 * 如果失败需要应用自己去重试,因此每个批次执行的命令数量需要控制。防止失败后重试的数量过多。<br />

 * 基于以上说明,建议在集群环境较稳定(增减节点不会过于频繁)的情况下使用,且允许失败或有对应的重试策略。<br />

 */

public class JedisClusterPipeline extends PipelineBase implements Closeable {

private static final Logger LOGGER = LoggerFactory.getLogger(JedisClusterPipeline.class);

 

// 部分字段没有对应的获取方法,只能采用反射来做

// 你也可以去继承JedisCluster和JedisSlotBasedConnectionHandler来提供访问接口

private static final Field FIELD_CONNECTION_HANDLER;

private static final Field FIELD_CACHE; 

static {

FIELD_CONNECTION_HANDLER = getField(BinaryJedisCluster.class, "connectionHandler");

FIELD_CACHE = getField(JedisClusterConnectionHandler.class, "cache");

}

 

private JedisSlotBasedConnectionHandler connectionHandler;

private JedisClusterInfoCache clusterInfoCache;

private Queue<Client> clients = new LinkedList<Client>();// 根据顺序存储每个命令对应的Client

private Map<JedisPool, Jedis> jedisMap = new HashMap<JedisPool, Jedis>();// 用于缓存连接

private boolean hasDataInBuf = false;// 是否有数据在缓存区

 

/**

* 根据jedisCluster实例生成对应的JedisClusterPipeline

* @param 

* @return

*/

public static JedisClusterPipeline pipelined(JedisCluster jedisCluster) {

JedisClusterPipeline pipeline = new JedisClusterPipeline();

    pipeline.setJedisCluster(jedisCluster);

    return pipeline;

}

 

public JedisClusterPipeline() {

}

 

public void setJedisCluster(JedisCluster jedis) {

connectionHandler = getValue(jedis, FIELD_CONNECTION_HANDLER);

clusterInfoCache = getValue(connectionHandler, FIELD_CACHE);

}

 

/**

* 刷新集群信息,当集群信息发生变更时调用

* @param 

* @return

*/

public void refreshCluster() {

connectionHandler.renewSlotCache();

}

 

/**

* 同步读取所有数据. 与syncAndReturnAll()相比,sync()只是没有对数据做反序列化

*/

public void sync() {

innerSync(null);

}

 

/**

* 同步读取所有数据 并按命令顺序返回一个列表

* @return 按照命令的顺序返回所有的数据

*/

public List<Object> syncAndReturnAll() {

List<Object> responseList = new ArrayList<Object>();

 

innerSync(responseList);

 

return responseList;

}

 

private void innerSync(List<Object> formatted) {

try {

for (Client client : clients) {

// 在sync()调用时其实是不需要解析结果数据的,但是如果不调用get方法,发生了JedisMovedDataException这样的错误应用是不知道的,因此需要调用get()来触发错误。

// 其实如果Response的data属性可以直接获取,可以省掉解析数据的时间,然而它并没有提供对应方法,要获取data属性就得用反射,不想再反射了,所以就这样了

Object data = generateResponse(client.getOne()).get();

if (null != formatted) {

formatted.add(data);

}

}

} catch (JedisRedirectionException jre) {

if (jre instanceof JedisMovedDataException) {

// if MOVED redirection occurred, rebuilds cluster's slot cache,

// recommended by Redis cluster specification

refreshCluster();

}

 

throw jre;

} finally {

// 所有还没有执行过的client要保证执行(flush),防止放回连接池后后面的命令被污染

for (Jedis jedis : jedisMap.values()) {

flushCachedData(jedis);

}

 

hasDataInBuf = false;

close();

}

}

 

@Override

public void close() {

clean();

 

clients.clear();

 

for (Jedis jedis : jedisMap.values()) {

if (hasDataInBuf) {

flushCachedData(jedis);

}

 

jedis.close();

}

 

jedisMap.clear();

 

hasDataInBuf = false;

}

 

private void flushCachedData(Jedis jedis) {

try {

jedis.getClient().getAll();

} catch (RuntimeException ex) {

// 其中一个client出问题,后面出问题的几率较大

LOGGER.error("flushCachedData error...", ex);

}

}

 

@Override

protected Client getClient(String key) {

byte[] bKey = SafeEncoder.encode(key);

 

return getClient(bKey);

}

 

@Override

protected Client getClient(byte[] key) {

Jedis jedis = getJedis(JedisClusterCRC16.getSlot(key));

 

Client client = jedis.getClient();

clients.add(client);

 

return client;

}

 

private Jedis getJedis(int slot) {

JedisPool pool = clusterInfoCache.getSlotPool(slot);

 

// 根据pool从缓存中获取Jedis

Jedis jedis = jedisMap.get(pool);

if (null == jedis) {

jedis = pool.getResource();

jedisMap.put(pool, jedis);

}

 

hasDataInBuf = true;

return jedis;

}

 

private static Field getField(Class<?> cls, String fieldName) {

try {

Field field = cls.getDeclaredField(fieldName);

field.setAccessible(true);

 

return field;

} catch (Exception e) {

throw new RuntimeException("cannot find or access field '" + fieldName + "' from " + cls.getName(), e);

}

}

 

@SuppressWarnings({"unchecked" })

private static <T> T getValue(Object obj, Field field) {

try {

return (T)field.get(obj);

} catch (Exception e) {

LOGGER.error("get value fail", e);

 

throw new RuntimeException(e);

}

}

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics