How to use java object as a value in Redis

Venkat Teki picture Venkat Teki · Mar 6, 2012 · Viewed 51.6k times · Source

I am pretty new to Redis.

I downloaded Jedis and added that to my classpath. But, it doesnt provide a way to store java object as "value"

Am i missing something or Jedis doesn't provide the way to store java object as value?

Thanks, -Venkat

Answer

Nikita Koksharov picture Nikita Koksharov · Aug 26, 2016

You can easily do it with Redis based framework for Java - Redisson:

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

// supports some useful functions like:
bucket.trySet(object);
bucket.compareAndSet(oldObject, newObject);
AnyObject prevObject = bucket.getAndSet(new AnyObject());

It handles serialization and maintains internal connection pool so you don't need to deal with it each time when you need to send an object to Redis. Redisson does it for you. Work with Redis as you used to work with Java objects.

It supports many popular codecs (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization).

DISCLAMER: I'm a lead developer of Redisson