Redis for Nosql: set Data Types and operation commands

Source: Internet
Author: User
Set Type 1: Overview set is a set, which is a unordered set of the string type. set is implemented through hashtable. The complexity of adding, deleting, and searching is O (1 ). for a set, we can take the Union set, intersection, and difference set. through these operations, we can implement the friend recommendation and blog tag FUNCTIONS In sns. ii. Related operation command 1: sadd Description: Add a member

Set Type 1: Overview set is a set, which is a unordered set of the string type. set is implemented through hash table. The complexity of adding, deleting, and searching is O (1 ). for a set, we can take the Union set, intersection, and difference set. through these operations, we can implement the friend recommendation and blog tag FUNCTIONS In sns. ii. Related operation command 1: sadd Description: Add a member

Set Type

I. Overview
Set is a collection, which is a disordered set of the string type. set is implemented through hash table,
The complexity of adding, deleting, and searching is O (1). For a set, we can take the Union set, intersection, and difference set.
Through these operations, we can implement the friend recommendation and blog tag FUNCTIONS In sns.

Ii. Related operation commands
1: sadd
Description: adds a member to the set sequence.
Return Value: the number of actually inserted members in this operation.
Command Format: sadd key member [member...]
Time Complexity: O (N) N: Number of table operation members
The procedure is as follows:

Redis 127.0.0.1: 6379> sadd my_set test1
(Integer) 1
Redis 127.0.0.1: 6379> sadd my_set test1
(Integer) 0
Redis 127.0.0.1: 6379> sadd my_set test2
(Integer) 1
Redis 127.0.0.1: 6379> smembers my_set
1) "test1 ″
2) "test2 ″
# Insert multiple members in a row
Redis 127.0.0.1: 6379> sadd my_set test3 test4
(Integer) 2
Redis 127.0.0.1: 6379> smembers my_set
1) "test3 ″
2) "test4 ″
3) "test1 ″
4) "test2 ″

2: smembers
Description: gets all the members in the set.
Time Complexity: O (1)
The operation is shown above.

3: scard
Description: gets the number of Set members.
Time Complexity: O (1)
4: sismember
Description: determines whether the specified member in the parameter already exists in the Set associated with the Key.
Time Complexity: O (1)
Returned value: Table 1 exists, and Table 0 does not exist.
The command is as follows:
Redis 127.0.0.1: 6379> scard my_set
(Integer) 4
Redis 127.0.0.1: 6379> sismember my_set1 aa
(Integer) 0
Redis 127.0.0.1: 6379> sismember my_set test1
(Integer) 1

5: spop
Description: Random removal and return to a member in the Set.
Time Complexity: O (1)
Returned value: the removed member. If the Key does not exist, nil is returned.
The command is as follows:
Redis 127.0.0.1: 6379> spop my_set
"Test3 ″
Redis 127.0.0.1: 6379> smembers my_set
1) "test4 ″
2) "test1 ″
3) "test2 ″

6: srem
Description: deletes a specified member (multiple members are allowed)
Time Complexity: O (N) N indicates the number of deleted members.
Command Format: srem key member [member...]
Returned value: returns the number of actually deleted members. If not, 0 is returned;
The command is as follows:
Redis 127.0.0.1: 6379> smembers my_set
1) "test4 ″
2) "test1 ″
3) "test2 ″
Redis 127.0.0.1: 6379>
Redis 127.0.0.1: 6379> srem my_set test4
(Integer) 1
Redis 127.0.0.1: 6379> smembers my_set
1) "test1 ″
2) "test2 ″
Redis 127.0.0.1: 6379> srem my_set test1 test2
(Integer) 2
Redis 127.0.0.1: 6379> smembers my_set
(Empty list or set)
7: srandmember
Description: Like SPOP, a member in the Set is returned randomly. The difference is that this command does not delete the returned member.
Time Complexity: O (1)
Return Value: return the random position of the member. If the Key does not exist, return nil.
The command is as follows:
Redis 127.0.0.1: 6379> srandmember my_set
(Nil)
Redis 127.0.0.1: 6379> sadd my_set test1 test2
(Integer) 2
Redis 127.0.0.1: 6379> srandmember my_set
"Test1 ″

8: smove
Description: moves an element in a set to another set.
Time Complexity: O (1)
Command Format: smove source destination member
Return Value: 1 indicates normal movement, and 0 indicates that the source does not contain parameter members.
The command is as follows:
Redis 127.0.0.1: 6379> smembers my_set
1) "test1 ″
2) "test2 ″
Redis 127.0.0.1: 6379> smembers my_set_2
(Empty list or set)
Redis 127.0.0.1: 6379> smove my_set my_set_2 test1
(Integer) 1
Redis 127.0.0.1: 6379> smembers my_set_2
1) "test1 ″
Redis 127.0.0.1: 6379> smembers my_set
1) "test2 ″

# When the Member is not stored
Redis 127.0.0.1: 6379> smove my_set_2 my_set_3 aa
(Integer) 0
9: sdiff
Description: return the differences between the Set associated with the first Key in the parameter and the Sets associated with all subsequent Keys.
Returned value: a set of members with different results.
Time Complexity: O (N) N indicates the total number of members in all Sets.
Command Format: sdiff key [key...]
The command is as follows:
Redis 127.0.0.1: 6379> smembers my_set_2
1) "test1 ″
Redis 127.0.0.1: 6379> smembers my_set
1) "test2 ″
Redis 127.0.0.1: 6379> sadd my_set test3
(Integer) 1
Redis 127.0.0.1: 6379> smembers my_set
1) "test3 ″
2) "test2 ″
Redis 127.0.0.1: 6379> sdiff my_set my_set_2
1) "test3 ″
2) "test2 ″
Redis 127.0.0.1: 6379> sdiff my_set_2 my_set
1) "test1 ″

10: sdiffstore
Description: The SDIFF command and the SDIFF command have the same functions. The only difference between the two is that SDIFF returns the result Member of the difference, and the command stores the difference member in the Set associated with destination. If the destination key already exists, this operation overwrites its members.
Returned value: returns the number of different members.
Time Complexity: O (N) N indicates the total number of members in all Sets.
Command Format: sdiffstore destination key [key...]
The command is as follows:
# Store the differential members of the Two sets in the Set associated with my_set_diff and return the number of inserted members.
Redis 127.0.0.1: 6379> sdiff my_set my_set2
1) "test3 ″
2) "test2 ″
Redis 127.0.0.1: 6379> sdiffstore my_set_diff my_set my_set_2
(Integer) 2
Redis 127.0.0.1: 6379> smembers my_set_diff
1) "test3 ″
2) "test2 ″

11: sinter
Description: This command returns the intersection of members in the Sets associated with Keys in the parameter. Therefore, if any Key in the parameter is associated

Or a Key does not exist, the result of this command is empty.
Time Complexity: O (N * M) N indicates the number of elements in the minimum Set, and M indicates the number of Sets in the parameter.
Command Format: sinter key [key...]
Returned value: a set of intersection result members.
The command is as follows:
Redis 127.0.0.1: 6379> sinter my_set my_set_diff
1) "test3 ″
2) "test2 ″

12: sinterstore
Description: Same as the sinter function, it returns the result Member of the intersection.
Time Complexity: O (N * M) N indicates the number of elements in the minimum Set, and M indicates the number of Sets in the parameter.
Command Format: sinterstore destination key [key...]
Return Value: returns the intersection member.
13: sunion

Description: returns the Union of members in the Sets associated with Keys in the parameter.
Command Format: sunion key [key...]
Time Complexity: O (N) N indicates the total number of members in all Sets.
The command is as follows:
Redis 127.0.0.1: 6379> sunion my_set my_set_2
1) "test3 ″
2) "test1 ″
3) "test2 ″

?
14: sunionstore
Description: returns the number of union members, which is the same as the sunion function.

The command is as follows:
Redis 127.0.0.1: 6379> sunionstore my_set my_set_2
(Integer) 1
Redis 127.0.0.1: 6379> sunion my_set my_set_2
1) "test1 ″

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.