1. How to install and protect redis on ubuntu18.04
  2. How do I connect to the Redis database
  3. How to manage Redis database and Keys
  4. How do I manage replicas and clients in Redis
  5. How do I manage strings in Redis
  6. How do I manage lists in Redis
  7. How do I manage Hashes in Redis
  8. How do I manage Sets in Redis
  9. How to manage Sorted Sets in Redis
  10. How do I run transactions in Redis
  11. How do I invalidate keys in Redis
  12. How do I solve problems in Redis
  13. How do I change the Redis configuration from the command line
  14. Introduction to Redis data types

introduce

Redis is an open source key-value data store in memory. In Redis, a list is a collection of strings sorted in insertion order, similar to a linked list. This tutorial shows you how to create and use elements in a Redis list.

How to use this guide

This guide is written as a cheat sheet with complete examples. We encourage you to skip to any section that is relevant to the task you are trying to accomplish.

The commands shown in this guide have been tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, follow step 1 of our guide on how to install and secure Redis on Ubuntu 18.04. We will demonstrate the behavior of these commands by running them using the Redis command line interface. Note that the exact output of some commands may be different if you use another Redis interface, such as Redli. redis-cli

Alternatively, you can provide a managed Redis database instance to test these commands, but be aware that some of the commands in this guide may not work as described depending on the level of control allowed by the database provider. To configure the DigitalOcean managed database, follow our managed database product documentation. You must then install Redli or set up a TLS tunnel to connect to the managed database over TLS.

Creating Lists

A key can hold only one list, although any list can hold four billion elements. Redis reads the list from left to right, and you can use commands to add new list elements to the beginning of the list (the “left” end), or lpush can use the tail (the “right” end) to add a new element, Rpush. You can also create new lists using lpush or rpush:

lpush key valueCopy the code

Both commands output an integer that shows how many elements there are in the list. To illustrate, run the following command to create a list of “I think it’s me” quotes:

lpush key_philosophy1 "therefore"
lpush key_philosophy1 "think"
rpush key_philosophy1 "I"
lpush key_philosophy1 "I"
rpush key_philosophy1 "am"Copy the code

The output of the last command will look like:

(integer) 5Copy the code

Note that you can add multiple list elements using a single LPUSH or Rpush statement:

rpush key_philosophy1 "-" "Rene" "Decartes"Copy the code

The lpushx and rpushx commands are also used to add elements to a list, but only work if the given list already exists. If either command fails, it returns (integer) 0:

Rpushx KEY_ZARA "Happiness" "is" "the" "highest" "Good" "-- "Aristotle" (INTEGER) 0Copy the code

To change an existing element in the list, run the lset command, then enter the key name, index and new value for the element to be changed:

lset key_philosophy1 5 "sayeth"Copy the code

An attempt to add a list element to an existing key that does not contain a list causes a data type conflict and returns an error. For example, the following set command creates a key that holds a string, so the following attempt to add a list element to it lpush will fail:

set key_philosophy3 "What is love?"
lpush key_philosophy3 "Baby don't hurt me"


(error) WRONGTYPE Operation against a key holding the wrong kind of valueCopy the code

There is no way to convert the Redis key from one data type to another, so to become the KEY_ZARA List, you need to delete the key and start over using the lPUSHor rpush command.

Retrievals Elements from a List.

To retrieve the range of items in the list, use the lrange command, followed by a start offset and a stop offset. Each offset is a zero-based index, with 0 representing the first element in the list, 1 representing the next, and so on.

The following command returns all elements from the sample list created in the previous section:

lrange key_philosophy1 0 7


1) "I"
2) "think"
3) "therefore"
4) "I"
5) "am"
6) "sayeth"
7) "Rene"
8) "Decartes"Copy the code

The offset lrange passed can also be negative. When used in this case, -1 represents the last element in the list, -2 represents the penultimate element in the list, and so on. The following example returns the last three elements saved in the list key_NEZARA 1:

lrange key_philosophy1 -3 -1


1) "I"
2) "am"
3) "sayeth"Copy the code

To retrieve individual elements from the list, use the lindex command. However, this command requires that you provide the index of the element as a parameter. As with lrange, indexes start from zero, which means that the first element is at index 0, the second element is at index 1, and so on:

lindex key_philosophy1 4


"am"Copy the code

To find out how many elements there are in a given list, use the following llen command, which is short for “l ist len GTH” :

llen key_philosophy1


(integer) 8Copy the code

Llen returns an error if the value stored on a given key does not exist.

Removes elements from the list

The lREM command deletes the first of the defined degrees that match the given value. To experiment with this, create the following list:

rpush key_Bond "Never" "Say" "Never" "Again" "You" "Only" "Live" "Twice" "Live" "and" "Let" "Die" "Tomorrow" "Never" "Dies"Copy the code

The following LREM example will remove the first occurrence of “Live” for this value:

lrem key_Bond 1 "Live"Copy the code

This command prints the number of elements removed from the list:

(integer) 1Copy the code

The number passed to the LREM command can also be negative. The following example removes the last two occurrences of “Never” :

lrem key_Bond -2 "Never"


(integer) 2Copy the code

The lpop command removes the list and returns the first or “leftmost” element:

lpop key_Bond


"Never"Copy the code

Again, to remove and return the last or “rightmost” element from the list, use RPOP:

rpop key_Bond


"Dies"Copy the code

Redis also includes the rpoplpush command, which removes the last element from a list and pushes it to the beginning of another list:

rpoplpush key_Bond key_AfterToday


"Tomorrow"Copy the code

If the source key passed to rpoplpush is the same as the target key, it will essentially rotate the elements in the list.

conclusion

This guide details the many commands you can use to create and manage lists in Redis. If you would like to outline other relevant commands, parameters, or procedures in this guide, please ask questions or make suggestions in the comments below.

For more information about the Redis command, see AboutHow to manage the Redis databaseSeries of tutorials. The author:Distributed programmingReference:zthinker.com/If you like this article, please long press the QR code, followDistributed programming .