Java 使用Kryo将HashMap序列化到Redis中
我正试图了解Java 使用Kryo将HashMap序列化到Redis中,java,redis,kryo,Java,Redis,Kryo,我正试图了解Kryoserlization的工作原理。我有一个非常大的HashMap,我想把它推到Redis。HashMap是: HashMap<String, HashMap<String, Set<Long>>> cache = new HashMap<>(); 但我不清楚使用Redis时,输出应该是什么 选项2:通过字节数组? 我也看到了以下可能的情况: Jedis jedis = new Jedis("localhost"); Kryo
Kryo
serlization的工作原理。我有一个非常大的HashMap,我想把它推到Redis
。HashMap是:
HashMap<String, HashMap<String, Set<Long>>> cache = new HashMap<>();
但我不清楚使用Redis时,输出应该是什么
选项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);
但这对我来说似乎效率低下,因为我正在有效地将我的大型缓存“克隆”到一个字节数组中
解决这个问题的有效方法是什么?好吧,Kryo没有Redis输出。Jedis只有byte[]
和String
api,因此不能使用包装/池缓冲区
使用Kryo已经是可能的,因为他们提供了使用ByteBuffer
s的Kryo编解码器。或者,您也可以使用它,它是一个低级的Redis驱动程序,同时提供一个usingByteBuffer
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);