Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use java object as a value in Redis

Tags:

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

like image 717
Venkat Teki Avatar asked Mar 06 '12 13:03

Venkat Teki


People also ask

Can objects be stored in Redis?

Object Storing Procedure. In the Redis, Everything can be stored as only key-value pair format. Key must be unique and storing an object in a string format is not a good practice anyway. Objects are usually stored in a binary array format in the databases.

Is Redis Java based?

Redisson is a Redis-based framework that provides a wrapper and interface for working with Redis in Java. Redisson includes implementations of many familiar Java classes, including distributed objects, distributed services, distributed locks and synchronizers, and distributed collections.


2 Answers

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

like image 95
Nikita Koksharov Avatar answered Oct 05 '22 03:10

Nikita Koksharov


There is no direct means - it can be done only via serialization and storing resultant byte-array. Please refer http://static.springsource.org/spring-data/redis/docs/1.0.x/api/org/springframework/data/redis/serializer/package-summary.html if you want to use spring.

like image 22
Muthukumaran Avatar answered Oct 05 '22 02:10

Muthukumaran