springboot在Jedis模式中实现pipeline

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/singgel/article/details/90021361

人狠话不多 

RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection redisConnection = factory.getConnection();
JedisClusterConnection jedisClusterConnection = (JedisClusterConnection) redisConnection;
// 获取到原始到JedisCluster连接
JedisCluster jedisCluster = jedisClusterConnection.getNativeConnection();
// 通过key获取到具体的Jedis实例
// 计算hash slot,根据特定的slot可以获取到特定的Jedis实例
int slot = JedisClusterCRC16.getSlot(key);
/**
 *  不建议这么使用,官方在2.10版本已经修复<a href="https://github.com/xetorthio/jedis/pull/1532">此问题</a><br>
 *  2.10版本中,官方会直接提供JedisCluster#getConnectionFromSlot
 */
Field field = ReflectionUtils.findField(BinaryJedisCluster.class, null, JedisClusterConnectionHandler.class);
field.setAccessible(true);
JedisSlotBasedConnectionHandler jedisClusterConnectionHandler = (JedisSlotBasedConnectionHandler) field.get(jedisCluster);
Jedis jedis = jedisClusterConnectionHandler.getConnectionFromSlot(slot);
// 接下来就是pipeline操作了
Pipeline pipeline = jedis.pipelined();
...
pipeline.syncAndReturnAll();

猜你喜欢

转载自blog.csdn.net/singgel/article/details/90021361