Redis Learning (3)-Data Type list

Source: Internet
Author: User

List type and operation
List is a linked list structure, the main function is to push, pop get all the values of a range, and so on, the Operation key is understood as the name of the list, the Redis list type is actually a two-way list of string type of each child element, we can push, The pop operation adds a delete element from the list's head or tail, so that the list can be both a stack and a queue

1.lpush
Adds a string element to the head of the list in key
Here we insert a world first, and then insert a hello in the world's head, where Lrange is used to fetch mylist content.
Command:
Lpush mylist "World"
Lpush mylist "Hello"
Lrange MyList 0-1
1) Hello
2) World


2.rpush

Rpush mylist2 "Hello"
Rpush Mylist2 "World"

1) "Hello"
2) "World"

at the end of Hello


3.linsert
Add a string element before or after the key corresponds to the position of the list
Rpush mylist3 "Hello"
Rpush mylist3 "World"
Linsert Mylist3 before "World" "there"
Lrange Mylist3 0-1
1) Hello
2) there
3) World
Here we first insert a Hello, then insert a world at the end of Hello, and then insert the there in front of world.


4.lset
sets the element value for the specified subscript in the list (subscript starting from 0)
Rpush mylist4 "one"
Rpush Mylist4 "The Other"
Rpush mylist4 "three"
LSet mylist3 0 "four"
LSet mylist3-2 "Five"
lrange mylist4 0-1
1) "Four"
2) "Five"
3) "three"
Here we insert the One,two,three in turn, set the value of 0 to four, and then set the value of the subscript-2 to five.

5.lrem
deletes the same count and value elements from the key corresponding list
count > 0 o'clock, Delete in order from beginning to end, as follows  
rpush mylist5 "Hello"
rpush mylist5 "Hello"
rpush mylist5 "foo"
rpush mylist5 "Hello"
rpush mylist5 2 "Hello"
lrange mylist5 0-1
1) "foo"
2) "Hello"
count<0, delete from the end of the head, as follows:  
rpush mylist6 "Hello"
rpush mylist6 "Hello"
rpush mylist6 "foo"
rpush mylist6 "Hello"
lrem mylist6-2 "Hello"
lrange Mylist6 0-1
1) "Hello"
2) "foo"
count=0, delete all, specifically as follows:
rpush mylist7 "Hello"
rpush mylist7 "Hello"
rpush mylist7 "foo"
rpush mylist7 "Hello"
lrem mylist7 0 "Hello"
lrange mylist7 0-1
1) "foo"


6.ltrim

Rpush Mylist8 "one"
Rpush mylist8" "The"
Rpush Mylist8 "three"
Rpush Mylist8 " Four "
LTrim mylist8 1-1
Lrange Mylist8 0-1
1)"
2) Three
3) Four


7.lpop

Lrange mylist 0-1
1) "Hello"
2) "World"
Lpop MyList
1) "Hello"
Lrange MyList 0-1
1) "World"


8.rpop
Deletes an element from the tail of the list and returns the element to delete
Lrange Mylist2 0-1
1) "Hello"
2) "World"
Rpop MyList
1) "World"
Lrange Mylist2 0-1
1) "Hello"


9.rpoplpush
removes the element from the end of the first list and adds it to the head of the second list, and finally returns the value of the removed element, the entire operation being atomic. If the first list is empty or does not exist return nil:
lrange mylist5 0-1
1) "Three"
2) "foo"
3) "Hello"
lrange Mylist6 0-1
1) "Hello"
2) "foo"
Rpoplpush mylist5 Mylist6
"Hello"
lrange mylist5 0-1
1) "Three"
2) "foo"
lrange Mylist6 0-1
1) "Hello"
2) "Hello"
3) "foo"


10.lindex
Returns the element of the index position in the list with the name key
Lrange Mylist5 0-1
1) "Three"
2) "Foo"
Lindex MYLIST5 0
"Three"
Lindex MYLIST5 1
"Foo"


11.llen
Returns the length of the key corresponding to the list
Llen MYLIST5
















Redis Learning (3)-Data Type 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.