Redis Data type: Lists

Source: Internet
Author: User

The Redis list type is actually a doubly linked list in which each child element is a string type.

We can add delete elements from the head or tail of the list by Push,pop operation, so that list can be used as

Stack, and can be used as a queue.

Lpush method: Adding a string element from the head

Lrange method: Fetching data from the list, fetching from the head

127.0.01:6379> lpush list1 "Hello" "word"
(integer) 2
127.0.01:6379> Lrange List1 0-1
1) "word"
2) "Hello"

Rpush method: Press in from the rear

127.0.01:6379> rpush list2 "Hello" "word"
(integer) 2
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "word"

Linsert method: Inserts the specified element before or after the specified key's list

127.0.01:6379> rpush list2 "Hello" "word"
(integer) 2
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "word"
127.0.01:6379> Linsert list2 before "word" "in"
(integer) 3
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "in"
3) "word"

LSet method: Sets the element value of the list to specify subscript

127.0.01:6379> Lpush List3 One and three
(integer) 3
127.0.01:6379> Lrange List3 0-1
1) "Three"
2) "Both"
3) "One"
127.0.01:6379> LSet List3 0 onechange
Ok
127.0.01:6379> Lrange List3 0-1
1) "Onechange"
2) "Both"
3) "One"

Lrem method: Removes N and value elements from the key corresponding list. Returns the number of deletions

(n<0 removed from tail, n=0 all removed)

127.0.01:6379> Rpush List2 "One" "One" "one"
(integer) 6
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "in"
3) "word"
4) "One"
5) "One"
6) "One"
127.0.01:6379> Lrem list2 2 "one"
(integer) 2
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "in"
3) "word"
4) "One"
127.0.01:6379> Lrem list2 1 "one"
(integer) 1
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "in"
3) "word"
127.0.01:6379> Lrem list2 1 "one"
(integer) 0

LTrim method: Retains key corresponding to the list's specified range of data

127.0.01:6379> Rpush list5 "One" "" "" "" "" Three "" Four "
(integer) 4
127.0.01:6379> Lrange list5 0-1
1) "One"
2) "Both"
3) "Three"
4) "Four"
127.0.01:6379> LTRIM LIST5 1 2
Ok
127.0.01:6379> Lrange list5 0-1
1) "Both"
2) "Three"

Lpop method: Removes the element from the list header and returns the Delete element

Rpop method: Removes the element from the tail of the list and returns the element deleted

127.0.01:6379> Rpush List6 "One" "One" "" "" Three "
(integer) 3
127.0.01:6379> Lrange List6 0-1
1) "One"
2) "Both"
3) "Three"
127.0.01:6379> Rpop List6
"Three"
127.0.01:6379> Lpop List6
"One"
127.0.01:6379> Lrange List6 0-1
1) "Both"

Rpoplpush method: Remove the trailing element from a list and add the element to the head of another list

127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "in"
3) "word"
127.0.01:6379> Lrange List3 0-1
1) "Onechange"
2) "Both"
3) "One"
127.0.01:6379> Rpoplpush List2 List3
"Word"
127.0.01:6379> Lrange List2 0-1
1) "Hello"
2) "in"
127.0.01:6379> Lrange List3 0-1
1) "word"
2) "Onechange"
3) "Both"
4) "One"

Lindex method: Returns the element of the index position in the list named key

127.0.01:6379> Lrange List3 0-1
1) "word"
2) "Onechange"
3) "Both"
4) "One"
127.0.01:6379> LINDEX List3 2
"Both"

Llen method: Returns the length of the list corresponding to key

127.0.01:6379> Lrange List3 0-1
1) "word"
2) "Onechange"
3) "Both"
4) "One"
127.0.01:6379> Llen List3
(integer) 4

Redis Data type: Lists

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.