Sets types and operations
Set is a collection, which is an unordered set of the string type. Using hash table, the complexity of adding, deleting, and searching is zero (1 ). For a set, we can achieve the Union of communication and difference sets. Through these operations, we can implement the friend recommendation and blog tag FUNCTIONS In SNS.
Sadd
Add an element to the set. 1 is returned for success, 0 is returned for failure, and the repeated value is failed.
For example:
Sadd myset1 hello
Smembers
View elements in a set
For example:
Smembers myset1
Scard
View the number of elements in the Set
For example:
Scard myset1
Sismember
If the test element is not in the Set, 1 is returned if it does not exist. If it does not exist, 0 is returned.
For example:
Sismember myset1 two
Srem
If the element in the set is deleted successfully, 1 is returned. If the element fails, 0 is returned.
For example:
Srem myset1 hello
Spop
Randomly pops up an element from the set and deletes it. The value of the pop-up element is returned.
For example:
Spop myset1
Sdiff
Returns the difference set part of the first set in the second set.
For example:
Three two in myset1
Two One in myset2
Sdiff myset1 myset2 // No three one will only return three
Sdiffstore
Returns the division of the first set in the second set and saves the result to the other set.
For example:
Sdiffstore myset4 myset2 myset3 // Save the difference set of myset2 in myset3 to myset4
Sinter
Returns the intersection of all sets.
For example:
Sinter myset1 myset2
Sinterstore
Returns the intersection of all sets and saves them. The usage is the same as that of sdiffstore.
Sunion
Returns the union of all sets.
For example:
Sunion myset1 myset2
Sunionstore
Returns the union of all sets and saves them. The usage is the same as that of sdiffstore.
Smove
Move the specified Element in the first set to the second set.
For example:
Smove myset2 myset1 three
Srandmember
Returns an element of the set randomly without deleting the element.
Sorted Sets
Is an upgraded version of the set. It adds an ordered Attribute Based on the set. This attribute can be specified when you add and modify elements. After each attribute is specified, zset automatically re-adjusts the order according to the new value.
Zadd
Add an element to the set. Member and score are used for sorting. If the element exists, the order is updated.
For example:
Zadd myzset1 1 one
Zrange
View elements in the zset set
For example:
Zrange myzset1 0-1 withscores // withscores output sequence number, not required
Zrem
Deletes an element with the specified value.
For example:
Zrem myset1 two
Zincrby
Add an element to the set. If the element already exists, add the increment value to the score of the element. Otherwise, add the element to the set. The score value is increment.
For example:
Zincrby myzset1 2 four
Zrank
Returns the ranking of elements in the set, in the ascending order of score, that is, subscript, starting from 0 as the array.
For example:
Zrank myzset1 four
Zrevrank
Like zrank, the difference is that the sorting is from big to small.
Zrevrange
Returns the results from the set in descending order.
For example:
Zrevrange myzset1 0-1 withscores
Zrangebyscore
Returns the specified returned element.
For example
Zrangebyscore myzset1 2 3 withscores
Zcount
Returns the number of elements in the given range of the score in the set.
For example:
Zcount myzset1 2 4
Zremrangebyrank
Deletes an element within the specified index range in a collection.
For example:
Zremrangebyrank myzset1 1 1 3
Zremrangebyscore
Deletes an element within the specified score range in a set.
For example:
Zremrangebyscore myzset1 1 2
Redis sets types and operations