Beckham _ redis set/zset type learning, _ rediszset
Redis set and zset types
I. collection type
2. view set and zset set commands
Iii. set and zset set instances
I. collection type
1. There are two types of redis collections: ordered and unordered.
2. Since it is a set type, it must also meet the characteristics of the set, that is
A. Set certainty: the nature of the elements that constitute the set must be clear and ambiguous.
B. Interaction of sets: for a given set, the elements in the set are different. "That is to say," for a given set, any two elements of the set are different.
C. sequence of a set: the elements that constitute a set are unordered.
3. Set category (assuming that both set A and Set B are not empty)
A. Union: removes the elements of the public parts of set A and Set B to form a new set.
B. Intersection: obtains the elements of the announcement section of set A and Set B to form A new set.
C. Empty set: a set of empty elements
2. view set and zset set commands
1. set commands
Command name |
Format |
Description |
Sadd |
Sadd key member |
Add the member element to the key. |
Srem |
Srem key member |
Remove the element member from the set key. |
Smembers |
Smembers key |
Lists all elements of a set key. |
Sismembers |
Sismembers key member |
Determine whether the member element is a set key element. |
Scard |
Scard key |
Obtains the number of elements in a set key. |
Smove |
Smove source destination member |
Move the member element from the source set to the destination set |
Spop |
Spop key |
Removes and returns a random element from the set. |
Srangmember |
Srangmember key |
Returns a random element in the set. |
Sinter |
Sinter key key1... |
Returns all members of a set, which is the intersection of all given sets. A key that does not exist is considered as an empty set. When there is an empty set in a given set, the result is also an empty set (according to the set operation law ). |
Sinterstore |
Sinterstore destination key key1... |
This command is equivalent to sinter, But it saves the result to the destination set instead of simply returning the result set. If the destination set already exists, overwrite it |
Sunion |
Sunion key key1 |
Returns all members of a set, which is the union of all given sets. A key that does not exist is considered as an empty set. |
Sunionstore |
Sunionstore destination key key1... |
This command is equivalent to sunion, But it saves the result to the destination set instead of simply returning the result set. If destination already exists, overwrite it |
Sdiff |
Sdiff key key1 |
Returns all members of a set, which is the difference set of all given sets. A key that does not exist is considered as an empty set. |
Sdiffstore |
Sdiffstore destination key key1... |
This command is equivalent to sdiff, But it saves the result to the destination set instead of simply returning the result set. If the destination set already exists, overwrite it |
2. zset commands
Command name |
Format |
Description |
Zadd |
Zadd key score member |
Add the member Element and Its score value to the sorted set key. The score value can be an integer or a double-precision floating point number. |
Zrem |
Zrem key member |
Remove the member from the sorted set key. If the member is not in the sorted set, no action is performed. If the key exists but is not an ordered set type, an error is returned. |
Zcard |
Zcard key |
Returns the base number of the sorted set key. |
Zcount |
Zcount key min max |
Returns the members of The sorted set key whose score value is between min and max (by default, the score value is equal to min or max ). |
Zscore |
Zscore key member |
Returns the score value of the member in the sorted set key. |
Zincrby |
Zincrby key increment member |
Add incremental increment to the score value of the member of the sorted set key |
Zrange |
Zrange key start stop [WITHSCORES] |
Returns the members in the specified range in the sorted set key. The positions of Members are sorted in ascending order of score values (from small to large ). |
Zrevrange |
Zrevrange key start stop [WITHSCORES] |
Returns the members in the specified range in the sorted set key. The positions of members are listed in descending order of score values (from big to small ). |
Zrangebyscore |
Zrangebyscore key min max [WITHSCORES] [LIMIT offset count] |
Returns all the Members whose score values are between min and max (including members equal to min or max) in the sorted set key. Sorted members are sorted in ascending order of score values (from small to large) |
Zrevrangebyscore |
Zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count] |
Returns all members whose score values are between max and min in the sorted set key (default includes members equal to max or min. Sequential integrators are listed in descending order of score values (from big to small. |
Zrank |
Zrank key member |
Returns the ranking of members in the sorted set key. The sorted members are listed in ascending order of score values (from small to large. The ranking is based on 0, that is, the ranking of the smallest member with the score value is 0. |
Zrevrank |
Zrevrank key member |
Returns the ranking of members in the sorted set key. The sorted members are sorted in descending order of score values (from big to small. The ranking is based on 0. That is to say, the highest score member ranking is 0. |
Zremrangebyrank |
Zremrangebyrank key start stop |
All members in the specified rank interval except the sorted set key. The following parameters start and stop respectively indicate the intervals, including start and stop. |
Zremrangebyscore |
Zremrangebyscore key min max |
Removes all members whose score values are between min and max (including members equal to min or max) from the sorted set key. |
Zinterstore |
Zinterstore destination numkeys key [key...] [WEIGHTS weight [weight...] [aggregate sum | MIN | MAX] |
Calculates the intersection of one or more given ordered sets. The number of given keys must be specified by the numkeys parameter. |
Zunionstore |
Zunionstore destination numkeys key [key...] [WEIGHTS weight [weight...] [aggregate sum | MIN | MAX] |
Calculates the union of one or more given ordered sets. The number of keys must be specified with the numkeys parameter. By default, the score value of a member in the result set is the sum of the score values of the member in all given sets. |
3. Example of a set command
3.1 Add the 'xiaobei' element to the set user'
127.0.0.1: 6379> sadd user xiaobei
(Integer) 1
32. view all elements of the Set user
127.0.0.1: 6379> smembers user
1) "xiaobei"
3.3 determine whether xiaobei is a collection user element
127.0.0.1: 6379> sismember user xiaobei
(Integer) 1
3.4 remove the xiaobei element of the Set user.
127.0.0.1: 6379> sremuser xiaobei
(Integer) 1
3.5 determine the number of users in the Set
127.0.0.1: 6379> sadduser xiaobei
(Integer) 1
127.0.0.1: 6379> sadduser script-boy
(Integer) 1
127.0.0.1: 6379> scard user
(Integer) 2
3.6 move the script-boy element of the set user to the set user1.
127.0.0.1: 6379> smove user user1 script-boy
(Integer) 1
127.0.0.1: 6379> smembers user1
1) "script-boy"
3.7. Calculate the set user and set user1
127.0.0.1: 6379> smembers user
1) "script-boy"
2) "xiaobei"
127.0.0.1: 6379> smembers user1
1) "script-boy"
127.0.0.1: 6379> sinter user user1
1) "script-boy"
3.8 Save the intersection of the Set user and set user1 in user2.
127.0.0.1: 6379> sinterstore user2 user1 user
(Integer) 1
127.0.0.1: 6379> smembers user2
1) "script-boy"
3.9. Calculate the union of the Set user and set user1.
127.0.0.1: 6379> sunion user user1
1) "script-boy"
2) "xiaobei"
3.10. Obtain and save the set user and set user1 in user2.
127.0.0.1: 6379> sunionstore user2 user user1
(Integer) 1
127.0.0.1: 6379> smembers user2
1) "script-boy"
3.11. Calculate the difference set between the set user1 and the set user.
127.0.0.1: 6379> smembers user
1) "script-boy"
127.0.0.1: 6379> smembers user1
1) "script-boy"
2) "xiaobei"
127.0.0.1: 6379> sdiff user1 user
1) "xiaobei"
3.12 Save the difference set of the Set user1 to the set user to user2.
Wagner. 0.0.1: 6379> sdiffstore user2 user1 user
(Integer) 1
127.0.0.1: 6379> smembers user2
1) "xiaobei"
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.