14

Using Redis Java client Jedis
How can I cache Java Object?

Simulant
  • 19,190
  • 8
  • 63
  • 98
DP Dev
  • 201
  • 1
  • 2
  • 9
  • Serialize it using your favorite marshaller, kryo or json for instance. – zenbeni May 15 '15 at 08:51
  • http://stackoverflow.com/questions/12279117/can-jedis-get-set-an-java-pojo I think my question has answered at this thread. – DP Dev May 15 '15 at 13:56

3 Answers3

19

you should convert your object as a json string to store it, then read the json and transform it back to your object.

you can use Gson in order to do so.

//store
Gson gson = new Gson();
String json = gson.toJson(myObject);
jedis.set(key,json);

//restore
String json = jedis.get(key);
MyObject object=gson.fromJson(json, MyObject.class);
jeorfevre
  • 2,286
  • 1
  • 17
  • 27
5

You can't store objects directly into redis. So convert the object into String and then put it in Redis. In order to do that your object must be serialized. Convert the object to ByteArray and use some encoding algorithm (ex base64encoding) and convert it as String then store in Redis. While retrieving reverse the process, convert the String to byte array using decoding algorithm (ex: base64decoding) and the convert it to object.

Karthikeyan Gopall
  • 5,469
  • 2
  • 19
  • 35
  • 1
    But is this recommended practices to do so with Redis ? – DP Dev May 15 '15 at 13:59
  • If you really want to store the object in Redis this is the only way. It's upto you to store the object directly or splitting up and storing individual values in objects as a hash or something. – Karthikeyan Gopall May 15 '15 at 14:04
  • 2
    This is a good answer. But you don't necessarily need to convert into String. You can store byte[] in Redis which will save you from converting to/from String. Good post on deserializing object to byte[] http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array – Madhav Aug 12 '16 at 01:00
4

I would recommend to use more convenient lib to do it: Redisson - it's a Redis based framework for Java. It has some advantages over Jedis

  1. You don't need to serialize/deserialize object by yourself each time
  2. You don't need to manage connection by yourself
  3. You can work with Redis asynchronously

Redisson does it for you and even more. It supports many popular codecs like Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization.

RBucket<AnyObject> bucket = redisson.getBucket("anyObject");
// set an object
bucket.set(new AnyObject());
// get an object
AnyObject myObject = bucket.get();
Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71
  • 1
    Beware this ads up a layer of complexity for your application since after a change on the signature of your clases Redisson won't be able to retrieve the serialized object "as is" – JorgeGarza Apr 30 '19 at 02:14
  • @JorgeGarza I'm afraid you're mistaken. Redisson supports about 10 codecs. If you use JDK Serialization codec, for example, it won't be a problem. You can even write your own codec. Which codec do you use? – Nikita Koksharov May 01 '19 at 04:10