14

It would be great if someone could suggest me on what would be the best way to store a list of java objects in Redis.

Currently, I'm converting the java objects into json strings and storing those strings in Redis and I have a set in Redis to keep track of all these.

For eg :-

SET student:1 '{"name":"testOne","stream":computer science}'
SET student:2 '{"name":"testTwo","stream":electronics}'
SADD students 1
SADD students 2

So when ever I want to fetch the list of students, I first get the set students and then iterate over it and get the json strings at those keys.

Just wondering if there is any other better way to handle the scenario of storing a list of java objects to Redis.

(I'm using redis as a cache)

user_ab
  • 225
  • 1
  • 3
  • 6

3 Answers3

12

If you don't need querying your java objects stored in redis (indexation, scores...), then you can just serialize them to a bytearray with a library (Kryo for instance) and store them in a String in redis. Note that you can serialize a whole collection of java objects in one redis string, just remember which class it is (collection type + type of object) when you want to deserialize, you can define a clever key name for this purpose for instance.

JSON will just use more space and will be more network intensive than other binary marshalling formats, if you don't need queries or human readable data in redis, then it is fine (and more performant) to store binary data in redis.

Note that if you are using jedis library (as you tagged it) you have methods on Jedis that take bytearrays as parameters instead of Java strings in order to send data as C-String to Redis (no data loss in the process).

zenbeni
  • 7,019
  • 3
  • 29
  • 60
3

Redis is more than a simple key value store. You can use hashes to store structured data in redis

HMSET student:1 name "testOne" stream "computer science" http://redis.io/commands/hmset

to get all contents of the hash call HGETALL student:1
you can also get single values of Hashes with HGET
EXAMPLE HGET student:1 name

Lugg
  • 197
  • 5
  • I'm actually looking to store a list of java objects and retrieve them when ever needed. Also I wanted to minimize the number of queries to retrieve the list so I chose to use json strings to store the objects which lets me use MGET and get all keys in one go. If I use hashes to store my java objects, I cannot get all the elements of the list of java objects in one go. – user_ab Mar 10 '15 at 04:03
  • 1
    If you want to store them not ordered you can use a SET to store the keys of the hash. You can use a LUA script to retrieve them in one go The problem with your approach: -Update and delete just possible on client side. ->bad multi user support – Lugg Mar 18 '15 at 01:28
3

You can easily do it with Redisson. It's a Redis based framework for Java which supports many popular codecs (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization) and Redis connection modes like Cluster, Sentinel, AWS Elasticache.

Here is an example how to store Java object to list:

RList<SomeObject> list = redisson.getList("anyList");
list.add(new SomeObject(1));
list.add(new SomeObject(2));

RList interface also implements java.util.List interface. Pretty easy, right?

Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71