The method of storing objects in redis
This article uses easy-to-understand examples to introduce the method of storing objects in redis, the code is very detailed, interested friends can refer to it, I hope it can be helpful to you.
Redis has been widely used, but redis itself does not have a way to store objects directly. We can store objects by transforming objects.
Several schemes are summarized as follows:
Scenario 1: serialize the object to binary
Use the redis interface:
Jedis.get (byte [] key) jedis.set (byte [] key, byte [] value)
As for serialization, we have many options, such as Java serialize,Protobuf, or we can serialize manually ourselves.
Public byte [] serialize (Object obj); public Object unSerialize (byte [] bytes)
Scenario 2: serialize to a string
Use the redis interface:
Jedis.get (String key); jedis.set (String key, String value)
We also have a lot of options for serializing into strings: Json (Jackson,FastJson), Xml, etc.
Plan 3: convert the object to map
Use the redis interface:
Jedis.hgetAll (String key); jedis.hmset (String key, Map values); the method of storing objects in redis is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.