[Redis 系列]redis 學習四,set 集合,hash 雜湊,zset 有序集合初步認知

小魔童哪吒發表於2022-01-21

set 集合

set 集合裡面的資料是不能重讀的

  • SADD key member [member …]

向 set 集合中新增元素

  • SMEMBERS key

檢視集合中的所有元素

  • SISMEMBER key member

檢視某一個資料是否在集合中

  • SCARD key

檢視集合資料的個數,也就是集合的長度

  • SREM key member [member …]

移除集合中指定的元素

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]

隨機獲取集合裡面的資料,可以指定個數

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]

隨機刪除集合中的任意元素

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

從指定的集合中取出一個元素,放到另外一個集合中

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 …]

取並集,對兩個集合取並集

  • SINTER key [key …]

取交集,對兩個集合取交集

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 應用場景可以有,多個使用者的共同關注,好友推薦,共同的粉絲,功能的愛好等等

hash 雜湊

Map 集合,key-map,此時 key-value 是一個集合,本質上和 string 型別沒有啥區別,還是一個簡單的 key-value 形式

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

在 hash key 中新增 1 個或者多個 key-value

  • HGET key field

獲取 hash 裡面的一個 key 對應的 value

  • HMGET key field [field …]

獲取 hash 裡面的多個 key 對應的 value

  • HGETALL key

獲取 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 …]

刪除 hash 裡面的 key,一個或者多個

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

獲取 hash 的長度,也就是 key-value 的對數

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

在集合中的某個 key 上增加值,若值是正數,則是加,若值是負數則是減

  • HSETNX key field value

向 hash 中加入鍵值對,若不存在則新增,存在則新增失敗

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"

hash 的應用場景有,經常變更資料的地方,尤其是一些使用者的資訊,經常變動的資訊

hash 更適合物件的儲存,string 更加適合字串的儲存

zset 有序集合

zset 也是集合,只是在 set 的基礎上加上了一個值

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

向有序集合中新增 分數 和 值,可以新增多個

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

按照分數的順序排序,-inf 為負無窮,+inf 為正無窮

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

檢視有序集合裡面的所有的值

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 …]

刪除有序集合裡面的資料

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]

對有序集合從大到小排序

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"

其餘的 api 我們可以到 redis 官網上去進行學習和實踐,可以檢視 redis 官方的中文文件,www.redis.cn/

zset 的應用場景有:

set 排序,儲存班級成績資訊,工資資訊等等,用於排序和篩選的

帶有權重的資料或者訊息,排行榜的實現等等

歡迎點贊,關注,收藏

朋友們,你的支援和鼓勵,是我堅持分享,提高質量的動力

好了,本次就到這裡

技術是開放的,我們的心態,更應是開放的。擁抱變化,向陽而生,努力向前行。

我是小魔童哪吒,歡迎點贊關注收藏,下次見~

本作品採用《CC 協議》,轉載必須註明作者和本文連結
關注微信公眾號:小魔童哪吒

相關文章