NoSQL----Redis 2.4--list

Source: Internet
Author: User
Tags redis version

List:

head element and tail element: The head element refers to the first element of the left/front end of the list; the tail element is the first element in the right/back end of the list.

For example, the list of lists contains three elements: X, Y, z, where x is the head element, and z is the tail element.

empty list: refers to a list that does not contain any elements, and Redis treats the nonexistent key as an empty list.

1. Lpush(left Push) method:

Lpush key value [value ...]

Inserts one or more values of value into the table header of the list key.

If there are multiple value values, then each value value is inserted sequentially from left to right into the header: for example, to perform Lpush mylist a b C on an empty list (mylist), the result list is C B A,

Equivalent to executing the command

Lpush MyList A

Lpush MyList b

Lpush MyList C

If the 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) # Add repeating element redis> Lpush languages python (integer) 2redis> lrange Languages 0-1 # List allows repeating elements 1) "Python" 2) "Python" # Add multiple elements redis> Lpush mylist a b C (integer) 3redis> lrange mylist 0- "C" 2) "B" 3) "a"

2, Lpushx(left Pushx) method:

Lpushx Key value

Inserts the value values into the table header of the list key when and only if key exists and is a list.

As opposed to the Lpush command, when key does not exist, the LPUSHX command does nothing.

For example: # scenario 1: lpushxredis> Llen Greet # greet is an empty list (integer) 0redis> lpushx greet "Hello" # attempt lpushx, failed because the list is empty ( Integer) 0# Case 2: Perform lpushxredis> lpush greet "Hello" on a non-empty list first create a list with one element (integer) Lpush 1redis> lpushx "good Morning "# This time 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 value values into the footer of the list key.

If there are multiple value values, the individual value values are inserted sequentially from left to right in the footer:

For example, for an empty list (mylist) execution Rpush mylist a b C, the result list is a B C,

Equivalent to executing the command

Rpush MyList A,

Rpush MyList B,

Rpush mylist c.

If the 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 version 2.4Rpushcommand, they accept only a singlevaluevalues.
# Add a single element redis> Rpush languages C (integer) # Add repeating element redis> Rpush languages C (integer) 2redis> lrange languages 0- 1 # 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

Inserts the value values into the footer of the list key when and only if key exists and is a list.

As opposed to the Rpush command, when key does not exist, the RPUSHX command does nothing.


For example: # case 1:key does not exist redis> Llen greet (integer) 0redis> rpushx greet "Hello" # Rpushx,push failed on nonexistent key. (integer) 0# condition 2:key exists and is a non-empty list redis> rpush greet "HI" # First insert an element with Rpush (integer) 1redis> rpushx greet "Hello" # Gree T is now a list type and the rpushx operation succeeded. (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 c++101 (integer) 2red Is> Lpop Course # Remove head 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 "one" (integer) 2redis> rpush mylist "three" (integer) 3red is> Rpop MyList # Returns the element that was popped "three" redis> lrange mylist 0-1 # List of remaining elements 1) "one" 2) "one"

7, Llen Method:

Llen Key

Returns the length of the list key.

If key does not exist, then key is interpreted as an empty list, returning 0.

If key is not a list type, an error is returned.

# scenario 1: Empty list redis> llen Job (integer) 0# condition 2: Non-empty list redis> Lpush job "Cook food" (integer) 1redis> lpush job "has lunch" (integer) 2redis> llen Job (integer) 2

8, Lrange Method:

Lrange Key Start end

Returns the element in the specified interval in the list key, specified by the offset start and end.

The subscript (index) parameter start and end all end with 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 to 1 for the last element of the list, 2 to represent the penultimate element of the list, and so on.

Subscript out of Range

An out-of-range subscript value does not cause an error.

IfStartSubscript is the largest subscript of the listEnd( llen list Minus1) is larger, 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 # Inserts a value into the list fp-language (integer) 1redis> lrange fp-language 0) "Lisp" Redis> Rpus H fp-language Scheme (integer) 2redis> lrange fp-language 0) "Lisp" 2) "scheme"

9, Linsert Method:

NoSQL----Redis 2.4--list

Related Article

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.