串行化执行代码实践

2.2.1 串行化执行代码实践
public class JedisClusterTest {

    private JedisCluster jedisCluster;

    @BeforeEach
    void setUp() {
        // 配置连接池
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxIdle(8);
        poolConfig.setMinIdle(0);
        poolConfig.setMaxWaitMillis(1000);
        HashSet<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.150.101", 7001));
        nodes.add(new HostAndPort("192.168.150.101", 7002));
        nodes.add(new HostAndPort("192.168.150.101", 7003));
        nodes.add(new HostAndPort("192.168.150.101", 8001));
        nodes.add(new HostAndPort("192.168.150.101", 8002));
        nodes.add(new HostAndPort("192.168.150.101", 8003));
        jedisCluster = new JedisCluster(nodes, poolConfig);
    }

    @Test
    void testMSet() {
        jedisCluster.mset("name", "Jack", "age", "21", "sex", "male");

    }

    @Test
    void testMSet2() {
        Map<String, String> map = new HashMap<>(3);
        map.put("name", "Jack");
        map.put("age", "21");
        map.put("sex", "Male");
        //对Map数据进行分组。根据相同的slot放在一个分组
        //key就是slot,value就是一个组
        Map<Integer, List<Map.Entry<String, String>>> result = map.entrySet()
                .stream()
                .collect(Collectors.groupingBy(
                        entry -> ClusterSlotHashUtil.calculateSlot(entry.getKey()))
                );
        //串行的去执行mset的逻辑
        for (List<Map.Entry<String, String>> list : result.values()) {
            String[] arr = new String[list.size() * 2];
            int j = 0;
            for (int i = 0; i < list.size(); i++) {
                j = i<<2;
                Map.Entry<String, String> e = list.get(0);
                arr[j] = e.getKey();
                arr[j + 1] = e.getValue();
            }
            jedisCluster.mset(arr);
        }
    }

    @AfterEach
    void tearDown() {
        if (jedisCluster != null) {
            jedisCluster.close();
        }
    }
}

2.2.2 Spring集群环境下批处理代码

   @Test
    void testMSetInCluster() {
        Map<String, String> map = new HashMap<>(3);
        map.put("name", "Rose");
        map.put("age", "21");
        map.put("sex", "Female");
        stringRedisTemplate.opsForValue().multiSet(map);


        List<String> strings = stringRedisTemplate.opsForValue().multiGet(Arrays.asList("name", "age", "sex"));
        strings.forEach(System.out::println);

    }

原理分析

在RedisAdvancedClusterAsyncCommandsImpl 类中

首先根据slotHash算出来一个partitioned的map,map中的key就是slot,而他的value就是对应的对应相同slot的key对应的数据

通过 RedisFuture mset = super.mset(op);进行异步的消息发送

@Override
public RedisFuture<String> mset(Map<K, V> map) {

    Map<Integer, List<K>> partitioned = SlotHash.partition(codec, map.keySet());

    if (partitioned.size() < 2) {
        return super.mset(map);
    }

    Map<Integer, RedisFuture<String>> executions = new HashMap<>();

    for (Map.Entry<Integer, List<K>> entry : partitioned.entrySet()) {

        Map<K, V> op = new HashMap<>();
        entry.getValue().forEach(k -> op.put(k, map.get(k)));

        RedisFuture<String> mset = super.mset(op);
        executions.put(entry.getKey(), mset);
    }

    return MultiNodeExecution.firstOfAsync(executions);
}

相关推荐

  1. 串行执行代码实践

    2024-05-02 09:46:05       34 阅读
  2. Python代码执行顺序

    2024-05-02 09:46:05       18 阅读
  3. 如何进行结构编程:结合代码实践指南

    2024-05-02 09:46:05       23 阅读
  4. mysql隔离级别和串行

    2024-05-02 09:46:05       58 阅读
  5. 手撕代码: C++实现数据的序列和反序列

    2024-05-02 09:46:05       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 09:46:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 09:46:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 09:46:05       87 阅读
  4. Python语言-面向对象

    2024-05-02 09:46:05       96 阅读

热门阅读

  1. selenium之document.querySelector()方法

    2024-05-02 09:46:05       32 阅读
  2. Redis除了当缓存 | 你还能想到哪些高级用法?

    2024-05-02 09:46:05       33 阅读
  3. 探索图像边缘:使用Python进行轮廓检测

    2024-05-02 09:46:05       30 阅读
  4. 【软测学习笔记】MySQL入门Day01

    2024-05-02 09:46:05       35 阅读
  5. Qt 配置 FFmpeg

    2024-05-02 09:46:05       32 阅读
  6. python 关键字(else)

    2024-05-02 09:46:05       29 阅读
  7. Ubuntu 18.0.4 安装 libc6 2.28 及公钥验证相关

    2024-05-02 09:46:05       34 阅读
  8. Python中关于子类约束的开发规范

    2024-05-02 09:46:05       31 阅读
  9. Ubuntu安装Docker和Docker Compose

    2024-05-02 09:46:05       29 阅读
  10. Vue指令、生命周期、Axios异步请求方式

    2024-05-02 09:46:05       31 阅读