Redis (nine) list list type

The function of the list is very unique, it can store N repeatable elements under a key. In fact, the value on the right side of the string type is replaced with a list of multiple elements.

One: String structure:

Due to the repeatability of the list, in the structure example below, the first and last elements can be repeated.

[
    "user":[
        "camellia1",
        "camellia2",
        "camellia3"
    ]
]

Two: Hash type related operation commands (PHP+linux)

1: Use of Linux commands

(1): After rpush adds a value to the list, it will return the current length (including the number of elements). (Here is the addition on the right side, the same for adding on the left side)

rpush list-key camellia1                     // 返回(integer) 1
rpush list-key camellia2                     // 返回(integer) 2
rpush list-key camellia3                     // 返回(integer) 3

(2): lrange gets a value in a certain range, and successfully returns all elements in the range. Here, 0 represents the starting position, and -1 represents the ending position. From 0 to -1, it means to take out all the values.

lrange list-key 0 -1

Output:

1) "camellia1"
2) "camellia2"
3) "camellia3"

(3): lindex gets an element in the list.

lindex list-key 1                   // 返回"camellia2"

(4): Delete an element from the left of the lpop list, and successfully return the deleted element

hdel hash-key camellia1             // 返回(integer) 1

2: PHP use:

(1): Add, return to the auto-incrementing primary key successfully (the left and right additions are consistent)

$r = $redis->rpush("list-key","camellia1"); // 从列表右侧添加一个值item
    var_dump($r);// 此时,列表中有一个元素,返回int 1
 
    $res = $redis->rpush("list-key","camellia2"); // 含有2个元素,返回int 2
    var_dump($res);

(2): We get all the values

$result = $redis->lrange("list-key", 0 ,-1);
var_dump($result);  // 输出:array(2) { [0]=> string(9) "camellia1" [1]=> string(9) "camellia2" }

(3): Get the value corresponding to the key.

$result = $redis->lindex("list-key",1);
var_dump($result);              // 返回camellia2

(4): lpop deletes an element from the left, or use rpop to delete an element from the right, and successfully returns the value of the deleted element

$r = r e d i s − > l p o p ( " l i s t − k e y " ) ; v a r d u m p ( redis->lpop("list-key"); var_dump( r e d i s ->lpop("listkey");v a rdu m p ( r); // return the deleted value camellia1

Three: Simple queue implementation based on redis list list type

<?php
    $redis = new Redis();
    $redis->connect("127.0.0.1", 6379);
    // 进队列
    // 随机数一个id,模仿用户id
    $userId = mt_rand(000000, 999999);
    $redis->rpush("QUEUE_NAME",json_encode(["user_id" => $userId]));
    // 随机数一个id,模仿用户id
    $userId = mt_rand(000000, 999999);
    $redis->rpush("QUEUE_NAME",json_encode(["user_id" => $userId]));
    // 随机数一个id,模仿用户id
    $userId = mt_rand(000000, 999999);
    $redis->rpush("QUEUE_NAME",json_encode(["user_id" => $userId]));
    echo "数据进队列成功 
";
    echo "<br>";
    // 查看队列
    $res = $redis->lrange("QUEUE_NAME", 0, 1000);
    echo "当前队列数据为: 
";
    print_r($res);
    echo "----------------------------- 
";
    echo "<br>";
    // 出队列
    $redis->lpop("QUEUE_NAME");
    echo "数据出队列成功 
";
    echo "<br>";
    // 查看队列
    $res = $redis->lrange("QUEUE_NAME", 0, 1000);
    echo "当前队列数据为: 
";
    print_r($res);
?>

Four: other commonly used redis List commands

Serial number

Command and description

BLPOP   key1 [key2 ] timeout 
  移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOP   key1 [key2 ] timeout 
  移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOPLPUSH   source destination timeout 
  从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
LINDEX   key index 
  通过索引获取列表中的元素
LINSERT   key BEFORE|AFTER pivot value 
  在列表的元素前或者后插入元素
LLEN   key 
  获取列表长度
LPOP   key 
  移出并获取列表的第一个元素
LPUSH   key value1 [value2] 
  将一个或多个值插入到列表头部
LPUSHX   key value 
  将一个值插入到已存在的列表头部
LRANGE   key start stop 
  获取列表指定范围内的元素
LREM   key count value 
  移除列表元素
LSET   key index value 
  通过索引设置列表元素的值
LTRIM   key start stop 
  对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
RPOP   key 
  移除列表的最后一个元素,返回值为移除的元素。
RPOPLPUSH   source destination 
  移除列表的最后一个元素,并将该元素添加到另一个列表并返回
RPUSH   key value1 [value2] 
  在列表中添加一个或多个值
RPUSHX   key value 
  为已存在的列表添加值

The above is basically the basic content of the redis List type I read. There are deficiencies, please point out the big guys.

Have a good suggestion, please enter your comment below.

Welcome to personal blog
https://guanchao.site

Welcome to the Mini Program:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39708228/article/details/113383021