How does Set Hash Zset operate in redis?

Command operation

set

   Add a zhangsan to the set

127.0.0.1:16379[3]> sadd set zhangsan
(integer) 1

   Add a zhangsan to the set (result: (integer) 0) is deduplicated

127.0.0.1:16379[3]> sadd set zhangsan
(integer) 0

   Get all members in set

127.0.0.1:16379[3]> SMEMBERS set 
1) "zhangsan"

   Add multiple values ​​to the set collection lisi wangwu zhaoliu

127.0.0.1:16379[3]> sadd set lisi wangwu zhaoliu 
(integer) 3

   Get all members in set

127.0.0.1:16379[3]> SMEMBERS set
1) "zhaoliu"
2) "wangwu"
3) "lisi"
4) "zhangsan"

   Determine whether there is lisi in the set collection, return 1

127.0.0.1:16379[3]> SISMEMBER set lisi
(integer) 1

   Determine whether there is haha ​​in the set collection without returning 0

127.0.0.1:16379[3]> SISMEMBER set haha
(integer) 0

   Remove the element whose value is zhaoliu in set

127.0.0.1:16379[3]> SREM set zhaoliu
(integer) 1

   Get information about all elements

127.0.0.1:16379[3]> smembers set
1) "wangwu"
2) "lisi"
3) "zhangsan"

   Randomly get an element in the set

127.0.0.1:16379[3]> SRANDMEMBER set
"lisi"

   Randomly get two elements in the set

127.0.0.1:16379[3]> SRANDMEMBER set 2
1) "lisi"
2) "wangwu"

   Get all the element information in the set

127.0.0.1:16379[3]> SMEMBERS set
1) "wangwu"
2) "lisi"
3) "zhangsan"

   Get (pop) the elements in the set

127.0.0.1:16379[3]> spop set
"zhangsan"

   Get all elements in the set

127.0.0.1:16379[3]> SMEMBERS set
1) "wangwu"
2) "lisi"

   Get all elements in the set
127.0.0.1:16379[3]> SMEMBERS set
1) "wangwu"
2) "lisi"

   Move the lisi element in the set to myset

127.0.0.1:16379[3]> SMOVE set myset lisi
(integer) 1

   Get all the elements in the set. At this time, one element called lisi is missing

127.0.0.1:16379[3]> SMEMBERS set
1) "wangwu"

   Get all the elements in myset. At this time, there is an additional element called lisi

127.0.0.1:16379[3]> smembers myset
1) "lisi"

   Five elements of abcde are added to collection1

127.0.0.1:16379[3]> sadd collection1 a b c d e
(integer) 5

   Added five elements of bcdfk to collection2

127.0.0.1:16379[3]> sadd collection2 b c d f k
(integer) 5
127.0.0.1:16379[3]> SMEMBERS collection1
1) "b"
2) "a"
3) "c"
4) "d"
5) "e"
127.0.0.1:16379[3]> SMEMBERS collection2
1) "f"
2) "b"
3) "c"
4) "d"
5) "k"

   Compare which elements collection1 has more than collection2

127.0.0.1:16379[3]> SDIFF collection1 collection2
1) "a"
2) "e"

   Compare which elements collection2 has more than collection1

127.0.0.1:16379[3]> SDIFF collection2 collection1
1) "f"
2) "k"

   Intersection of collection2 and collection1

127.0.0.1:16379[3]> SINTER collection1 collection2
1) "b"
2) "d"
3) "c"

   The union of collection2 and collection1

127.0.0.1:16379[3]> SUNION collection1 collection2
1) "a"
2) "b"
3) "e"
4) "d"
5) "f"
6) "k"
7) "c"

Scenario: Weibo A users put all the people they follow in a set collection! They also put their fans in a set.
Common attention, common hobbies, second-degree friends, recommended friends (six-degree segmentation theory)

hash

   Set a key to hash as a key-value pair whose name is zhangsan

127.0.0.1:16379[4]> hset hash name zhangsan
(integer) 1

   Get the value of the key name in the hash

127.0.0.1:16379[4]> hget hash name
"zhangsan"

   Set a key to hash two key-value pairs with age value 22 and key sex value nan

127.0.0.1:16379[4]> HSET hash age 22 sex nan
(integer) 2

   Get the value of name, age, sex in hash in batch

127.0.0.1:16379[4]> hmget hash name age sex
1) "zhangsan"
2) "22"
3) "nan"

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"
5) "sex"
6) "nan"

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"
5) "sex"
6) "nan"

   Delete the value of sex in the hash key

127.0.0.1:16379[4]> hdel hash sex
(integer) 1

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"

   Batch add to add value to hash

127.0.0.1:16379[4]> hmset hash score 22 grade 5
OK

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"
5) "score"
6) "22"
7) "grade"
8) "5"

   Get the length of the hash

127.0.0.1:16379[4]> hlen hash
(integer) 4

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"
5) "score"
6) "22"
7) "grade"
8) "5"

   Test whether the name key in the hash exists

127.0.0.1:16379[4]> HEXISTS hash name
(integer) 1

   Test whether the hah key exists in the hash

127.0.0.1:16379[4]> HEXISTS hash hah
(integer) 0

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"
5) "score"
6) "22"
7) "grade"
8) "5"

   Get all the key values ​​in the hash

127.0.0.1:16379[4]> HKEYS hash
1) "name"
2) "age"
3) "score"
4) "grade"

   Get all the value values ​​in the hash

127.0.0.1:16379[4]> HVALS hash
1) "zhangsan"
2) "22"
3) "22"
4) "5"

   Get all the values ​​in the hash

127.0.0.1:16379[4]> hgetall hash
1) "name"
2) "zhangsan"
3) "age"
4) "22"
5) "score"
6) "22"
7) "grade"
8) "5"

   The value in the specified key in hash is incremented

127.0.0.1:16379[4]> HINCRBY hash age 1
(integer) 23

   The value in the specified key in hash is incremented

127.0.0.1:16379[4]> HINCRBY hash age 1
(integer) 24

   Get the value of the specified key in the hash

127.0.0.1:16379[4]> hget hash age
"24"

   Set the value of the specified key in the hash, create it if not

127.0.0.1:16379[4]> HSETNX hash fav hiking
(integer) 1

   Set the value of the specified key in the hash, create it if not (fav already exists, so the creation fails)

127.0.0.1:16379[4]> HSETNX hash fav hiking
(integer) 0

   Get the value of the specified key in the hash

127.0.0.1:16379[4]> hgetall hash
 1) "name"
 2) "zhangsan"
 3) "age"
 4) "24"
 5) "score"
 6) "22"
 7) "grade"
 8) "5"
 9) "fav"
10) "hiking"

   Hash is more suitable for operating objects than string, and storage objects are all suitable for their value after all

127.0.0.1:16379[4]> hmset user:1001 name zhangsan age 23 fav swing
OK
127.0.0.1:16379[4]> hmget user:1001 name age fav
1) "zhangsan"
2) "23"
3) "swing"

zset

   Add a zhangsan with a score of 1 to the set collection

127.0.0.1:16379[5]> zadd set 1 zhangsan
(integer) 1

   Add a lisi with a score of 2 to the set collection

127.0.0.1:16379[5]> zadd set 2 lisi
(integer) 1

   Add a wangwu with a score of 3 to the set

127.0.0.1:16379[5]> zadd set 3 wangwu 
(integer) 1

   Get all the values ​​in the set

127.0.0.1:16379[5]> ZRANGE set 0 -1
1) "zhangsan"
2) "lisi"
3) "wangwu"

   Get members according to the score in the specified interval, the range of the interval is from negative infinity to positive infinity, and the score is from high to low

127.0.0.1:16379[5]> ZRANGEBYSCORE set -inf +inf
1) "zhangsan"
2) "lisi"
3) "wangwu"

   Get members according to the scores in the specified interval, the range of the interval is from negative infinity to positive infinity, and the scores are in reverse order from low to high with scores

127.0.0.1:16379[5]> ZRANGEBYSCORE set -inf +inf withscores
1) "zhangsan"
2) "1"
3) "lisi"
4) "2"
5) "wangwu"
6) "3"

   Get members according to the score in the specified interval. The range of the interval is from negative infinity to 2 (including 2), and the scores are in reverse order from low to high with score

127.0.0.1:16379[5]> ZRANGEBYSCORE set -inf 2 withscores
1) "zhangsan"
2) "1"
3) "lisi"
4) "2"

   Get all members in the set

127.0.0.1:16379[5]> ZRANGE set 0 -1
1) "zhangsan"
2) "wangwu"

   Get the length of the set

127.0.0.1:16379[5]> ZCARD set
(integer) 2

   Get the number of elements in the specified set score interval

127.0.0.1:16379[5]> ZCOUNT set 1 2
(integer) 1

   Get the number of elements in the specified set score interval, the score is not between 5 and 6, so it is 0

127.0.0.1:16379[5]> ZCOUNT set 5 6
(integer) 0

Guess you like

Origin blog.csdn.net/weixin_38071259/article/details/106336813