RedisExpendUtil.java

    1. /**
    2. * pipeline批量执行redis命令
    3. * @param zSetMap
    4. * @return
    5. */
    6. public List<Object> zSetPipelineBatchAdd(Map<String, Map<Object, Double>> zSetMap) {
    7. return redisTemplate.executePipelined(new RedisCallback<String>() {
    8. //序列化方式
    9. RedisSerializer keySerializer = redisTemplate.getKeySerializer();
    10. RedisSerializer valueSerializer = redisTemplate.getValueSerializer();
    11. @Override
    12. public String doInRedis(RedisConnection redisConnection) throws DataAccessException {
    13. for (String key : zSetMap.keySet()) {
    14. //value集合
    15. Map<Object, Double> valueScoreMap = zSetMap.get(key);
    16. Set<RedisZSetCommands.Tuple> rawTuples = new LinkedHashSet<>(valueScoreMap.size());
    17. valueScoreMap.forEach((k, v) -> rawTuples.add(new DefaultTuple(valueSerializer.serialize(k), v)));
    18. redisConnection.zAdd(keySerializer.serialize(key), rawTuples);
    19. }
    20. return null;
    21. }
    22. }, redisTemplate.getValueSerializer());
    23. }