36

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.

3
  • 4
    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. Commented Sep 9, 2019 at 15:22
  • @for_stack this is all well and good. How do we do this customization in redislabs deployments? Commented Nov 11, 2020 at 8:49
  • @eigenfield Sorry, but I'm not familiar with redislabs deployments, but I think you can simply build the module, and config it with loadmodule configuration. Also you can ask @Itamar Haber for help. I'm sure his advice will be very helpful :) Commented Nov 11, 2020 at 9:47

3 Answers 3

40

Redis' data structures cannot be nested inside other data structures, so storing a List inside a Hash is not possible. Instead, use different keys for your servers' CPU values (e.g. server1:cpu).

Sign up to request clarification or add additional context in comments.

1 Comment

Redis' data structures cannot be nested inside other data structures, so storing a List inside a Hash is not possible. Perfectly phrased I wish the docs said this?
8

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");

Diclamer: I'm a developer of Redisson

2 Comments

so I have to create separate RLists, and name them? Then when I remove an entry from RMap I have to somehow clean or remove RList as well? I have to manage that? The RMap might be temporary, pollution is certainly going to be increasing. The RList then has to be named before you add to it, so the key has to be supplied to the RList as name. Seems like a nice big mess. Thinking of sticking with SQL. Seems impossible to manage stuff.
This answer needs a disclaimer that Nikita Koksharov is the - Founder and Lead Developer @ Redisson
5

You can encode/stringify push the data, while pulling data you can decode/parse the data.

Encode -> Decode

Stringify -> Parse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.