Nosql--redis 2.4--set

Source: Internet
Author: User

First, overview:

unordered character set , and as with the list type, we can also perform operations such as adding, deleting, or judging whether an element exists on a data value of that type. It should be stated that the time complexity of these operations is O (1), which is the completion of the operation within a constant time. The maximum number of elements that a set can contain is 4294967295. Unlike the list type, duplicate elements are not allowed in the set collection, in other words , if you add the same element more than once, only one copy of the element will be retained in set. There is also a very important feature of the set type in terms of functionality compared to the list type, which is to perform aggregate computation operations between multiple sets on the server side, such as intersection, and set and difference set . Because these operations are done on the server side, they are highly efficient and also save a lot of network IO overhead. such as QQ Friend recommended is set of the difference set.


Second, the scope of application:

1, You can use the Redis set data type to track some unique data, such as access to a blog's unique IP address information. For this scenario, we only need to place the IP of the visitor in Redis each time we access the blog, and the set data type automatically guarantees the uniqueness of the IP address.

2, make full use of the set type of service-side aggregation operation convenient and efficient features, can be used to maintain the relationship between data objects. For example, the customer ID of all purchases of an electronic device is stored in a specified set, and the customer ID of the purchase of another electronic product is stored in another set, and if we want to obtain a customer who buys both of these goods at this time, Set the intersections command can give full play to its advantages of convenience and efficiency.


Third, the method:

1, Sadd Method:

Sadd key member [member ...]

Adding one or more member elements to the collection key, the member element that already exists in the collection is ignored.

If key does not exist, create a collection that contains only the member element as a member.

When key is not a collection type, an error is returned.


# Add a single element redis> Sadd bbs "discuz.net" (integer) # Add repeating element redis> Sadd bbs "discuz.net" (integer) 0# 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 ...]

Removes 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 Presence Element redis> Srem Languages non-exists-language (integer) 0# 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 in the collection key.

# scenario 1: Empty collection redis> EXISTS Not_exists_key # Non-existent key is considered an empty collection (integer) 0redis> smembers not_exists_key (empty list or set) # Scenario 2: Non-empty collection redis> sadd programming_language python (integer) 1redis> sadd programming_language Ruby (integer) 1redis& Gt 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 collection).


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 the members of a collection, which is the set of differences for all given collections, as standard for first-written collections .

A nonexistent key is considered an empty set.


redis> smembers myset11) "one" 2) "one" (redis> smembers myset21) "One" (2) "three" redis> Sdiff myset1 myset21) "one" R edis> Sdiff Myset2 myset11) "three"

6, Sdiffstore Method:


Sdiffstore destination key [key ...]

This command is equivalent to Sdiff, but it saves the result to the destination collection instead of simply returning the result set.

If the destination collection already exists, it is overwritten.

Destination can be the key itself.


redis> smembers myset11) "one" 2) "one", "Redis> smembers myset21", "2" "three" redis> sdiffstore myset3 myset1 Mys et21) "One" redis> smembers myset31) "One"


7, Sinter Method:

SINTER key [Key ...]

Returns all the members of a collection that are the intersection of all given collections.

A nonexistent key is considered an empty set.

When there is an empty set in a given collection, the result is also an empty set (according to the Law of collection Operations).

redis> smembers myset11) "one" 2) "one" (redis> smembers myset21) "One" (2) "three" redis> SINTER myset1 myset21) "One"

8, Sinterstore Method:

Sinterstore destination key [key ...]

This command is equivalent to sinter, but it saves the result to the destination collection instead of simply returning the result set.

If the destination collection already exists, it is overwritten.

Destination can be the key itself.


redis> smembers myset11) "one" 2) "one", "Redis> smembers myset21", "2", "three" redis> Sinterstore myset3 Myset1 my set2redis> smembers myset31) "The other"

9, Sunion Method:


Sunion key [Key ...]

Returns all the members of a collection, which is the set of all the given collections.

A nonexistent key is considered an empty set.


redis> smembers myset11) "one" 2) "one" (redis> smembers myset21) "One" (2) "three" redis> sunion myset1 myset21) " "2)" three "3)" one "

10, Sunionstore Method:


Sunionstore destination key [key ...]

This command is equivalent to Sunion, but it saves the result to the destination collection instead of simply returning the result set.

If the destination already exists, overwrite it.

Destination can be the key itself.

redis> smembers myset11) "one" 2) "one", "Redis> smembers myset21", "2", "three" redis> Sunionstore myset3 myset1 m yset2redis> smembers myset31) "2" "three" 3) "one"

11, Smove Method:

Smove Source Destination Member

Moves 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 and returns only 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) "" redis> sismember Joe ' s_movies "Bet Man" (integer) 0re Dis> sismember Joe ' S_movies "Fast Five" (integer) 1

14, Srandmember Method:

Srandmember key Returns a random element in the collection.

The operation is similar to Spop, but Spop removes and returns random elements from the collection, and Srandmember returns only random elements without any changes to the collection.

Redis> smembers Joe ' S_movies1) "Hi, Lady" 2) "Fast Five" 3) "" "Redis> srandmember Joe ' s_movies" Fast Five "Redis > Smembers Joe ' s_movies # element in the collection does not change 1) "Hi, Lady" 2) "Fast Five" 3) "2012"



Nosql--redis 2.4--set

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.