Зміст курсу
Introduction to Redis
Introduction to Redis
List
A list preserves the order of elements and supports access by index. This makes it suitable for tasks such as creating task queues, logging events, or storing real-time data like recent user activities.
The Main Commands for Working With Lists in Redis
The key commands for working with lists in Redis include several categories, each addressing a specific task: adding, retrieving, reading, modifying, deleting elements, and managing the list's size.
Adding Elements to a List
Redis provides two commands for adding elements to a list: LPUSH
and RPUSH
, which add elements to the beginning and the end of the list, respectively.
After executing these commands, the tasks
list will look like this: ["Task2", "Task1", "Task3", "Task4"]
.
Removing Elements from a List
To remove elements from a list, Redis provides two commands: LPOP
and RPOP
, which remove and return elements from the beginning and the end of the list, respectively.
After executing these commands, the tasks
list will look like this: ["Task1", "Task3"]
.
Reading Elements from a List
To read elements from a list, Redis offers the commands LRANGE
, LINDEX
, and LLEN
. LRANGE
and LINDEX
allow you to retrieve elements by index, while LLEN
returns the total number of elements in the list.
LRANGE
will return all elements:["Task1", "Task3"]
;LINDEX
will return "Task1";LLEN
will return 2.
Modifying a List
To modify an element in a list, use the LSET
command, which allows you to update an element at a specific index.
After executing this command, the tasks
list will be: ["Task1", "UpdatedTask"]
.
Trimming a List
The LTRIM
command is used to trim a list, keeping only the elements within a specified range. All other elements will be removed.
If the tasks
list was: ["Task1", "Task2", "Task3"]
, after executing the command, the list will contain: ["Task1", "Task2"]
.
Дякуємо за ваш відгук!