【发布时间】: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);
但这对我来说似乎效率低下,因为我正在有效地将我的大型缓存“克隆”到一个字节数组中。
解决这个问题的有效方法是什么?
【问题讨论】: