Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Jedis get/set an Java POJO?

Tags:

jedis

I'm using Jedis as the java client to connect to Redis Servers.

Question 1: It seems there is no method to get/set Object < ? extends Serializable> ? All the values must be String or byte[]?

Other clients like "JRedis" and Spymemcache(for memcached Server) could.

Question 2: If I use ShardedJedis, it cannot set auth/password? While Jedis class can (using auth(String password)).

like image 394
hongtium Avatar asked Sep 05 '12 10:09

hongtium


People also ask

Can Redis store Java object?

There is no direct way to store a Java object as value in redis, however one can store and get an java object as byte[] and the Object can be to/from converted to byte[] array using ByteBuffer. This can be used to even reduce memory usage on redis if the object has numerical values.

Can POJO implement interface?

suppose if you provide interface and its implementation(in POJO which implements interface), and you are persisting it into database using interface reference to Implementation class.

Why do we need POJO classes in Java?

POJOs are used for increasing the readability and re-usability of a program. POJOs have gained the most acceptance because they are easy to write and understand. They were introduced in EJB 3.0 by Sun microsystems.


2 Answers

Regard Question 1: Jedis won't handle POJOs. You should serialize to a string or byte[] and use jedis to do that, although I won't recommend to store your java objects serialized, as you won't be able to use all Redis cool features. A different approach would be to use something like a object-hash mapper, like JOhm.

Regard Question 2: ShardedJedis will only support commands that run on a single key. This is to guarantee atomicity. If you want to run a specific command on a specific redis you should use shardedJedis.getShard('someky') which will return a Jedis instance that you can use. Another way to handle this, the recommended one, is to specify your password in the JedisShardInfo instances. You can see and example of this in the tests.

like image 142
xetorthio Avatar answered Sep 28 '22 06:09

xetorthio


Answer to question 1:

Redisson (Redis Java client) can work with POJO objects. And you don't need to serialize/deserialize object by yourself each time and work with connections (acquire/release). It's all done by the Redisson.

Here is an example:

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

or you can use LiveObjectService which stores POJO fields as keys in Redis hash object.

@REntity
public class MyObject {

   @RId
   private String id;
   @RIndex
   private String value;
   private MyObject parent;

   public MyObject(String id) {
       this.id = id;
   }

   public MyObject() {
   }

   // getters and setters

}

Redisson supports many popular codecs like Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization.

like image 28
Nikita Koksharov Avatar answered Sep 28 '22 08:09

Nikita Koksharov