I have to store some machine details in redis. As there are many different machines i am planning to use the below structure

server1 => {name => s1, cpu=>80}
server2 => {name => s2, cpu=>40}

I need to store more than one value against the key CPU. Also i need to maintain only the last 10 values in the list of values against cpu

1) How can i store a list against the key inside the hash?

2) I read about ltrim. But it accepts a key. How can i do a ltrim for key cpu inside server1?

I am using jedis.

4 years past, and now, you can use the redis-protobuf module to save nested data structures to Redis. Disclaimer: I'm the author of this module. – for_stack Sep 9 at 15:22

It's possible to do this with Redisson framework. It allows to store a reference to Redis object in another Redis object though special reference objects which handled by Redisson.

So your task could be solved using List inside Map:

RMap<String, RList<Option>> settings = redisson.getMap("settings");
RList<Option> options1 = redisson.getList("settings_server1_option");
options1.add(new Option("name", "s1"));
options1.add(new Option("cpu", "80"));
settings.put("server1", options1);
RList<Option> options2 = redisson.getList("settings_server2_option");
options2.add(new Option("name", "s2"));
options2.add(new Option("cpu", "40"));
settings.put("server2", options2);
// read it
RList<Option> options2Value = settings.get("server2");

Or using Map inside Map:

RMap<String, RMap<String, String>> settings = redisson.getMap("settings");
RMap<String, String> options1 = redisson.getMap("settings_server1_option");
options1.put("name", "s1");
options1.put("cpu", "80");
settings.put("server1", options1);
RMap<String, String> options2 = redisson.getMap("settings_server2_option");
options2.put("name", "s2");
options2.put("cpu", "40");
settings.put("server2", options1);
// read it
RMap<String, String> options2Value = settings.get("server2");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.9.20.34984