Redis集群批量操作

      众所周知,Redis集群是没法执行批量操作命令的,如mget,pipeline等。这是因为redis将集群划分为16383个哈希槽,不同的key会划分到不同的槽中。但是,Jedis客户端提供了计算key的slot方法,已经slot和节点之间的映射关系,通过这两个数据,就可以计算出每个key所在的节点,然后使用pipeline获取数据。具体代码如下:

/**
 * 根据key计算slot,再根据slot计算node,获取pipeline
 * 进行批量操作 
 */
public class BatchUtil {

	public static Map<String,String> mget(JedisCluster jc,String... keys){
		Map<String,String> resMap = new HashMap<>();
		if(keys == null || keys.length == 0){
			return resMap;
		}
		//如果只有一条,直接使用get即可
		if(keys.length == 1){
			resMap.put(keys[0],jc.get(keys[0]));
			return resMap;
		}
		
		//JedisCluster继承了BinaryJedisCluster
		//BinaryJedisCluster的JedisClusterConnectionHandler属性
		//里面有JedisClusterInfoCache,根据这一条继承链,可以获取到JedisClusterInfoCache
		//从而获取slot和JedisPool直接的映射
		MetaObject metaObject = SystemMetaObject.forObject(jc);
		JedisClusterInfoCache cache = (JedisClusterInfoCache) metaObject.getValue("connectionHandler.cache");
		
		//保存地址+端口和命令的映射
		Map<JedisPool,List<String>> jedisPoolMap = new HashMap<>();
		
		List<String> keyList = null;
		JedisPool currentJedisPool = null;
		Pipeline currentPipeline = null;
		
		for(String key:keys){
			//计算哈希槽
			int crc = JedisClusterCRC16.getSlot(key);
			//通过哈希槽获取节点的连接
			currentJedisPool = cache.getSlotPool(crc);
			
			//由于JedisPool作为value保存在JedisClusterInfoCache中的一个map对象中,每个节点的
			//JedisPool在map的初始化阶段就是确定的和唯一的,所以获取到的每个节点的JedisPool都是一样
			//的,可以作为map的key
			if(jedisPoolMap.containsKey(currentJedisPool)){
				jedisPoolMap.get(currentJedisPool).add(key);
			}else{
				keyList = new ArrayList<>();
				keyList.add(key);
				jedisPoolMap.put(currentJedisPool, keyList);
			}
		}
		
		//保存结果
		List<Object> res = new ArrayList<>();
		
		//执行
		for(Entry<JedisPool,List<String>> entry:jedisPoolMap.entrySet()){
			try {
				currentJedisPool = entry.getKey();
				keyList = entry.getValue();
				//获取pipeline
				currentPipeline = currentJedisPool.getResource().pipelined();
				for(String key:keyList){
					currentPipeline.get(key);
				}
				//从pipeline中获取结果
				res = currentPipeline.syncAndReturnAll();
				currentPipeline.close();
				for(int i=0;i<keyList.size();i++){
					resMap.put(keyList.get(i),res.get(i)==null?null:res.get(i).toString());
				}
			} catch (Exception e) {
				e.printStackTrace();
				return new HashMap<>();
			}
			
		}
		return resMap;
	}
	
}

 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值