I am trying to under how Kryo
serlization works. I have a very large HashMap which I would like to push into Redis
. The HashMap is:
HashMap<String, HashMap<String, Set<Long>>> cache = new HashMap<>();
What is the fastest way to serialize into Redis
?
Option 1: Directly into Redis?
I see that you can use Kryo
like:
Kryo kryo = new Kryo();
kryo.register(HashMap.class);
Output output = //For Redis what would the output be ?
kryo. writeObject(output, cache)
But I am confused as to what Output
should be when using Redis
.
Option 2: Via a byte array?
I have also seen that the following maybe possible:
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);
But this seems inefficient to me as I am effectively "cloning" my large cache into a byte array.
What is an efficient approach for this problem ?
AFAIK there's no Redis output for Kryo. Jedis has only byte[]
and String
APIs so you can't use wrapped/pooled buffers.
Using Kryo is possible already with redisson as they provide a Kryo codec which uses ByteBuffer
s. Alternatively, you could also use lettuce which is a low-level Redis driver providing also a codec API using ByteBuffer
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.