批量录入:
批量导入: //Map<String, String> markTypeMapredisTemplate.executePipelined((RedisCallback<Object>) connection -> {// 处理标签缓存markTypeMap.forEach((key, value) -> {byte[] valueBytes = new byte[0];try {//必须做序列化valueBytes = objectMapper.writeValueAsBytes(value);} catch (JsonProcessingException e) {log.info("序列化异常");throw new RuntimeException(e);}//key必须getBytesconnection.set(key.getBytes(), valueBytes);//这里设置超时时间 60秒 * 10connection.expire(key.getBytes(), 60 * 10);});//这里不用管return null;});
批量查询
批量查询: //返回值就List,泛型当前业务所以String redisKeys是key值集合List<String> stringValues = redisTemplate.opsForValue().multiGet(redisKeys);
备注:这里取出来的数据是List,每个索引位数据对应redisKeys(要查询的key集合索引为)如果redisKeys某一个索引位在redis里不存在,则会补null,如4索引位不存在1,2,3,null,5
批量查询到的数据转型(我存的是 , 间隔的String数据,这里只做参考)
//多个商品id对应的多条标签集合(逗号分隔的string)List<String> stringValues = redisTemplate.opsForValue().multiGet(redisKeys);List<List<String>> redisMarkTypeList = new ArrayList<>();if (stringValues != null) {for (String value : stringValues) {if (value == null || value.trim().isEmpty()) {// 如果为空,可以加入空列表或者跳过redisMarkTypeList.add(Collections.emptyList());} else {List<String> markTypes = Arrays.stream(value.split(",")).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());redisMarkTypeList.add(markTypes);}}}
对应关系