In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what the lists data types are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
What are the lists data types
The list type of Redis is actually a two-way linked list with each child element of type string. The maximum length of the linked list is (2 to the 32). We can add and remove elements from the head or tail of the linked list through the push,pop operation. This allows list to be used as both a stack and a queue.
What is interesting is that there is also a blocking version of list's pop operation. When we [lr] pop a list object, if list is empty or does not exist, we will immediately return nil. But the blocking version of b[ LR] pop can block, of course, you can add a timeout, and a nil will be returned after the timeout. Why block the version of pop, mainly to avoid polling. Let's take a simple example if we use list to implement a work queue. The thread that executes the task can call the blocking version of pop to get the task so that polling can be avoided to check for the existence of the task. When the task comes, the worker thread can return immediately, or it can avoid the delay caused by polling.
How to operate lists data
1 、 lpush
Add a string element to the header of the corresponding list of key:
Redis 127.0.0.1 world 6379 > lpush mylist "world"
(integer) 1
Redis 127.0.0.1 hello 6379 > lpush mylist "hello"
(integer) 2
Redis 127.0.0.1 6379 > lrange mylist 0-1
1) "hello"
2) "world"
Redis 127.0.0.1 purl 6379 >
Here we insert a world and then a hello in the head of the world. Where lrange is the content used to fetch mylist.
2 、 rpush
Add a string element at the end of the corresponding list of key:
Redis 127.0.0.1 hello 6379 > rpush mylist2 "hello"
(integer) 1
Redis 127.0.0.1 world 6379 > rpush mylist2 "world"
(integer) 2
Redis 127.0.0.1 6379 > lrange mylist2 0-1
1) "hello"
2) "world"
Redis 127.0.0.1 purl 6379 >
Here we insert a hello and then a world at the end of the hello.
3 、 linsert
Add a string element before or after the specific position of the key corresponding to the list:
Redis 127.0.0.1 hello 6379 > rpush mylist3 "hello"
(integer) 1
Redis 127.0.0.1 world 6379 > rpush mylist3 "world"
(integer) 2
Redis 127.0.0.1 world 6379 > linsert mylist3 before "world"there"
(integer) 3
Redis 127.0.0.1 6379 > lrange mylist3 0-1
1) "hello"
2) "there"
3) "world"
Redis 127.0.0.1 purl 6379 >
Here we insert a hello, then a world at the end of the hello, and then a there in front of the world.
4 、 lset
Sets the element value of the specified subscript in the list (the subscript starts at 0):
Redis 127.0.0.1 one 6379 > rpush mylist4 "one"
(integer) 1
Redis 127.0.0.1 two 6379 > rpush mylist4 "two"
(integer) 2
Redis 127.0.0.1 three 6379 > rpush mylist4 "three"
(integer) 3
Redis 127.0.0.1 four 6379 > lset mylist4 0 "four"
OK
Redis 127.0.0.1 five 6379 > lset mylist4-2 "five"
OK
Redis 127.0.0.1 6379 > lrange mylist4 0-1
1) "four"
2) "five"
3) "three"
Redis 127.0.0.1 purl 6379 >
Here we insert one,two,three in turn, then set the value of 0 to four and the value of subscript-2 to five.
5 、 lrem
Delete count elements that are the same as value from the corresponding list of key.
When count > 0, delete it in the order from beginning to end, as follows:
Redis 127.0.0.1 hello 6379 > rpush mylist5 "hello"
(integer) 1
Redis 127.0.0.1 hello 6379 > rpush mylist5 "hello"
(integer) 2
Redis 127.0.0.1 foo 6379 > rpush mylist5 "foo"
(integer) 3
Redis 127.0.0.1 hello 6379 > rpush mylist5 "hello"
(integer) 4
Redis 127.0.0.1 hello 6379 > lrem mylist5 2 "hello"
(integer) 2
Redis 127.0.0.1 6379 > lrange mylist5 0-1
1) "foo"
2) "hello"
Redis 127.0.0.1 purl 6379 >
Countrpush mylist6 "hello"
(integer) 1
Redis 127.0.0.1 hello 6379 > rpush mylist6 "hello"
(integer) 2
Redis 127.0.0.1 foo 6379 > rpush mylist6 "foo"
(integer) 3
Redis 127.0.0.1 hello 6379 > rpush mylist6 "hello"
(integer) 4
Redis 127.0.0.1 hello 6379 > lrem mylist6-2 "hello"
(integer) 2
Redis 127.0.0.1 6379 > lrange mylist6 0-1
1) "hello"
2) "foo"
Redis 127.0.0.1 purl 6379 >
When count=0, delete all of them, as follows:
Redis 127.0.0.1 hello 6379 > rpush mylist7 "hello"
(integer) 1
Redis 127.0.0.1 hello 6379 > rpush mylist7 "hello"
(integer) 2
Redis 127.0.0.1 foo 6379 > rpush mylist7 "foo"
(integer) 3
Redis 127.0.0.1 hello 6379 > rpush mylist7 "hello"
(integer) 4
Redis 127.0.0.1 hello 6379 > lrem mylist7 0 "hello"
(integer) 3
Redis 127.0.0.1 6379 > lrange mylist7 0-1
1) "foo"
Redis 127.0.0.1 purl 6379 >
6 、 ltrim
Preserves data within the value range of the specified key:
Redis 127.0.0.1 one 6379 > rpush mylist8 "one"
(integer) 1
Redis 127.0.0.1 two 6379 > rpush mylist8 "two"
(integer) 2
Redis 127.0.0.1 three 6379 > rpush mylist8 "three"
(integer) 3
Redis 127.0.0.1 four 6379 > rpush mylist8 "four"
(integer) 4
Redis 127.0.0.1 6379 > ltrim mylist8 1-1
OK
Redis 127.0.0.1 6379 > lrange mylist8 0-1
1) "two"
2) "three"
3) "four"
Redis 127.0.0.1 purl 6379 >
What about the lists data type to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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
© 2024 shulou.com SLNews company. All rights reserved.