Une liste est un ensemble de paires clé-valeur liées les unes aux autres.
LPUSH
etRPUSH
sont les deux commandes pour travailler avec les listes.
Vous utilisez la commandeLPUSH <listkey> <value>
pour créer le premier élément.
Exemple:
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.
More redis tutorials:
- Introduction to Redis
- How to install Redis
- First steps with Redis
- Redis Lists
- Using Redis Sets
- How to use Redis Sorted Lists
- How to use Redis Hashes
- Redis Publish/subscribe