美文网首页
Redis管道批量操作数据

Redis管道批量操作数据

作者: AC编程 | 来源:发表于2021-08-20 16:01 被阅读0次

    批量插入数据

    /**
         * 管道批量插入数据
         * @param key
         * @param datas
         * @return
         */
        public Boolean sSetPipe(String key,List<Object> datas){
            try {
                redisTemplates.executePipelined(new SessionCallback<Object>() {
                    @Override
                    public <K, V> Map execute(RedisOperations<K, V> operations) throws DataAccessException {
                        if(CollectionUtils.isEmpty(datas) || StringUtils.isEmpty(key)){
                            return null;
                        }
    
                        SetOperations setOperations = operations.opsForSet();
                        for(Object value : datas){
                            setOperations.add(key,value);
                        }
                        return null;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                RedisConnectionUtils.unbindConnection(redisTemplates.getConnectionFactory());
            }
    
            return true;
        }
    

    批量查询数据

    public List<Object> lrightPopLeftPush(String sourceKey, String destinationKey, int limit) {
            try {
                return redisTemplates.executePipelined(new SessionCallback<Object>() {
                    @Override
                    public <K, V> Map execute(RedisOperations<K, V> operations) throws DataAccessException {
    
                        ListOperations listOperations = operations.opsForList();
                        for (long i = 0; i < limit; i++) {
                            listOperations.rightPopAndLeftPush(sourceKey, destinationKey);
                        }
                        return null;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                return new ArrayList<>();
            } finally {
                RedisConnectionUtils.unbindConnection(redisTemplates.getConnectionFactory());
            }
        }
    

    相关文章

      网友评论

          本文标题:Redis管道批量操作数据

          本文链接:https://www.haomeiwen.com/subject/xaptiltx.html