有没有办法使用Spring Data Redis'存储库一次检索多个哈希值?
我的代码不起作用:
@RedisHash("Message")
public class Message implements Serializable {
@Id
private String id;
private String text;
private LocalDateTime posted;
private Integer upvotes;
private String authorId;
}
@Repository
public interface MessageRepository extends CrudRepository<Message, String> {
// Got: IN (1): [IsIn, In]is not supported for redis query derivation
Set<Message> getByIdIn(Set<String> ids);
}
目前我正在使用RestTemplate#executePipelined
,但我发现这种方法很麻烦:
public Set<Message> getMessagesById(Set<String> ids) {
// I had to implement custom mapper since BeanUtilsHashMapper fails on LocalDateTime deserialization from String
HashMapper<Message, String, String> mapper = new DTOConverter<>(Message.class);
List<Object> messages = template.executePipelined((RedisCallback<Object>) connection -> {
StringRedisConnection conn = (StringRedisConnection) connection;
ids.stream()
.map(id -> "Message:" + id)
.forEach(conn::hGetAll);
return null;
});
return messages.stream()
.map(o -> (Map<String, String>) o)
.map(o -> mapper.fromHash(o))
.collect(toSet());
}
有没有办法将mapper
传递给executePipelined
?