对码当歌,猿生几何?

关于redis管道和批量处理的代码

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

更多代码请访问  www.itkc8.com

List<GpsUploadVO> list = requestData.getVo().getList();
        Map<Long, GpsUploadVO> reMap = list.stream().collect(Collectors.toMap(GpsUploadVO::getUploadTime, a -> a,(k1, k2)->k1));
        String key = CacheKey.GPS_UPLOAD_DATA + requestData.getUserId();
        redisTemplate.executePipelined(new SessionCallback<Object>() {
            @Override
            public <K, V> Object execute(RedisOperations<K, V> redisOperations) throws DataAccessException {
                for (Map.Entry<Long, GpsUploadVO> map : reMap.entrySet()) {
                    redisComponent.setField(key, map.getKey().toString(), JSON.toJSONString(map.getValue()));
                }
                return null;
            }
        });
SessionCallback<Object> sessionCallback = new SessionCallback<Object>(){
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException{
                operations.multi();

                operations.opsForList().range(key, start, end);
                redisTemplate.opsForList().trim(key, end + 1, -1);

                Object val = operations.exec();
                return val;
            }
        };
        Object execute = redisTemplate.execute(sessionCallback);
        System.out.println(execute.getClass());
        List list = (List) execute;
        List o = (List)list.get(0);
        System.out.println(o);
        System.out.println(o.get(0));

 

 

 

事务代码

public Object testRedisMulti(String key, int start, int end) {
        Object o = redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                operations.multi();
                operations.opsForList().range(key, start, end);
                redisTemplate.opsForList().trim(key, end + 1, -1);
                Object rs = operations.exec();
                return rs;
            }
        });
        return o;
    }