Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis storing list inside hash

Tags:

java

redis

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.

like image 826
Shabin Hashim Avatar asked Mar 23 '15 05:03

Shabin Hashim


People also ask

Can I store a list in Redis?

Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush command or the tail (the “right” end) with rpush . You can also use lpush or rpush to create a new list: lpush key value.

Does Redis support nested hash?

Redis doesn't support nested data structures, and specifically it doesn't support a Hash inside a Hash :) You basically have a choice between two options: either serialize the internal Hash and store it in a Hash field or use another Hash key and just keep a reference to it in a field of the outer Hash.

Can Redis store Hashmap?

Redis doesn't support storing hash inside hash. But there is REDIS as a JSON store that can store JSON in REDIS, It allows storing, updating and fetching JSON values from Redis keys.

Does Redis store everything as string?

As we have mentioned earlier that Redis is a key-value store, but that doesn't mean that it stores only string keys and string values. Redis supports different types of data structures as values.


1 Answers

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).

like image 162
Itamar Haber Avatar answered Oct 04 '22 03:10

Itamar Haber