【问题标题】:Serialize HashMap into Redis Using Kryo使用 Kryo 将 HashMap 序列化为 Redis
【发布时间】:2016-04-12 15:09:19
【问题描述】:

我正在尝试了解Kryo serlization 的工作原理。我有一个非常大的 HashMap,我想将它推入 Redis。 HashMap 是:

HashMap<String, HashMap<String, Set<Long>>> cache = new HashMap<>();

序列化为Redis 的最快方法是什么?

选项 1:直接进入 Redis?

我看到你可以使用Kryo like:

Kryo kryo = new Kryo();
kryo.register(HashMap.class);
Output output = //For Redis what would the output be ?
kryo. writeObject(output, cache)

但我对使用 Redis 时的 Output 应该是什么感到困惑。

选项 2:通过字节数组?

我还看到以下可能:

Jedis jedis = new Jedis("localhost");
Kryo kryo = new Kryo();
kryo.register(HashMap.class);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
Output output = new Output(stream);
kryo.writeObject(output, cache);
output.close();
byte[] buffer = stream.toByteArray();
jedis.set("Test", buffer);

但这对我来说似乎效率低下,因为我正在有效地将我的大型缓存“克隆”到一个字节数组中。

解决这个问题的有效方法是什么?

【问题讨论】:

    标签: java redis kryo


    【解决方案1】:

    AFAIK Kryo 没有 Redis 输出。 Jedis 只有 byte[]String API,因此您不能使用包装/池化缓冲区。

    redisson 已经可以使用 Kryo,因为它们提供了使用 ByteBuffers 的 Kryo 编解码器。或者,您也可以使用 lettuce,它是一个低级 Redis 驱动程序,还使用 ​​ByteBuffer 提供 codec API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多