Redis data type is list Command sorting and example, redislist

Source: Internet
Author: User

Redis data type is list Command sorting and example, redislist

It is often used to create queues. Of course, lpush + rpop can also be used as stacks.

# Describe rpush rpushx lpush lpushx (the difference between RPUSH and RPUSHX is actually the difference between the insertion direction) RPUSH key value [value...]

Insert all specified values to the end of the list of keys. If the key does not exist, an empty list is created and then pushed. If the key is not saved in a list, an error is returned.

You can use one command to put multiple elements into the queue. You only need to specify multiple parameters after the command. The element is inserted from left to right one by one from the end of the list. For example, the command RPUSH mylist a B c returns a list. The first element is a, the second element is B, and the third element is c.

# Case 1 key is not list 127.0.0.1: 6379> set list2 te1_127.0.0.1: 6379> RPUSH list2 1 2 3 (error) WRONGTYPE Operation against a key holding the wrong kind of value # case 2 normal situation 127.0.0.1: 6379> RPUSH list1 1 1 2 3 (integer) 3127.0.0.1: 6379> LRANGE list1 0-11) "1" 2) "2" 3) "3"
RPUSHX key value

Insert the value to the end of the table of the list key if and only if the key exists and is a list. Unlike the RPUSH command, the RPUSHX command does not do anything when the key does not exist.

# Case 1 normal case the list1 example 127.0.0.1: 6379> RPUSHX list1 4 (integer) 4127.0.0.1: 6379> LRANGE list1 0-11) "1" 2) "2" 3) "3" 4) "4" # case 2 list does not exist, return 0127.0.0.1: 6379> RPUSHX list3 4 (integer) 0 without any operation
# Pop command. Introduce rpop lpop together (introduce RPOP in detail, because it is actually the difference in the pop-up direction) RPOP key

Remove and return the last element of the list stored in the key.

# 1list does not exist 127.0.0.1: 6379> RPOP list3 (nil) # case 2 Non-list127.0.0.1: 6379> get list2 "test" 127.0.0.1: 6379> RPOP list2 (error) WRONGTYPE Operation against a key holding the wrong kind of value # case 3 normal situation 127.0.0.1: 6379> LRANGE list1 0-11) "1" 2) "2" 3) "3" 4) "4" 127.0.0.1: 6379> RPOP list1 "4" 127.0.0.1: 6379> LRANGE list1 0-11) "1" 2) "2" 3) "3" # case 4 when list is empty 127.0.0.1: 6379> RPOP list (nil)
# If you want to modify the value of a location lsetLSET key index value

Set the value of the list element at the index position to value.

If the index is out of the range, an error is returned.

127.0.0.1:6379> LRANGE list1 0 -11) "1"2) "2"3) "3"127.0.0.1:6379> LSET list1 0 0OK127.0.0.1:6379> LRANGE list1 0 -11) "0"2) "2"3) "3"
# Use ltrimLTRIM key start stop if you want to cut off the Reserved list Space

Trim (trim) an existing list, so that the list will only contain the specified element of the specified range. Start and stop are counted from 0. Here 0 is the first element (header) in the list, 1 is the second element, and so on.

LTRIMIs used with LPUSH/RPUSH. This scenario can be used to record logs, similar to a circular queue. For example:

  • LPUSH mylist someelement
  • LTRIM mylist 0 99
127.0.0.1:6379> LRANGE list1 0 -11) "0"2) "2"3) "3"127.0.0.1:6379> LTRIM list1 0 1OK127.0.0.1:6379> LRANGE list1 0 -11) "0"2) "2"
# Use linsertLINSERT key BEFORE | AFTER each value if you want to increase the value BEFORE and AFTER a list value

Insert value into the list of keys before or after the benchmark value limit.

If the key does not exist, this list will be considered as an empty list, and no operation will happen.

If the key exists but not saved as a list, an error is returned.

# The following example inserts 3 after 2 and 1 before 2.

127.0.0.1:6379> LRANGE list1 0 -11) "0"2) "2"127.0.0.1:6379> LINSERT list1 AFTER 2 3(integer) 3127.0.0.1:6379> LINSERT list1 BEFORE 2 1(integer) 4127.0.0.1:6379> LRANGE list1 0 -11) "0"2) "1"3) "2"4) "3"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.