Using jedis How to cache a Java object

Using the Redis Java Jedis Client
How can I cache a Java object?

+4
source share
3 answers

You cannot store objects directly in redis. So, convert the object to String, and then put it in Redis. To do this, your object must be serialized. Convert the object to ByteArray and use some encoding algorithm (ex base64encoding) and convert it to String, then save to Redis. After choosing the reverse process, convert the String array to byte using the decoding algorithm (for example: base64decoding) and convert it to an object.

+2
source

lib : Redisson - Redis Java. Jedis

  • /
  • .
  • Redis

Redisson . , Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy JDK Serialization.

RBucket<AnyObject> bucket = redisson.getBucket("anyObject");
// set an object
bucket.set(new AnyObject());
// get an object
AnyObject myObject = bucket.get();
+2

you must convert your object as a json string to save it, then read json and convert it back to your object.

you can use gson for this.

//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);
+1
source

All Articles