2016-04-12 68 views
2

我试图根据Kryo serlization如何工作。我有一个非常大的HashMap,我想推入Redis。 HashMap是:使用Kryo将HashMap序列化到Redis

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

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

选项1:直接进入Redis?

我看到,你可以使用Kryo像:

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); 

但这似乎效率不高我,我有效地“克隆”我的大缓存为字节数组。

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

回答

0

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

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