Can Jedis get/set an Java POJO?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 01:21:50

问题


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


回答1:


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.




回答2:


Answer to question 1:

Redisson (Redis based framework for Java) 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 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.

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



来源:https://stackoverflow.com/questions/12279117/can-jedis-get-set-an-java-pojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!