redis database operations (3)

set type

Add element to unordered set:
sadd KEY VALUE1 VALUE2 VALUE3 Example: ( sadd my_set1 abcddcsa )

Get the elements of an unordered set:
smembers KEY Example: ( smembers my_set1 )
Result:
1) "c"
2) "s"
3) "a"
4) "d"
5) "b"

Delete a specified element in an unordered set:
srem KEY VALUE Example: ( srem my_set1 a )
Query results through smembers my_set1:
1) "c"
2) "s"
3) "d"
4) "b"

Delete random elements in an unordered set:
spop KEY VALUE Example: ( spop my_set1 )
Query results by smembers my_set1:
1) "c"
2) "s"
3) "b"

Move an element of one set to another:
smove SRC_KEY DST_KEY VALUE Example: ( smove my_set1 my_set2 s )
query result by smembers my_set1:
1) "c"
2) "b"
query result by smembers my_set2:
1) "s"

Determine whether there is an element in the set:
sismember KEY VALUE Example: ( sismember my_set1 b )

交集:
sinter KEY1 KEY2
例:
sadd my_set1 a b c d e
sadd my_set2 c d e f g
sinter my_set1 my_set2
结果:
1) "c"
2) "d"
3) "e"

Merge the intersection of KEY1 and KEY2 into KEY3:
sinterstore KEY3 KEY1 KEY2
Example:
sinterstore my_set3 my_set1
my_set2 Query results through smembers my_set3:
1) "d"
2) "c"
3) "e"

并集:
sunion KEY1 KEY2
例:
sunion my_set1 my_set2
结果:
1) "g"
2) "c"
3) "s"
4) "d"
5) "a"
6) "f"
7) "e"
8) "b"

Merge the union of KEY1 KEY2 into KEY3:
sunionstore KEY3 KEY1 KEY2
Example:
sunionstore my_set3 my_set1
my_set2 Query results by smembers my_set3:
1) "g"
2) "c"
3) "s"
4) "d"
5) "a "
6) "f"
7) "e"
8) "b"

Difference set: (in set 1, remove the elements in set 2, leaving the elements that are not in set 2 but only in set 1)
sdiff KEY1 KEY2
Example:
sdiff my_set1 my_set2
Result:
1) "a"
2) "b"

Merge the difference of KEY1 and KEY2 into KEY3:

sdiffstore KEY3 KEY1 KEY2
Example:
sdiffstore my_set3 my_set2
my_set1 Query result by smembers my_set3:
1) "g"
2) "f"
3) "s"

Get the number of elements in the set:
scard KEY
Example:
scard my_set1

Randomly returns an element in the set:
srandmember KEY
Example:
srandmember my_set1

 


zset type

Add an element to a sorted set:
zadd KEY SCORE VALUE
Example:
zadd my_zset 1 'one'
zadd my_zset 2 'two'
zadd my_zset 3 'three'
zadd my_zset 4 'four'
zadd my_zset 5 'five'

正序获取有序集合中的元素:
zrange KEY START STOP (withscores)
例:
zrange my_zset 0 -1
结果:
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
例:
zrange my_zset 0 3 withscores
结果:
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"

Get the elements in a sorted set in reverse order:
zrevrange KEY START STOP (withscores)
Example:
zrevrange my_zset 1 3Results
:
1) "four"
2) "three"
3) "two"
Example:
zrevrange my_zset 1 -1 withscores
1) " four"
2) "4"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"

Delete an element in a sorted set:
zrem KEY VALUE
Example:
zrem my_zset two
Look at the result by zrange my_zset 0 -1:
1) "one"
2) "three"
3) "four"
4) "five"

Find the position of an element in a sorted set in positive order:
zrank KEY VALUE
Example:
zrank my_zset five
Result:
3

Find the position of an element in a sorted set in reverse order:
zrevrank KEY VALUE
Example:
zrevrank my_zset five
Result:
0

View the number of sorted set elements:
zcard KEY
Example:
zcard my_set

Returns the elements in the set whose SCORE is in the given interval:
zrangebyscore KEY SCORE_MIN SCORE_MAX (withscores)
Example:
zrangebyscore my_zset 1 4
Result:
1) "one"
2) "three"
3) "four"
Example:
zrangebyscore my_zset 1 4 withscores
Result:
1) "one"
2) "1"
3) "three"
4) "3"
5) "four"
6) "4"

Returns the number of elements in the set with SCORE in the given interval:
zcount KEY SCORE_MIN SCORE_MAX
Example:
zcount my_zset 1 4

View the SCORE value of an element:
zscore KEY VALUE

例:
zscore my_zset five

Remove elements in a set that are ranked in a given interval (forward):
zremrangebyrank KEY START STOP
Example:
zremrangebyrank my_zset 0 2

Delete elements in the set whose score is in the given interval:
zremrangebyscore KEY SCORE_MIN SCORE_MAX
Example:
zremrangebyscore my_zset 4 5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339415&siteId=291194637