Preface
Reasoning feel that the list in Redis is nothing to write, if the operation of a few commands is too tedious, so in the end I will be based on the Redis list data type special properties, while comparing the mature Message Queuing product rabbitmq, using Redis to implement a message queue.
To make this article more attractive, I'll introduce the basic properties of list in Redis, and why use the list type in Redis, why use Message Queuing, why use Redis to implement Message Queuing without RABBITMQ? , so far, if you are a big coffee, Daniel, great God, Big Uncle! Don't listen to me, Close Page and forget me! —_—
First answer the 4 questions in the preface
1. What are the basic properties of the list in Redis?
The list data structure is a linked list structure, which means that regardless of the amount of data, tail-to-side operation of the data is very fast, the list of capacity is 2 of the 32 square minus 1 elements, that is, 4,294,967,295 elements of a number.
2. Why use the list data type in Redis?
Rely on the advantages of Redis in-memory operation data, but also provide some practical and unique API control data, with simple, fast, and can achieve unique data features such as reading and writing, time axis data, comment list, message delivery and so on, but also provide easy paging, read and write operations. You use No.
3. Why use Message Queuing?
To give a simple example, the function is this, you want to render the page to the user to see, before you render the page has a very complex time-consuming operation to operate, but this operation does not affect the page rendering data, nor as the page rendering data.
Scenario One: Finish the operation, render the page.
Scenario Two: The data to be calculated, thrown into the persistent message queue, do not do time-consuming operations, directly render the page. Then use another program to perform operations on the data in the message queue separately.
Obviously, scenario two is the best answer, you use no message queue.
4, why not use the mature rabbitmq and use Redis implementation of Message Queuing?
RABBITMQ only focus on data FIFO, no data priority concept, if you want to give that data to deal with the first privilege, then embarrassed, I do not support, but RABBITMQ can also be flexible to deal with, is to establish multiple queues with program routing to implement this privileged function. Then the Redis implementation of the message queue, can be flexibly controlled, followed by a demonstration.
a detailed command of the list in Redis
1. Data insertion command for list lists in Redis: Lpush,rpush,linsert
127.0.0.1:6379>rpush mylist 1---Result: (integer) 1
127.0.0.1:6379>rpush mylist 2---Result: (integer) 2
127.0.0.1:6379>rpush mylist 3---rpush command: Insert 3 data from the right to the MyList list, and return the value to the current list's capacity. The result is: (integer) 3
127.0.0.1:6379>lrange mylist 0-1---lrange command: View the data in the MyList list, 0 start position, 1 end position, and end position at-1, which represents the last position of the list, that is, view all. The result is:1> "1" 2> "2" 3> "3"
127.0.0.1:6379>lpush mylist 0---lpush command: Insert a data 0 data from the left to the MyList list
127.0.0.1:6379>lrange mylist 0-1---result is:1> "0" 2> "1" 3> "2" 4> "3"
127.0.0.1:6379>linsert mylist after 3 4---The Linsert command, the expression is Linsert key Before|after pivot value; This command means a list of keys for MyList To find data with a value of 3, after which a value of 4 is inserted.
127.0.0.1:6379>lrange MyList 0-1---results for:1> "0" 2> "1" 3> "2" 4> "3" 5> "4"
127.0.0.1:6379>linsert mylist before 0-1---means: look for data with a value of 0 in the list of key mylist, and insert a value of 1 before it.
127.0.0.1:6379>lrange mylist 0-1---result for:1> "-1" 2> "0" 3> "1" 4> "2" 5> "3" 6> "4"
127.0.0.1:6379>lisert mylist after 5 8---The result is:-1, because the MyList list does not have data with a value of 5, so nothing is done and the status value-1 is returned. If key does not exist, an error message is returned.
127.0.0.1:6379>lrange mylist 0-1---result for:1> "-1" 2> "0" 3> "1" 4> "2" 5> "3" 6> "4"
2. Data deletion command for list in Redis: Lpop,rpop
127.0.0.1:6379>lpop mylist---lpop command: Remove a piece of data from the left side of the list while outputting the deleted data, where the result is 1
127.0.0.1:6379>lrange MyList 0-1---results for:1> "0" 2> "1" 3> "2" 4> "3" 5> "4"
127.0.0.1:6379>rpop mylist---rpop command: Remove a piece of data from the right side of the list and output the deleted data, where the result is 4
127.0.0.1:6379>lrange mylist 0-1---result is:1> "0" 2> "1" 3> "2" 4> "3"
127.0.0.1:6379>ltrim mylist 1 3----ltrim command: Retains the value of the set of two subscript intervals, deleting all values that are not in its range. 1 is the subscript value to start preserving, and 3 is the subscript value that ends the reservation.
127.0.0.1:6379>lrange mylist 0-1---result is:1> "1" 2> "2" 3> "3"
3. Data View command for the list in Redis: Lrange,llen,lindex
127.0.0.1:6379>llen mylist---llen command: Return the length of the list, here MyList only 4 data left, so the output is 4
127.0.0.1:6379>lindex mylist 3---lindex command: Gets the data for the given position, where the data at coordinates 3 is "2", so the result is 2.
4. The list data modification command in Redis: LSet
127.0.0.1:6379>lset mylist 2 zlh---lset command: Set the value of subscript 2 to ZLH and return an error if the subscript value goes out of range or an empty list is LSet
127.0.0.1:6379>lrange mylist 0-1---Result: 1> "1" 2> "2" 3> "ZLH"
5. List in Redis, two List A, B, add the tail element of a list to the head element of list B, command: Rpoplpush
#这里我有连个列表A数据为 {4,5,6}, b list data = {*.}
127.0.0.1:6379>rpoplpush A B
127.0.0.1:6379>lrange A---result for:1> "1 ' 2>" 2 "
127.0.0.1:6379>lrange B---result is:1> "3 ' 2>" 4 "3>" 5 "4>" 6 "
6. Several advanced commands with blocking in Redis: Blpop,brpop,brpoplpush
127.0.0.1:6379>blpop a---Means: a list has values, remove a data from the left, if there is no value, then wait for a to insert the data, wait time is 30 seconds, if the time is set to 0 means that the blocking time unlimited extension
127.0.0.1:6379>blpop B30---Means: A list has values, remove a data from the left, if there is no value, then wait for a to insert the data, wait time is 30 seconds, if the time is set to 0 means that the blocking time unlimited extension
127.0.0.1:6379>brpoplpush a b---means: Add the tail element of a list to the head element of list B, if there is a value in the A list is inserted, if there is no value, wait for a to insert data, wait time is 30 seconds, If the time is set to 0, the blocking time is extended indefinitely
Redis implements Message Queuing with its own priority feature
1, the first Redis list is a linked list structure, with the message queue in the first-out feature.
2, from the above several advanced commands can be seen, the list has a few self-blocking function, the time is set to 0, can be regarded as a never-rest listening process.
Realize:
1, said the above two points I think you should have an idea.
2, sorry a little late, and tomorrow to work, but also to accompany the daughter-in-law to eat a spicy soup, come back to bed, here is not difficult to understand and realize, such as the need to communicate and exchange learning, into the upper left corner group, to live, 88, good night.
REDIS Data Structure detailed list (ii)