Spring环境下RedisTemplate同时配置两个数据源的数据进行同步数据

大概执行逻辑

1、从第一个库中获取需要同步的资源存入到map中 ,一个Redis的配置是在yml配置文件中

2、循环map然后配置新的连接库,配置完毕后将数据进行缓存

    @Autowired
    private ItemPropsRedis propsRedis;
    private List<Long> numIids = Lists.newArrayList(50000697L, 162103L, 50011277L,
            50012042L, 50012027L, 201309706L, 50011165L, 122654005L, 50013878L,
            50012010L, 162703L, 50020835L, 50010817L, 50012028L, 50025883L, 122690003L,
            50012825L, 50012019L);

    public void syncRedisDate() {
        Map<Long, List<ItemProp>> map = new LinkedHashMap<>();
        for (Long cid : cids) {//循环获取第一个Redis中的缓存数据
            List<ItemProp> itemProps = propsRedis.getItemProps(cid);
            if (CollectionUtils.isEmpty(itemProps)) continue;
            map.put(cid, itemProps);//数据存入map中
        }
        for (Map.Entry<Long, List<ItemProp>> entry : map.entrySet()) {
            save(entry.getKey().toString(), entry.getValue());//开始同步到另外一个Redis中
        }
    }

 private void save(String cid, List<ItemProp> itemProps) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName("localhost");
        jedisConnectionFactory.setPassword("password");
        jedisConnectionFactory.setPort(6479);
        jedisConnectionFactory.setDatabase(0);
        jedisConnectionFactory.afterPropertiesSet();
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.afterPropertiesSet();//更新配置信息
        ValueOperations<String, List<ItemProp>> operations = redisTemplate.opsForValue();
        System.out.println("itemProps = " + itemProps.size());
        operations.set(cid, itemProps);
        List<ItemProp> itemProps2 = operations.get(cid);
        System.out.println("itemProps2 = " + itemProps2.size());
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Acmen-zym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值