Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a List into another List in Redis

Tags:

redis

Is this possible to have a datastructure inside another data structure? So far I can only insert a string or a number into a list.

A desired data structure would be a to have a list where each component is also a list.

like image 500
denniss Avatar asked Dec 02 '11 18:12

denniss


People also ask

How do I add a list in Redis?

Creating Lists 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.

Can we store a list in Redis?

Redis lists are lists of strings, sorted by insertion order. It's possible to add elements to a Redis list by pushing items to the front and back of the list with LPUSH/RPUSH and can pop items from the front and back of the list with LPOP/RPOP.

What is LPOP in Redis?

Removes and returns the first elements of the list stored at key . By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.

What is Lpush Redis?

Redis LPUSH command is used to insert all the specified values at the head of the list stored at key. It is created as an empty list before performing the push operations if tje key does not exist. When key holds a value that is not a list, an error is returned.


2 Answers

Simple answer: No, Redis list members can only be strings.

Complex answer: There is a lot you can do with strings. You can serialize a list into a string using a number of different formats (JSON, XML, CSV, language specific serialization, etc.). Redis can hold pretty large string values so you can store just identifiers which point to a key containing the real values, or you can have a serialized list of the values themselves.

like image 97
Carl Zulauf Avatar answered Sep 18 '22 18:09

Carl Zulauf


Redis have a very limited set of data types which can be found here: Data types

What you can do is to store the names of element lists in a list so it will effectively act as list of lists.

like image 33
Aamir Khan Avatar answered Sep 20 '22 18:09

Aamir Khan