Redis Lists
A list is a set of key-values pairs linked to each other.
LPUSH
and RPUSH
are the two commands to work with lists.
You use the command LPUSH <listkey> <value>
to create the first item.
Example:
LPUSH names "Flavio"
Then subsequent items can be added at the bottom of the list: RPUSH <listkey> <value>
Or at the top of the list with LPUSH <listkey> <value>
.
Example:
LPUSH names "Flavio"
LPUSH names "Syd"
RPUSH names "Roger"
You can add duplicate values into a list.
LPUSH names "Flavio"
LPUSH names "Flavio"
RPUSH names "Flavio"
A list can hold a big number of items, more than 4 billions.
Count how many items are in a list with LLEN <listkey>
.
Get and remove the last item in a list with RPOP <listkey>
. Do the same with the first item with LPOP
.
Remove multiple items from the list using the LREM
command.
You can limit how long a list is using LTRIM
.
LTRIM names 0 1
cuts the list to just 2 items, item at position 0 (the first) and item at position 1.
Using LRANGE
you can get the items in the list.
LRANGE names 0 100
returns items starting at position 0 (the beginning), ending at position 100.
LRANGE names 0 0
returns the item in position 0 (the first).
LRANGE names 2 2
returns the item in position 2.
LRANGE names 0 -1
lists all items.
See all the lists commands here.
THE VALLEY OF CODE
THE WEB DEVELOPER's MANUAL
You might be interested in those things I do:
- Learn to code in THE VALLEY OF CODE, your your web development manual
- Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
- I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
- Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
- Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
- Find me on X