简体   繁体   中英

Best way to store big complex java class objects in Redis?

What is the best way to store a nested and complex java object in Redis. The way we are doing is as follows.

We are using Redisson java client library for Redis interactions. Please see the code below :

   try{
        Config conf = new Config();
        conf.useSingleServer().setTimeout(3600000);
        conf.useSingleServer().setRetryInterval(3600000);
        conf.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redisson = Redisson.create(conf);
        RMap<String,Object> map = redisson.getMap("myCache");
        MyClass myObject; // This is the big complex object.
        map.put("key", myObject);
        redisson.shutdown();
    }catch (Exception ex) {
        ex.printStackTrace();
    }

Similarly, instead of put, we are using get to fill our myObject from redis.

We have increased the timeouts because of big size data being stored in redis, We were getting following exception :

Command execution timeout for command: (EVAL) with params...

Please answer the following questions as well :

  1. Is this the best way to put huge complex java objects in Redis? (The data can be around 1GB)
  2. Our team is new to Redis, We have read the good performance given by Redis, Should we use it to store complex data or it is good only in case of small single String as the value? (If So, what is Advised for our use case)

Thanks.

I think your problem is actually two problems put together: Complex object and Huge object.

So let me break it down and talk about them one by one:

  1. For complex Java object problem, you can convert it, along with its nested custom types, into Redisson Live Objects. It works by mapping all the fields of a class into a Redis hash. It also handles object references the same way Java does, providing it is also a Redisson object or a Live Object. When you call myObject.getField1() if would only fetch one field from Redis instead of the entire object like shown in your example.

There are two articles on DZone which can help you understand Redisson Live Object a bit more: Introducing Redisson Live Objects (Object Hash Mapping) (written by myself) and A Look at the Java Distributed In-Memory Data Model (Powered by Redis) (written by Nikita Koksharov)

  1. For huge object problem, if the huge object is made out of one or a few of field in the class are one big unbreakable objects, like a byte array, an image or a big file. You can convert each those fields into a Redisson binary stream. You can read from it or write to it in chunks without worry about the timeouts or blocking the Redis service.

Redisson binary stream is achieved by mapping the data to a Redis string object and it will split the data into chunks when necessary. You can find the usage of Redisson binary stream at the project wiki page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM