Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Redis List Data Structure Operations

Tech May 15 2

Redis List Data Structure

Redis lists are string-based collections that maintain insertion order. Elements can be added to either the head (left) or tail (right) of a list. A Redis list can hold up to 2^32 - 1 elements, enabling storage of more than 4 billion items per list.

Getting Help for List Commands

You can use the following command to view list-related operations:

127.0.0.1:6379> help @list

Creating Lists

Inserting Elements from the Left

Use LPUSH to add one or more elements to the left of the list:

127.0.0.1:6379> LPUSH mylist ddfff 21 nb
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nb"
2) "21"
3) "ddfff"

Appending Elements to the Right

Use RPUSH to add one or more elements to the right of the list:

127.0.0.1:6379> RPUSH mylist qq ll nn
(integer) 6
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nb"
2) "21"
3) "ddfff"
4) "qq"
5) "ll"
6) "nn"

Prepending Elements Only If List Exists

LPUSHX adds elements to the left only if the list already exists:

127.0.0.1:6379> LPUSHX mylist ww ee
(integer) 8
127.0.0.1:6379> LPUSHX youlist ww ee
(integer) 0

Appending Elements Only If List Exists

RPUSHX appends elements to the right only if the list exists:

127.0.0.1:6379> RPUSHX mylist rr nn
(integer) 10
127.0.0.1:6379> RPUSHX youlist rr nn
(integer) 0

Inserting Elements Before or After a Pivot

Use LINSERT to insert an element before or after a specific value:

127.0.0.1:6379> LINSERT mylist before ddfff www
(integer) 8
127.0.0.1:6379> LINSERT mylist after ddfff eee
(integer) 9

Removing Elements

Removing Elements by Value

LREM removes a specified number of occurrences from the list, starting from the left:

127.0.0.1:6379> LREM mylist 2 we
(integer) 2

Modifying Elements

Setting a Value at a Specific Index

Use LSET to update the value at a specific position:

127.0.0.1:6379> LSET mylist 1 clearlove7

Trimming Lists

Keeping Only a Range of Elements

LRANGE shows elements within a specified range, and LRANGE combined with LRANGE can be used to verify the result:

127.0.0.1:6379> LTRIM mylist 1 4

Querying Lists

Retrieving a Range of Elements

127.0.0.1:6379> LRANGE mylist 0 -1

Getting an Element by Index

127.0.0.1:6379> LINDEX mylist 0

Getting the Length of a List

127.0.0.1:6379> LLEN mylist

Removing and Returning Elements

  • From the left: LPOP
  • From the right: RPOP

Moving Elements Between Lists

RPOPLPUSH removes the last element from one list and pushes it to another:

127.0.0.1:6379> RPOPLPUSH mylist testlist

Blocking Pop Operations

Blocking Pop from the Left

BLPOP blocks until an element becomes available in the list:

# Client A
127.0.0.1:6379> BLPOP testlist 100

# Client B
127.0.0.1:6379> LPUSH testlist we

# Client A
127.0.0.1:6379> BLPOP testlist 100
1) "testlist"
2) "we"
Tags: Redis

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.