A list is a set of key-values pairs linked to each other.
RPUSH are the two commands to work with lists.
You use the command
LPUSH <listkey> <value>
to create the first item.
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>.
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
Get and remove the last item in a list with
RPOP <listkey>. Do the same with the first item with
Remove multiple items from the list using the
You can limit how long a list is using
LTRIM names 0 1 cuts the list to just 2 items, item at position 0 (the first) and item at position 1.
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:
- How to install Redis
- How to use Redis Sorted Lists
- First steps with Redis
- Introduction to Redis
- Redis Publish/subscribe
- Redis Lists
- Using Redis Sets
- How to use Redis Hashes