In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Redis list and collection how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Redis list and collection how to use" it!
List
Lists are another data type in Redis. Let's take a look at some basic operation commands in the list.
LPUSH
Insert one or more values value into the header of the list key. If there are more than one value, each value is inserted in the header from left to right, as follows:
127.0.0.1 3LRANGE 6379 > LPUSH K1 v1 v2 v3 (integer)
Returns the elements in the specified interval in the list key, which is specified by offset start and stop, and the subscript (index) parameters start and stop are based on 0, where 0 represents the first element of the list, 1 represents the second element of the list, and so on. We can also use a negative subscript with-1 for the last element of the list,-2 for the penultimate element of the list, and so on. As follows:
127.0.1 LRANGE 6379 > V1 k10-11) "v3" 2) "v2" 3) "v1" RPUSH
The function of RPUSH is basically the same as that of LPUSH, except that the value values in RPUSH are inserted in the order from right to left, as follows:
127.0.0.1 RPUSH 6379 > LRANGE k2 12 3 4 5 (integer) 5127.0.1 LRANGE k20-11) "1" 2) "2" 3) "3" 4) "4" 5) "5" RPOP
The RPOP command removes and returns the tail element of the list key. As follows:
127.0.0.1 RPOP 6379 > LRANGE k2 "5" 127.0.0.1 LPOP 6379 > LPOP 20-11) "1" 2) "2" 3) "3" 4)
LPOP is similar to RPOP, except that LPOP removes and returns the header element of the list key, as follows:
127.0.0.1 LPOP K2 "1" 127.0.0.1 LINDEX 6379 > LRANGE k20-11) "2" 2) "3" 3) "4" LINDEX
The LINDEX command can return the element in the list key with subscript index, positive subscript 0 for the first element, or negative subscript for the first element, and-1 for the penultimate element, as follows:
127.0.0.1 LINDEX 6379 > LINDEX K2-1 "4" LTRIM
The LTRIM command can prune a list, that is, let the list retain only the elements within the specified range, and all elements that are not within the specified range will be deleted. The subscript is consistent with the previous description, so I won't repeat it here. As follows:
127.0.0.1 LRANGE K10-11) "v3" 2) "v2" 3) "v1" 127.0.1 1OK127.0.0.1:6379 > LRANGE K10 1OK127.0.0.1:6379 > LRANGE K10-11) "v3" 2) "v2" BLPOP
BLPOP is the pop-up primitive for blocking lists. It is a blocking version of the command LPOP, and when there are no elements available to pop up in a given list, the connection is blocked by the BLPOP command. When multiple key parameters are given, check each list in the order of the parameter key, and the header element of the first non-empty list pops up. At the same time, when using this command, you also need to specify the length of blocking, in seconds, within which if there is no element to eject, the blocking ends. The returned result is a combination of key and value, as follows:
127.0.0.1 BLPOP K1 101) "K1" 2) "v2" 127.0.0.1 nil 6379 > BLPOP K1 10 (nil) (10.03s)
Finally, BRPOP, BPOPLPUSH, and BRPOPLPUSH are all blocking versions of the corresponding commands, so I won't repeat them here.
Set
Next, let's look at some common operation commands in the collection:
SADD
The SADD command can add one or more specified member elements to the key of the collection. The specified one or more element member is ignored if it already exists in the collection key. If the collection key does not exist, create a new collection key and add the member element to the collection key. As follows:
127.0.0.1 4SREM 6379 > SADD K1 v1 v2 v3 v4 (integer)
The SREM command removes a specified element from the key collection and ignores it if the specified element is not an element in the key collection. If the key collection does not exist, it is considered an empty collection, and the command returns 0. As follows:
127.0.0.1 SREM v2 (integer) 1127.0.1 SREM v10 (integer) 0SISMEMBER
The SISMEMBER command returns whether the member member is a member of the stored collection key. As follows:
127.0.0.1 1SCARD 6379 > SISMEMBER K1 v3 (integer)
The SCARD command returns the cardinality of the key stored by the collection (the number of collection elements), as follows:
127.0.0.1 3SMEMBERS 6379 > SCARD K1 (integer)
The SMEMBERS command returns all the elements of the key collection, as follows:
127.0.0.1 SMEMBERS 6379 > V4 "2)" v1 "3)" v3 "SRANDMEMBER
As long as we provide the key parameter, SRANDMEMBER will randomly return an element in the key collection. Starting with Redis2.6, the command can also accept an optional count parameter, if the count is an integer and less than the number of elements Count random elements are returned. If count is an integer and is greater than the number of elements in the set, all elements in the set are returned. If count is negative, an array of elements containing the absolute value of count is returned. If the absolute value of count is greater than the number of elements, an element will appear multiple times in the returned result set. As follows:
127.0.0.1) SRANDMEMBER K1 > SRANDMEMBER K1) SRANDMEMBER K1 21) "v4" 2) "v1" 127.0.0.1 SRANDMEMBER K1 51) "v4" 2) "v1" 2) "v1" 3) "v3" 127.0.0.1) SRANDMEMBER K1-11) "v4" 127.0.1 SRANDMEMBER K1-51) "v3" 2) "v1" 3) "v1" 4) "v3" SPOP
The use of the SPOP command is similar to that of SRANDMEMBER, except that each time SPOP selects a random element, the element goes off the stack, while SRANDMEMBER does not go out of the stack, just showing it.
SMOVE
The SMOVE command moves member from the source collection to the destination collection, as follows:
127.0.0.1 SMOVE K1 K2 v1 (integer) 1127.0.1 SMEMBERS K11) "v4" 2) "v3" 127.0.0.1 SMEMBERS SDIFF
SDIFF can be used to return the elements of the difference between a collection and a given set, as follows:
127.0.0.1 SDIFF 6379 > V4 "2)" v3 "
The elements in K1 are v3, v4 and the elements in K2 are v1, and the difference sets are v3 and v4.
SDIFFSTORE
The SDIFFSTORE command is basically the same as the SDIFF command, except that the SDIFFSTORE command saves the results in a collection, as follows:
127.0.0.1 SDIFFSTORE key K2 (integer) 2127.0.1 SMEMBERS key1) "v4" 2) "v3" SINTER
The SINTER command can be used to calculate the intersection of elements between specified key, as follows:
127.0.0.1 SMEMBERS K11) "v4" 2) "v3" 127.0.0.1 SINTER 6379 > SMEMBERS k21) "v1" 2) "v3" 127.0.1 SINTER K21) v3 SINTERSTORE
The SINTERSTORE command is similar to the SINTER command, except that it saves the results to a new collection, as follows:
127.0.0.1 SMEMBERS 6379 > SINTERSTORE K3 K1 K2 (integer) 1127.0.1 SMEMBERS K31) "v3" SUNION
SUNION can be used to calculate the union of two sets, as follows:
127.0.0.1 SUNION K1 K21) "v4" 2) "v1" 3) "v3" SUNIONSTORE
The SUNIONSTORE command is similar to the SUNION command, except that it saves the results to a new collection, as follows:
127.0.0.1Redis 6379 > SUNIONSTORE K4 K1K2 (integer) 3127.0.0.1 SMEMBERS K41) "v4" 2) "v1" 3) "v3" Thank you for your reading. The above is the content of "how to use Redis lists and collections". After the study of this article, I believe you have a deeper understanding of how to use Redis lists and collections, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.