In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
I. Overview:
1. List: duplicate elements are allowed, and list can be used as a stack (first in, first out) or queue (first in, first out).
Header element and trailing element: the header element refers to the first element at the left / front end of the list; the trailing element refers to the first element at the right end / back end of the list.
For example, the list list contains three elements: X, y, and z, where x is the header element and z is the tail element.
Empty list: refers to a list that does not contain any elements, and Redis treats a key that does not exist as an empty list.
2. Tips on linked list structure:
Value,Redis for linked list structure gives some practical tips in its official documentation, such as the RPOPLPUSH command, which is explained in detail below.
Redis linked lists are often used for message queuing services to complete message exchange between multiple programs. Suppose an application is performing a LPUSH operation to add new elements to the linked list. We usually call such a program "Producer", while another application is performing a RPOP operation to extract elements from the linked list. We call such a program "Consumer". If at this time, the consumer program crashes immediately after fetching the message element, because the message has been retrieved and has not been processed normally, then we can assume that the message has been lost, which may lead to the loss of business data. or business state inconsistency and other phenomena occur. However, by using the RPOPLPUSH command, the consumer program takes the message from the main message queue and then inserts it into the backup queue until the consumer program completes the normal processing logic before removing the message from the backup queue. At the same time, we can also provide a daemon that, when a message in the backup queue is found to have expired, can be put back into the main message queue so that other consumer programs can continue to process.
2. Application scenarios:
Get the latest 10 login user information:
1. To use database, you need to execute the following sql statement:
Select * from loginuser order by logintime desc limit 10
But when there is a lot of data, all the data will be affected, and the load on the database is relatively high.
If necessary, you also need to set an index on the key field logintime, which is also more resource-intensive.
2. If the above functions are achieved through redis's list linked list, it will be very simple. You can keep only the latest 10 login users in the list linked list, delete an old data every time a new data comes in, and get the resources you need directly from the linked list each time, greatly saving resources in all aspects.
3. Redis's list can also cache some system configuration information that is not often modified.
Such as caching address information (country, province, city, street), caching user information, and so on.
Third, common operations:
1. Lpush (left push) method:
LPUSH key value [value...]
Insert one or more values value into the header of the list key.
If there are multiple values, each value is inserted in the header from left to right: for example, if LPUSH mylist a b c is performed on an empty list (mylist), the resulting list is c b a
Equivalent to executing a command.
LPUSH mylist a
LPUSH mylist b
LPUSH mylist c
If key does not exist, an empty list is created and the LPUSH operation is performed.
An error is returned when key exists but is not a list type.
For example: # add a single element redis > LPUSH languages python (integer) repeat element redis > LPUSH languages python (integer) 2redis > LRANGE languages 0-repeat list allows repeating elements 1) "python" 2) "python" # add multiple elements redis > LPUSH mylist a b c (integer) 3redis > LRANGE mylist 0-11) "c" 2) "b" 3) "a"
2. Lpushx (left pushx) method:
LPUSHX key value
Insert the value value into the header of the list key if and only if key exists and is a list.
In contrast to the LPUSH command, the LPUSHX command does nothing when key does not exist.
For example: # case 1: execute LPUSHXredis > LLEN greet # greet on an empty list (integer) 0redis > LPUSHX greet "hello" # attempt LPUSHX failed Because the list is empty (integer) hello case 2: execute LPUSHXredis > LPUSH greet "hello" # first create a list with one element with LPUSH (integer) 1redis > LPUSHX greet "good morning" # this LPUSHX execution succeeded (integer) 2redis > LRANGE greet 0-11) "good morning" 2) "hello"
3. Rpush (right push) method:
RPUSH key value [value...]
Inserts one or more values value into the footer of the list key.
If there are multiple value values, each value is inserted at the end of the table from left to right:
For example, if you execute RPUSH mylist a b c on an empty list (mylist), the result list is a b c.
Equivalent to executing a command.
RPUSH mylist a 、
RPUSH mylist b 、
RPUSH mylist c .
If key does not exist, an empty list is created and the RPUSH operation is performed.
An error is returned when key exists but is not a list type.
Note: prior to Redis 2.4, RPUSH commands only accepted a single value.
# add a single element redis > RPUSH languages c (integer) repeat element redis > RPUSH languages c (integer) 2redis > LRANGE languages 0-repeat list allows repeating elements 1) "c" 2) "c" # add multiple elements redis > RPUSH mylist a b c (integer) 3redis > LRANGE mylist 0-11) "a" 2) "b" 3) "c"
4. Rpushx (right pushx) method:
RPUSHX key value
Insert the value value into the footer of the list key if and only if key exists and is a list.
In contrast to the RPUSH command, the RPUSHX command does nothing when key does not exist.
For example: # case 1:key does not exist redis > LLEN greet (integer) 0redis > RPUSHX greet "hello" # failed to RPUSHX,PUSH a key that does not exist. (integer) 2:key exists and is a non-empty list redis > RPUSH greet "hi" # first insert an element with RPUSH (integer) 1redis > RPUSHX greet "hello" # greet is now a list type, and the RPUSHX operation was successful. (integer) 2redis > LRANGE greet 0-11) "hi" 2) "hello"
5. Lpop (left pop) method:
LPOP key
Removes and returns the header element of the list key.
Redis > LLEN course (integer) 0redis > RPUSH course algorithm001 (integer) 1redis > RPUSH course cymbals 101 (integer) 2redis > LPOP course # remove header element "algorithm001"
6. Rpop (right pop) method:
RPOP key
Removes and returns the tail element of the list key.
Redis > RPUSH mylist "one" (integer) 1redis > RPUSH mylist "two" (integer) 2redis > RPUSH mylist "three" (integer) 3redis > RPOP mylist # returns the popup element "three" redis > LRANGE mylist 0-1 # list remaining elements 1) "one" 2) "two"
7. Llen method:
LLEN key
Returns the length of the list key.
If key does not exist, key is interpreted as an empty list and returns 0. 0.
If key is not a list type, an error is returned.
# case 1: empty list redis > LLEN job (integer) situation 2: non-empty list redis > LPUSH job "cook food" (integer) 1redis > LPUSH job "have lunch" (integer) 2redis > LLEN job (integer) 2
8. Lrange method:
LRANGE key start end
Returns the elements in the specified interval in the list key, specified by offset start and end.
The index parameters start and end are both based on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.
You can also use a negative subscript, with-1 for the last element of the list,-2 for the penultimate element of the list, and so on.
Out-of-range subscript
Subscript values that are out of range do not cause errors.
If the start subscript is larger than the largest subscript of the list, end (LLEN list minus 1), or start > stop,LRANGE returns an empty list.
If the stop subscript is larger than the end subscript, Redis sets the value of stop to end.
Redis > RPUSH fp-language lisp # insert a value into the list fp-language (integer) 1redis > LRANGE fp-language 0 01) "lisp" redis > RPUSH fp-language scheme (integer) 2redis > LRANGE fp-language 0 11) "lisp" 2) "scheme"
9. Linsert method:
LINSERT key BEFORE | AFTER pivot value
Inserts the value value into the list key, before or after the value pivot.
When pivot does not exist in the list key, no action is performed.
When key does not exist, key is treated as an empty list and no action is performed.
If key is not a list type, an error is returned.
Redis > RPUSH mylist "Hello" (integer) 1redis > RPUSH mylist "World" (integer) 2redis > LINSERT mylist BEFORE "World"There" (integer) 3redis > LRANGE mylist 0-11) "Hello" 2) "There" 3) "World" redis > LINSERT mylist BEFORE "go"let's" # insert into a non-empty list Find a nonexistent pivot (integer)-1 # failed redis > EXISTS fake_list # failed to execute the LINSERT command (integer) 0redis > LINSERT fake_list BEFORE "nono"gogogog" (integer) 0 # on an empty list
10. Lset method:
LSET key index value
Replace the value of the element whose subscript of the list key is index with value.
Please refer to the LINDEX operation for more information.
An error is returned when the index parameter is out of range or when LSET is performed on an empty list (key does not exist).
# case 1: LSETredis > EXISTS list (integer) 0redis > LSET list 0 item (error) ERR no such key# for empty list 2: LSETredis > LPUSH job "cook food" (integer) 1redis > LRANGE job 0.01) "cook food" redis > LSET job 0 "play game" OKredis > LRANGE job 0.01) "play game" # case 3:index is out of range redis > LLEN list # list length is 1 (integer) 1redis > LSET list 3 'out of range' (error ERR index out of range)
11. Lrem method:
LREM key count value
Removes the element in the list that is equal to the parameter count based on the value of the parameter value.
The values of count can be as follows:
Count > 0: search from the header to the footer of the table to remove the elements equivalent to value, and the number is count.
Count
< 0: 从表尾开始向表头搜索,移除与value相等的元素,数量为count的绝对值。 count = 0: 移除表中所有与value相等的值。 # 先创建一个表,内容排列是# morning hello morning helllo morningredis>LPUSH greet "morning" (integer) 1redis > LPUSH greet "hello" (integer) 2redis > LPUSH greet "morning" (integer) 3redis > LPUSH greet "hello" (integer) 4redis > LPUSH greet "morning" (integer) 5redis > LRANGE greet 0 4 # View all elements 1) "morning" 2) "hello" 3) "morning" 4) "hello" 5) "morning" redis > LREM greet 2 morning # remove from header to footer The first two morning (integer) 2 # elements are removed redis > LLEN greet # three elements (integer) 3redis > LRANGE greet 021) "hello" 2) "hello" 3) "morning" redis > LREM greet-1 morning # remove from footer to header The first morning (integer) 1redis > LLEN greet (integer) 2redis > LRANGE greet 0 11) "hello" 2) "hello" redis > LREM greet 0 hello # remove all hello (integer) 2 # two hello from the table redis > LLEN greet (integer) 0
12. Ltrim method:
LTRIM key start stop
Trim a list, that is, let the list retain only the elements within the specified range, and all elements that are not within the specified range will be deleted.
For example, executing the command LTRIM list 0 2 means that only the first three elements of the list list are retained, and all the remaining elements are deleted.
The index parameters start and stop are both based on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.
You can also use a negative subscript, with-1 for the last element of the list,-2 for the penultimate element of the list, and so on.
An error is returned when key is not a list type.
# case 1: generally mark redis > LRANGE alpha 0-1 # to create a list of 5 elements 1) "h" 2) "e" 3) "l" 4) "l" 5) "o" redis > LTRIM alpha 1-1 # delete the element OKredis > LRANGE alpha 0-1 # "h" is deleted 1) "e" 2) "l" 3) "l" 4) "o" # case The 2:stop subscript is larger than the maximum subscript of the element redis > LTRIM alpha 1 10086OKredis > LRANGE alpha 0-11) "l" 2) "l" 3) "o" # case both 3:start and stop subscript are larger than the maximum subscript And start
< sotpredis>LTRIM alpha 10086 200000OKredis > LRANGE alpha 0-1 # the whole list is cleared, which is equivalent to the case of DEL alpha (empty list or set) # 4:start > stopredis > LRANGE alpha 0-1 # in a new list 1) "h" 2) "u" 3) "a" 4) "n" 5) "g" 6) "z" redis > LTRIM alpha 10086 4OKredis > LRANGE alpha 0-1 # list is also cleared (empty list or set)
13. Rpoplpush method:
RPOPLPUSH source destination
The command RPOPLPUSH performs the following two actions in one atomic time:
Pops up the last element (the tail element) in the list source and returns it to the client.
Insert the source pop-up element into the list destination as the header element of the destination list.
For example, you have two lists-source and destination,source-list has elements a, b, c, source destination list has elements x, y, z, after performing RPOPLPUSH source destination, the list of source contains elements a, the list of bdirection destinations contains elements c, x, y, z, and element c is returned.
If source does not exist, the value nil is returned and no other action is performed.
If source and destination are the same, the footer element in the list is moved to the header and returned, which can be thought of as a rotation operation of the list.
# related data redis > RPUSH alpha a (integer) 1redis > RPUSH alpha b (integer) 2redis > RPUSH alpha c (integer) 3redis > RPUSH alpha d (integer) situation 1:source and destination different redis > LRANGE alpha 0-1 # View all elements 1) "a" 2) "b" 3) "c" 4) "d" redis > RPOPLPUSH alpha reciver # execute RPOPLPUSH once to see "d" redis > LRANGE alpha 0-11) "a" 2) "b" 3) "c" redis > LRANGE reciver 0-11) "d" redis > RPOPLPUSH alpha reciver # execute again Make sure rpop and lpush are positioned correctly "c" redis > LRANGE alpha 0-11) "a" 2) "b" redis > LRANGE reciver 0-11) "c" 2) "d" # case 2:source is the same as destination redis > RPOPLPUSH alpha alpha "c" redis > LRANGE alpha 0-1 # the original tail element "c" is placed in the header 1) "c" 2) "a" 3) "b"
14. Lindex method:
LINDEX key index
Returns the element in the list key with the subscript index.
The index parameters start and stop are both based on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.
You can also use a negative subscript, with-1 for the last element of the list,-2 for the penultimate element of the list, and so on.
If key is not a list type, an error is returned.
Redis > LPUSH mylist "World" (integer) 1redis > LPUSH mylist "Hello" (integer) 2redis > LINDEX mylist 0 "Hello" redis > LINDEX mylist-1 "World" redis > LINDEX mylist 3 # index is not within the range of mylist (nil)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
* not indexed * * > var startTime = new Date (); > db.tem
© 2024 shulou.com SLNews company. All rights reserved.