Using jedis How to cache Java object

DP Dev picture DP Dev · May 15, 2015 · Viewed 21.1k times · Source

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

Answer

jeorfevre picture jeorfevre · Dec 7, 2015

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