[redis series] redis learning IV, set set, hash, zset ordered set preliminary cognition

Posted by bluegreen1 on Mon, 24 Jan 2022 22:33:26 +0100

Set set

The data in the set cannot be re read

  • SADD key member [member ...]

Adds an element to the set collection

  • SMEMBERS key

View all elements in the collection

  • SISMEMBER key member

Check whether a data is in the collection

  • SCARD key

View the number of set data, that is, the length of the set

  • SREM key member [member ...]

Removes the specified element from the collection

127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "wolrd" "xiaomotong"
(integer) 2
127.0.0.1:6379> SMEMBERS myset
1) "wolrd"
2) "xiaomotong"
3) "hello"
127.0.0.1:6379> SISMEMBER myset hello
(integer) 1
127.0.0.1:6379> SCARD myset
(integer) 3
127.0.0.1:6379> SREM myset wolrd
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "hello"
  • SRANDMEMBER key [count]

Randomly obtain the data in the set, and you can specify the number

127.0.0.1:6379> sadd myset "v1" "v2" "v3" "v4"
(integer) 4
127.0.0.1:6379> SMEMBERS myset
1) "v4"
2) "xiaomotong"
3) "v1"
4) "v2"
5) "hello"
6) "v3"
127.0.0.1:6379> SRANDMEMBER myset
"v2"
127.0.0.1:6379> SRANDMEMBER myset
"xiaomotong"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "v4"
2) "v1"
127.0.0.1:6379>
127.0.0.1:6379> SRANDMEMBER myset 2
1) "xiaomotong"
2) "v1"
  • SPOP key [count]

Randomly delete any element in the collection

127.0.0.1:6379> SMEMBERS myset
1) "v4"
2) "xiaomotong"
3) "v1"
4) "v2"
5) "hello"
6) "v3"
127.0.0.1:6379> SPOP myset
"v2"
127.0.0.1:6379> SMEMBERS myset
1) "v4"
2) "xiaomotong"
3) "v1"
4) "hello"
5) "v3"
127.0.0.1:6379> SPOP myset
"v4"
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "v1"
3) "hello"
4) "v3"
  • SMOVE source destination member

Take an element from the specified collection and put it into another collection

127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "v1"
3) "hello"
4) "v3"
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "v1"
3) "hello"
4) "v3"
127.0.0.1:6379> SMOVE myset newset v1
(integer) 1
127.0.0.1:6379> SMEMBERS newset
1) "v1"
127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "hello"
3) "v3"
  • SUNION key [key ...]

Union, union of two sets

  • SINTER key [key ...]

Take the intersection of two sets

127.0.0.1:6379> SMEMBERS myset
1) "xiaomotong"
2) "hello"
3) "v3"
127.0.0.1:6379> sadd newset v3
(integer) 1
127.0.0.1:6379> sadd newset v6 v9
(integer) 2
127.0.0.1:6379> SUNION myset newset
1) "v3"
2) "hello"
3) "xiaomotong"
4) "v1"
5) "v6"
6) "v9"
127.0.0.1:6379> SINTER myset newset
1) "v3"
127.0.0.1:6379>

set application scenarios can include common concerns of multiple users, friend recommendations, common fans, functional hobbies, etc

Hash hash

Map set, key map. At this time, key value is a set, which is essentially no different from string type. It is also a simple form of key value

  • HSET key field value [field value ...] / HMSET key field value [field value ...]

Add one or more key values to the hash key

  • HGET key field

Get the value corresponding to a key in the hash

  • HMGET key field [field ...]

Get the value corresponding to multiple key s in the hash

  • HGETALL key

Get all key value pairs in hash

127.0.0.1:6379> hset myhash name xiaomotong age 12 hobby play
(integer) 3
127.0.0.1:6379> hget myhash name
"xiaomotong"
127.0.0.1:6379> hmget myhash name age
1) "xiaomotong"
2) "12"
127.0.0.1:6379> hgetall myhash
1) "name"
2) "xiaomotong"
3) "age"
4) "12"
5) "hobby"
6) "play"
  • HDEL key field [field ...]

Delete one or more key s in the hash

127.0.0.1:6379> HSET myhash k1 v1 k2 v2 k3 v3
(integer) 3
127.0.0.1:6379> hgetall
(error) ERR wrong number of arguments for 'hgetall' command
127.0.0.1:6379> hgetall myhash
 1) "age"
 2) "12"
 3) "hobby"
 4) "play"
 5) "k1"
 6) "v1"
 7) "k2"
 8) "v2"
 9) "k3"
10) "v3"
127.0.0.1:6379> HDEL myhash k1 k2
(integer) 2
127.0.0.1:6379> HDEL myhash name
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "age"
2) "12"
3) "hobby"
4) "play"
5) "k3"
6) "v3"
  • HLEN key

Get the length of the hash, that is, the logarithm of the key value

127.0.0.1:6379> HLEN myhash
(integer) 3
  • HINCRBY key field increment

Add a value to a key in the set. If the value is positive, it is plus; if the value is negative, it is minus

  • HSETNX key field value

Add a key value pair to the hash. If it does not exist, it will be added. If it exists, it will fail to add

127.0.0.1:6379> hset myhash age 12
(integer) 1
127.0.0.1:6379> HINCRBY myhash age 2
(integer) 14
127.0.0.1:6379> HINCRBY myhash age -1
(integer) 13
127.0.0.1:6379> HSETNX myhash name xiaozhu
(integer) 1
127.0.0.1:6379> HSETNX myhash name xiaopangzi
(integer) 0
127.0.0.1:6379> hgetall myhash
1) "age"
2) "13"
3) "name"
4) "xiaopangzi"

The application scenarios of hash include places where data is often changed, especially some user information and frequently changing information

hash is more suitable for storing objects, and string is more suitable for storing strings

zset ordered set

zset is also a set, but a value is added to the set

  • ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]

To add scores and values to an ordered collection, you can add more than one

  • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Sorted in the order of fractions, - inf is negative infinity and + inf is positive infinity

  • ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]

View all the values in the ordered set

127.0.0.1:6379> ZADD myage 10 xiaoming 8 xiaohong 19 xiaozhu
(integer) 3
127.0.0.1:6379> ZRANGEBYSCORE myage 0 -1
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE myage -inf +inf
1) "xiaohong"
2) "xiaoming"
3) "xiaozhu"
127.0.0.1:6379> ZRANGEBYSCORE myage -inf +inf withscores
1) "xiaohong"
2) "8"
3) "xiaoming"
4) "10"
5) "xiaozhu"
6) "19"
127.0.0.1:6379> ZRANGEBYSCORE myage -inf 10
1) "xiaohong"
2) "xiaoming"
127.0.0.1:6379> ZRANGE myage 0 -1
1) "xiaohong"
2) "xiaoming"
3) "xiaozhu"
  • ZREM key member [member ...]

Delete the data in the ordered set

127.0.0.1:6379> ZREM myage xiaoming
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE myage -inf +inf withscores
1) "xiaohong"
2) "8"
3) "xiaozhu"
4) "19"
  • ZREVRANGE key start stop [WITHSCORES]

Sort ordered sets from large to small

127.0.0.1:6379> ZADD myage 7 xiaohe 17 xiaoliu 14 huhu
(integer) 3
127.0.0.1:6379> ZRANGE myage 0 -1
1) "xiaohe"
2) "xiaohong"
3) "huhu"
4) "xiaoliu"
5) "xiaozhu"
127.0.0.1:6379> ZRANGEBYSCORE myage -1 20
1) "xiaohe"
2) "xiaohong"
3) "huhu"
4) "xiaoliu"
5) "xiaozhu"
127.0.0.1:6379> ZRANGEBYSCORE myage -1 20 withscores
 1) "xiaohe"
 2) "7"
 3) "xiaohong"
 4) "8"
 5) "huhu"
 6) "14"
 7) "xiaoliu"
 8) "17"
 9) "xiaozhu"
10) "19"
127.0.0.1:6379> ZREVRANGE myage 0 -1
1) "xiaozhu"
2) "xiaoliu"
3) "huhu"
4) "xiaohong"
5) "xiaohe"

We can learn and practice other APIs on the official website of redis, and view the official Chinese documents of redis, www.redis.cn/

The application scenarios of zset are:

set sorting stores class grade information, salary information, etc. for sorting and filtering

Data or messages with weight, implementation of leaderboard, etc

Welcome to like, follow and collect

My friends, your support and encouragement are the driving force for me to insist on sharing and improve quality

Well, that's all for this time

Technology is open, and our mentality should be open. Embrace change, live in the sun and strive to move forward.

I'm Nezha, the Little Devil boy. Welcome to praise and pay attention to the collection. See you next time~

Topics: Go