我需要多个 Redis Stream 来传递消息,这些消息具有相似的格式。所以我定义了一个泛型类:
@Data
public class GenericObject<K,V> {
private K key;
private V value;
}
然后我向 redis 流添加一条消息,如下所示:
GenericObject<Long, AnotherObject> msg = new GenericObject<>();
msg.setKey(1L);
msg.setValue(new AnotherObject("hello"));
ObjectRecord<String, GenericObject<Long, AnotherObject>> record = StreamRecords.objectBacked(msg).withStreamKey("hello-stream");
stringRedisTemplate.opsForStream().add(record);
我从 Redis 得到这样的记录:
xrange hello-stream - +
1) 1) 1631713727786-0
2) 1) _class
2) com.redisexample.pojo.GenericObject
3) key._class
4) java.lang.Long
5) key
6) 1
似乎该值被忽略了。
如何正确使用 RedisTemplate 将通用对象添加到 Redis Stream?