This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Lists are unique in that they can store N repeatable elements under a single key. You’re essentially replacing the value on the right-hand side of string with a list of elements.
1. String structure:
Because of the repeatability of lists, in the following structure example, the first and last elements can be repeated.
[
"user": ["camellia1"."camellia2"."camellia3"]]Copy the code
Hash hash commands (PHP+ Linux)
1: Linux command usage
(1) : After rpush adds a value to the list, it returns the current length (including the number of elements). (Here is the right side add, the left side add the same)
Rpush list-key camellia1 // return (integer1 rpush list-key camellia2 // return (integer2 rpush list-key camellia3 // return (integer) 3
Copy the code
(2) : Lrange obtains values within a certain range and returns all elements within the range successfully. So 0 is the starting position, -1 is the ending position. From 0 to -1, that means you get all the values.
lrange list-key 0 -1
Copy the code
Output:
1) "camellia1"
2) "camellia2"
3) "camellia3"
Copy the code
(3) : Lindex retrieves an element from the list.
Lindex list-key 1 // returns"camellia2"
Copy the code
(4) : Delete an element to the left of the LPOP list, and return the deleted element successfully
Hdel hash-key camellia1 // returns (integer1)Copy the code
2: PHP use:
(1) : add, successful return autoincrement primary key (left and right add the same)
$r = $redis->rpush("list-key"."camellia1"); // Add a value from the right of the list item var_dump($r); // There is an element in the list that returns int 1$res = $redis->rpush("list-key"."camellia2"); // Contains 2 elements, return int 2 var_dump($res);
Copy the code
(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"}
Copy the code
(3) : obtain the value corresponding to the key.
$result = $redis->lindex("list-key".1);
var_dump($result); / / return camellia2
Copy the code
(4) : LPOP deletes an element from the left, or rPOP deletes an element from the right, successfully returning the value of the deleted element
r=r = r=redis->lpop(“list-key”); var_dump($r); // Return the deleted value camellia1
Three: simple queue implementation based on redis list list type
$redis = new Redis();
$redis->connect("127.0.0.1".6379);
/ / into the queue
// Random number an ID, mimicking the user ID
$userId = mt_rand(000000.999999);
$redis->rpush("QUEUE_NAME",json_encode(["user_id"= >$userId]));
// Random number an ID, mimicking the user ID
$userId = mt_rand(000000.999999);
$redis->rpush("QUEUE_NAME",json_encode(["user_id"= >$userId]));
// Random number an ID, mimicking the user ID
$userId = mt_rand(000000.999999);
$redis->rpush("QUEUE_NAME",json_encode(["user_id"= >$userId]));
echo "Data queued successfully";
echo "<br>";
// Check the queue
$res = $redis->lrange("QUEUE_NAME".0.1000);
echo "Current queue data is:";
print_r($res);
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -";
echo "<br>";
/ / out of the queue
$redis->lpop("QUEUE_NAME");
echo "Data dequeued successfully";
echo "<br>";
// Check the queue
$res = $redis->lrange("QUEUE_NAME".0.1000);
echo "Current queue data is:";
print_r($res);
? >
Copy the code
Other commonly used redis List commands
The serial number
Commands and Description
BLPOP key1 [KEY2] timeout Removes and retrieves the first element of the list. If there are no elements in the list, the list is blocked until the wait times out or a popup element is found.Copy the code
BRPOP key1 [KEY2] timeout Removes and retrieves the last element of the list. If there are no elements in the list, the list is blocked until the wait times out or a popup element is found.Copy the code
BRPOPLPUSH sourceDestination timeout Pops a value from a list, inserts the pop-up element into another list, and returns it. If the list has no elements, it blocks until the wait times out or a popup element is found.Copy the code
LINDEX Key Index Retrieves the elements in the list by indexCopy the code
LINSERT key BEFORE | AFTER the pivot value in a list of elements BEFORE or AFTER the inserted elementCopy the code
LLEN key Obtains the list lengthCopy the code
The LPOP key moves out and gets the first element of the listCopy the code
LPUSH key value1 [value2] Inserts one or more values into the head of the listCopy the code
LPUSHX key Value Inserts a value into an existing list headerCopy the code
LRANGE key start stop gets the elements in the specified range of the listCopy the code
LREM Key count value Removes list elementsCopy the code
LSET Key Index Value Sets the value of a list element by indexCopy the code
LTRIM Key start stop To trim a list, that is, to keep only the elements within a specified range. All elements that are not within a specified range are deleted.Copy the code
RPOP Key removes the last element in the list and returns the removed element.Copy the code
RPOPLPUSH sourceDestination removes the last element of the list, adds it to another list and returnsCopy the code
RPUSH key value1 [value2] Adds one or more values to the listCopy the code
RPUSHX key value Adds a value to an existing listCopy the code
The above is basically the basic content of the redis List type THAT I read. If there is any deficiency, please kindly point out.
For good suggestions, please enter your comments below.
Welcome to my blog guanchao.site
Welcome to applets: