Redis管道批量操作数据

2021-08-20  本文已影响0人  AC编程

批量插入数据

/**
     * 管道批量插入数据
     * @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());
        }
    }
上一篇下一篇

猜你喜欢

热点阅读