list series functions

The list series function in Redis records 18. The list structure is mainly used for queues.

1. Insert data (6 functions)

functionfunction
lPush()Push data from the left side of the list
lPushx()When the list exists, data is pushed from the left side of the list
rPush()Push data from the right side of the list
rPushx()When the list exists, data is pushed from the right side of the list
lSet()Modify list elements according to index position
linsert()Add to the position before or after the specified element

//Add elements from the left side of the list
$redis->lpush('lx',1,2,3,4,5,6);

List structure
127.0.0.1:6379> lrange lx 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"

lpush The element added by the function, from the position with index 0 on the left, is the last element added.

rpush Function and lpush Instead, insert the element into the list from the right, and index 0 is the first element added

$redis->rpush('lx',1,2,3,4,5,6);

127.0.0.1:6379> lrange lx 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"

The lPushx() and rPushx() functions are only available when the queue exists, Chinese Meteorology To add elements. Use the same method as above.

linsert() insert function

In the list, find parameter 3, add 14 after it and 12 before it. If this value does not exist, the addition fails.
$result=$redis->linsert("lx",Redis::AFTER,4,14);
$result=$redis->linsert("lx",Redis::BEFORE,4,12);

127.0.0.1:6379> lrange lx 0 -1
1) "1"
2) "2"
3) "3"
4) "12"
5) "4"
6) "14"
7) "5"
8) "6"

lset modifies list elements based on the index

#Modify the element value according to the index
LSET key index value

//Change the element value of index 3 to 7
$redis->lset("lx",3,7);

127.0.0.1:6379> lrange lx 0 -1
1) "1"
2) "2"
3) "3"
4) "7"
5) "4"
6) "14"
7) "5"
8) "6"

2. Query data

functionfunction
lrangeQuery the elements in the list according to the index range
lindexReturns the element value according to the index
llenGet the number of elements in the list

//Connect to Redis
$redis=new Redis();
$redis->connect('127.0.0.1',6379,1,null,100);
$redis->auth("lafenfen");

//Push 4 pieces of data to the right of list list1
$redis->rpush('list1',1,2,3,4);

//Query all elements in list1
$redis->lrange('list1',0,-1);

//Return results
<pre>Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

//See how many elements there are in the list
$redis->llen('list1')

//View element values for index 0
$redis->lindex('list1',0)

3. Eject and block eject

functionfunction
lpopPop up elements from the left side of the list
rpopPop up elements from the right side of the list
blpopMulti list left blocking pop-up
brpopMulti list right blocking pop-up

These four functions, together with the previously learned insert data function, can form a queue function.

lpop and rpop can add a parameter when Redis is greater than or equal to version 6.2 to specify the element of the specified data to pop up each time. In the current version 5 I use, only one element can pop up at a time.

Usage in version 6.2

lpop  key [count]
rpop key [count]

Usage demonstration in version 5

$redis->lpop('list1')  //Pop up an element from the left side of the list
$redis->rpop("list1");  //Pop up an element from the right side of the list

blpop and lrpop are two blocking pop-up functions that can specify multiple queues
For example, there are three queues, LIST1, List2 and list3. According to their order, the leftmost list has the highest priority.
When popping up, the element will pop up from list1 first, and then pop up in list2 until it is empty.
If all three lists are empty, it will wait for 10 seconds before disconnecting.

$redis->blpop(['list1','list2','list3'],10);

<pre>Array
(
    [0] => list1
    [1] => a
)

Finally, there are two pop-up functions

functionfunction
rpoplpush(a,b)Pop up the element on the last side of the a list and put it on the left side of the b list
brpoplpush(a,b)Block pop-up the last element of a list and put it on the left side of b list

The returned value of successful operation is the element moved.

These two element parameters are two lists. Now show me

list1 and list2 The contents of the list elements are as follows
127.0.0.1:6379>    lrange list1 0 -1
1) "d"
2) "a"
3) "a"
127.0.0.1:6379> lrange list2 0 -1
1) "1"
2) "2"
3) "3"
4) "4"


Now will list1 Medium a Put list2 List left
$redis->rpoplpush('list1','list2');

Print list2
127.0.0.1:6379> lrange list2 0 -1
1) "a"
2) "1"
3) "2"
4) "3"
5) "4"


brpoplpush It means that a timeout parameter is added at the end. The usage is the same.

These two functions can be used as loop listening function in practical application
I created a list called listenIp, which contains the IP addresses of three servers.

$redis->rpush('listenIp','192.168.100.4','192.168.100.5','192.168.100.6');

$list=$redis->lrange('listenIp',0,-1);


Print results

<pre>Array
(
    [0] => 192.168.100.4
    [1] => 192.168.100.5
    [2] => 192.168.100.6
)

Now read the list circularly, fill in the two parameters as listenIp, read the end element each time, and then put it to the left of the list. Achieve the effect of circulation.

$ip=$redis->rpoplpush('listenIp','listenIp');
//Execute ping command
shell_exec("ping -c 3 $ip");

4. Delete element function

functionfunction
lremDeletes the specified number of elements
ltrimDeletes elements outside the specified range

Redis Syntax: delete a specified number of value element
lrem list1 count value

among count >0  It will be deleted from the left side of the list count individual value value
count<0   It will be deleted from the right side of the list count individual value value
count=0,Put in the list value Delete all elements


Now list list1,There are 9 elements, arranged from the left as follows:
127.0.0.1:6379> lrange list1 0 -1
1) "d"
2) "b"
3) "b"
4) "a"
5) "a"
6) "a"
7) "b"
8) "c"
9) "d"

Now delete 2 from the left b Element, note here:
stay phpredis In the client lrem In function
 The deleted elements are placed at the position of parameter 2, and the number is placed last. Note the parameter position and Redis In server lrem The use of is different
$redis->lrem('list1','b',2)

Now look at the list as follows: the first two b The element value is gone.
127.0.0.1:6379>  lrange list1 0 -1
1) "d"
2) "a"
3) "a"
4) "a"
5) "b"
6) "c"
7) "d"

ltrim deletes elements outside the specified range. When used, specify the index range start end. Elements within this range will be retained and elements outside the range will be deleted.

In front list1 Inside, there are these elements now
127.0.0.1:6379>  lrange list1 0 -1
1) "d"
2) "a"
3) "a"
4) "a"
5) "b"
6) "c"
7) "d"

Now I only keep the three elements from index 0 to index 2.
$redis->ltrim('list1',0,2);

Now? list1 The element in the returns the result
<pre>Array
(
    [0] => d
    [1] => a
    [2] => a
)

Added by ChessclubFriend on Mon, 24 Jan 2022 16:39:50 +0200