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

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

List list = requestData.getVo().getList();
        Map 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() {
            @Override
            public  Object execute(RedisOperations redisOperations) throws DataAccessException {
                for (Map.Entry map : reMap.entrySet()) {
                    redisComponent.setField(key, map.getKey().toString(), JSON.toJSONString(map.getValue()));
                }
                return null;
            }
        }); 
  
SessionCallback sessionCallback = 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 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;
    }

 

 

你可能感兴趣的:(Redis)