In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
I. Overview:
1. In Redis, we can regard the Set type as a collection of characters without sorting, and like the List type, we can also add, delete or determine the existence of an element on the data value of this type. It should be noted that the time complexity of these operations is O (1), that is, the second operation is completed in a constant time. The maximum number of elements that a Set can contain is 4294967295.
2. Unlike the List type, duplicate elements are not allowed in the Set collection, in other words, if you add the same element multiple times, only one copy of the element will be retained in the Set.
3. Compared with the List type, the Set type also has a very important feature in function, that is, it completes the aggregate computing operations between multiple Sets on the server side, such as intersection, union and difference. Because these operations are done on the server side, it is extremely efficient and saves a lot of network IO overhead. For example, what qq friends recommend is the intersection of Set.
Example:
TomFri Collection:
LinkenFri Collection:
Intersection:
Union:
Subtraction:
2. Scope of application:
1. You can use Redis's Set data type to track some unique data, such as unique IP address information for accessing a blog. For this scenario, we only need to store the visitor's IP in Redis each time we visit the blog, and the Set data type automatically guarantees the uniqueness of the IP address.
2. Making full use of the convenient and efficient operation of server-side aggregation of Set type, it can be used to maintain the relationship between data objects. For example, the ID of all customers who buy an electronic device is stored in a designated Set, while the ID of customers who buy another electronic product is stored in another Set. If we want to get which customers have purchased the two goods at the same time, the intersections command of Set can give full play to its advantages of convenience and efficiency.
3. Methods:
1. Sadd method:
SADD key member [member...]
If one or more member elements are added to the collection key, member elements that already exist in the collection will be ignored.
If key does not exist, create a collection that contains only the member element as a member.
An error is returned when key is not a collection type.
# add single element redis > SADD bbs "discuz.net" (integer) "add repeating element redis > SADD bbs" discuz.net "(integer)" add multiple elements redis > SADD bbs "tianya.cn"groups.google.com" (integer) 2redis > SMEMBERS bbs1) "discuz.net" 2) "groups.google.com" 3) "tianya.cn"
2. Srem method:
SREM key member [member...]
Remove one or more member elements from the collection key, and member elements that do not exist are ignored.
When key is not a collection type, an error is returned.
# Test data redis > SMEMBERS languages1) "c" 2) "lisp" 3) "python" 4) "ruby" # remove single element redis > SREM languages ruby (integer) remove no element redis > SREM languages non-exists-language (integer) remove multiple elements redis > SREM languages lisp python c (integer) 3redis > SMEMBERS languages (empty list or set)
3. Smembers method:
SMEMBERS key
Returns all members of the collection key.
# case 1: empty collection redis > EXISTS not_exists_key # key that does not exist is regarded as empty collection (integer) 0redis > SMEMBERS not_exists_key (empty list or set) # case 2: non-empty collection redis > SADD programming_language python (integer) 1redis > SADD programming_language ruby (integer) 1redis > SADD programming_language c (integer) 1redis > SMEMBERS programming_language1) "c" 2) "ruby" 3) "python"
4. Spop method:
SPOP key
Removes and returns a random element in the collection (set is an unordered set).
Redis > SMEMBERS my_sites1) "huangz.51cto.com" 2) "sideeffect.me" 3) "douban.com" redis > SPOP my_sites "huangz.51cto.com" redis > SMEMBERS my_sites1) "sideeffect.me" 2) "douban.com"
5. Sdiff method:
SDIFF key [key...]
Returns all members of a collection, which is the difference of all given sets, based on the first written set.
A key that does not exist is considered an empty set.
Redis > SMEMBERS myset11) "one" 2) "two" redis > SMEMBERS myset21) "two" 2) "three" redis > SDIFF myset1 myset21) "one" redis > SDIFF myset2 myset11) "three"
6. Sdiffstore method:
SDIFFSTORE destination key [key...]
This command is equivalent to SDIFF, but it saves the results to the destination collection instead of simply returning the result set.
If the destination collection already exists, it is overwritten.
Destination can be key itself.
Redis > SMEMBERS myset11) "one" 2) "two" redis > SMEMBERS myset21) "two" 2) "three" redis > SDIFFSTORE myset3 myset1 myset21) "one" redis > SMEMBERS myset31) "one"
7. Sinter method:
SINTER key [key...]
Returns all members of a collection that is the intersection of all given collections.
A key that does not exist is considered an empty set.
When there is an empty set in a given set, the result is also an empty set (according to the law of set operation).
Redis > SMEMBERS myset11) "one" 2) "two" redis > SMEMBERS myset21) "two" 2) "three" redis > SINTER myset1 myset21) "two"
8. Sinterstore method:
SINTERSTORE destination key [key...]
This command is equivalent to SINTER, but it saves the results to the destination collection instead of simply returning the result set.
If the destination collection already exists, it is overwritten.
Destination can be key itself.
Redis > SMEMBERS myset11) "one" 2) "two" redis > SMEMBERS myset21) "two" 2) "three" redis > SINTERSTORE myset3 myset1 myset2redis > SMEMBERS myset31) "two"
9. Sunion method:
SUNION key [key...]
Returns all members of a collection that is the union of all given collections.
A key that does not exist is considered an empty set.
Redis > SMEMBERS myset11) "one" 2) "two" redis > SMEMBERS myset21) "two" 2) "three" redis > SUNION myset1 myset21) "two" 2) "three" 3) "one"
10. Sunionstore method:
SUNIONSTORE destination key [key...]
This command is equivalent to SUNION, but it saves the results to the destination collection instead of simply returning the result set.
If destination already exists, overwrite it.
Destination can be key itself.
Redis > SMEMBERS myset11) "one" 2) "two" redis > SMEMBERS myset21) "two" 2) "three" redis > SUNIONSTORE myset3 myset1 myset2redis > SMEMBERS myset31) "two" 2) "three" 3) "one"
11. Smove method:
SMOVE source destination member
Move the member element from the source collection to the destination collection.
SMOVE is an atomic operation.
If the source collection does not exist or does not contain the specified member element, the SMOVE command does nothing but returns 0. Otherwise, the member element is removed from the source collection and added to the destination collection.
When the destination collection already contains the member element, the SMOVE command simply removes the member element from the source collection.
An error is returned when source or destination is not a collection type.
Redis > SMEMBERS songs1) "Billie Jean" 2) "Believe Me" redis > SMEMBERS my_songs (empty list or set) redis > SMOVE songs my_songs "Believe Me" (integer) 1redis > SMEMBERS songs1) "Billie Jean" redis > SMEMBERS my_songs1) "Believe Me"
12. Scard method:
SCARD key
Returns the number of elements in the collection key.
Redis > SMEMBERS tool1) "pc" 2) "printer" 3) "phone" redis > SCARD tool (integer) 3redis > SMEMBERS fake_set (empty list or set) redis > SCARD fake_set (integer) 0
13. Sismember method:
SISMEMBER key member
Determines whether the member element is a member of the collection key.
Redis > SMEMBERS joe's_movies1) "hi, lady" 2) "Fast Five" 3) "2012" redis > SISMEMBER joe's_movies "bet man" (integer) 0redis > SISMEMBER joe's_movies "Fast Five" (integer) 1
14. Srandmember method:
SRANDMEMBER key returns a random element in the collection.
This operation is similar to SPOP, except that SPOP removes and returns random elements from the collection, while SRANDMEMBER returns only random elements without making any changes to the collection.
Redis > SMEMBERS joe's_movies1) "hi, lady" 2) "Fast Five" 3) "2012" redis > SRANDMEMBER joe's_movies "Fast Five" redis > SMEMBERS joe's_movies # collection elements unchanged 1) "hi, lady" 2) "Fast Five" 3) "2012"
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.