Redis資料型別
通用操作
# 判斷key是否存在
127.0.0.1:6379> EXISTS name
(
integer
) 1
127.0.0.1:6379> EXISTS name1
(
integer
) 0
# 修改key名字
127.0.0.1:6379> RENAME name name2
OK
# 檢視指定key的資料型別
127.0.0.1:6379>
type
name2
string
127.0.0.1:6379>
type
PHPREDIS_SESSION:5555fb82ae8b698030baad3da87f9a8b
string
# 刪除key
127.0.0.1:6379> del name2
(
integer
) 1
# 檢視一個key的生存時間
127.0.0.1:6379> ttl age
(
integer
) -1
127.0.0.1:6379> ttl PHPREDIS_SESSION:5555fb82ae8b698030baad3da87f9a8b
(
integer
) 1212
# 以秒為單位設定生存時間
127.0.0.1:6379> EXPIRE aaa 100
# 以毫秒為單位設定生存時間
127.0.0.1:6379> PEXPIRE aaa 100
# 取消生存時間
127.0.0.1:6379> PERSIST aaa
字串型別操作(strings)
增
# 設定key
127.0.0.1:6379>
set
name zls
OK
# 設定多個key
127.0.0.1:6379> mset name zls name1 huanglong name2 wuyangke
OK
# 檢視多個key值
127.0.0.1:6379> MGET name name1 name2
1)
"zls"
2)
"huanglong"
3)
"wuyangke"
# 先獲取一個key的值,再設定或者修改key值
127.0.0.1:6379> getset xxx 123
(nil)
127.0.0.1:6379> get xxx
"123"
# 設定key同時設定生存時間(秒為單位)
127.0.0.1:6379>
set
book hongloumeng ex 100
OK
# 設定key同時設定生存時間(毫秒為單位)
127.0.0.1:6379>
set
book hongloumeng px 100
OK
# 字串自增
127.0.0.1:6379> incr zan
(
integer
) 1
# 指定增加數量
127.0.0.1:6379> incrby fans 10000
(
integer
) 10023
# 自減
127.0.0.1:6379> decr fans
(
integer
) 10012
# 執行自減數量
127.0.0.1:6379> decrby fans 1000
(
integer
) 9012
# 按照小數自增
127.0.0.1:6379> incrbyfloat zls 0.1
"1.4"
刪
127.0.0.1:6379> del zls
(
integer
) 1
改
## 字串追加
127.0.0.1:6379> APPEND name1 liquanyi
(
integer
) 17
127.0.0.1:6379> get name1
"huanglongliquanyi"
## 修改第N個字元
127.0.0.1:6379> setrange name1 5 L
(
integer
) 17
127.0.0.1:6379> get name1
"huangLongliquanyi"
查
# 檢視一個key
127.0.0.1:6379> get name1
"huangLongliquanyi"
# 檢視多個key
127.0.0.1:6379> mget name1 name2
1)
"huangLongliquanyi"
2)
"wuyangke"
# 檢視字串的長度
127.0.0.1:6379> strlen name1
(
integer
) 17
127.0.0.1:6379> strlen name2
(
integer
) 8
# 檢視生存時間
127.0.0.1:6379> ttl name1 // 秒
(
integer
) 93
127.0.0.1:6379> pttl name1 // 毫秒
(
integer
) 91417
# 字串擷取
127.0.0.1:6379> getrange name1 0 4
"huang"
hash型別(字典型別)
# 建立key
hset keyname field value
hset student_id_1 name zls
hset student_id_1 name zls age 18 gender m
hmset student_id_1 name zls age 18 gender m
# 查詢
hget keyname field
hget student_id_1 name
hgetall keyname
hgetall student_id_1
hmget student_id_1 name age
# 刪除
127.0.0.1:6379> hdel student_id_1 age gender
(
integer
) 2
127.0.0.1:6379> hgetall student_id_1
127.0.0.1:6379> del student_id_1
# 改
127.0.0.1:6379> hset student_id_1 name zls age 18 gender m
(
integer
) 3
127.0.0.1:6379> hgetall student_id_1
1)
"fans"
2)
"8"
3)
"zan"
4)
"9"
5)
"name"
6)
"zls"
7)
"age"
8)
"18"
9)
"gender"
10)
"m"
list型別(列表型別)

-
RabbitMQ(最早金融公司,OpenStack)
-
ZeroMQ(SaltStack)
-
RocketMQ
-
Kafka(Java)
-
Redis(訊息佇列)
kafka
在kafka中可以建立topic會話
topic會話組,可以重複消費,消費者取資料根據topic的偏移量來取
公司的OA系統使用Jira
公司的文件系統使用 confluence
127.0.0.1:6379> lpush access_log
'10.0.0.1 - - [03/Sep/2022:03:38:41 +0800] "POST /index.php HTTP/1.1" 200 1500 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" "-"'
127.0.0.1:6379> lpush access_log
'10.0.0.1 - - [03/Sep/2022:03:54:09 +0800] "POST /index.php HTTP/1.1" 200 1500 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" "-"'
# 將資料新增在可以的最右邊
127.0.0.1:6379> rpush name1 hl
(
integer
) 1
127.0.0.1:6379> rpush name1 wyk
(
integer
) 2
127.0.0.1:6379> rpush name1 zls
(
integer
) 3
# 如果資料不存在則不建立
lpushx name2 hl
# 將資料插入到key的任意位置
127.0.0.1:6379> linsert name2 after hl hjx
after:在...之後
before:在...之前
# 檢視key中的指定範圍資料
127.0.0.1:6379> lrange name 0 2
(empty array)
127.0.0.1:6379> lrange name1 0 2
1)
"hl"
2)
"wyk"
3)
"zls"
# 從key的最左邊取出資料
127.0.0.1:6379> lpop name1
"hl"
127.0.0.1:6379> lpop name1
"wyk"
127.0.0.1:6379> lpop name1
"zls"
# 檢視key的長度
127.0.0.1:6379> llen name1
lpush:左邊寫入
rpush:右邊寫入
lpop:左邊取出
rpop:右邊取出
# 將name1右邊的資料取出,寫入name2左邊
127.0.0.1:6379> lrange name1 0 2
1)
"wyk"
2)
"hjx"
3)
"xwq"
127.0.0.1:6379> lrange name2 0 2
1)
"hl"
2)
"hjx"
3)
"zls"
127.0.0.1:6379> rpoplpush name1 name2
"xwq"
127.0.0.1:6379> lrange name2 0 2
1)
"xwq"
2)
"hl"
3)
"hjx"
127.0.0.1:6379> lrange name2 0 3
1)
"xwq"
2)
"hl"
3)
"hjx"
4)
"zls"
## 刪除
# 從左邊開始刪除指定個數元素 lrem
127.0.0.1:6379> lrange name2 0 100
1)
"hl"
2)
"hl"
3)
"hl"
4)
"hl"
5)
"hl"
6)
"hl"
7)
"xwq"
8)
"hl"
9)
"hjx"
10)
"zls"
11)
"hl"
12)
"hl"
13)
"hl"
14)
"hl"
127.0.0.1:6379> lrem name2 7 hl
(
integer
) 7
127.0.0.1:6379> lrange name2 0 100
1)
"xwq"
2)
"hjx"
3)
"zls"
4)
"hl"
5)
"hl"
6)
"hl"
7)
"hl"
# 保留指定下標的元素,其餘的都刪除
127.0.0.1:6379> lrange name2 0 100
1)
"xwq"
2)
"hjx"
3)
"zls"
4)
"hl"
5)
"hl"
6)
"hl"
7)
"hl"
127.0.0.1:6379> ltrim name2 0 1
OK
127.0.0.1:6379> lrange name2 0 100
1)
"xwq"
2)
"hjx"
## 改
127.0.0.1:6379> lrange name1 0 100
1)
"wyk"
2)
"hjx"
127.0.0.1:6379> lset name1 0 xwq
OK
127.0.0.1:6379> lrange name1 0 100
1)
"xwq"
2)
"hjx"
## 查
# 按照索引查詢
127.0.0.1:6379> lindex name1 1
"hjx"
127.0.0.1:6379> lindex name1 0
"xwq"
127.0.0.1:6379> lindex name1 2
(nil)
127.0.0.1:6379> lindex name1 -1 //最後一個元素
"hjx"
## 發朋友圈
127.0.0.1:6379> lpush wechat
'monday hl sb'
(
integer
) 1
127.0.0.1:6379> lpush wechat
'tiusiday hl yiran sb'
(
integer
) 2
127.0.0.1:6379> lpush wechat
'wensiday hl always sb'
(
integer
) 3
## 檢視朋友圈
127.0.0.1:6379> lrange wechat 0 -1
1)
"wensiday hl always sb"
2)
"tiusiday hl yiran sb"
3)
"monday hl sb"
set(集合型別)
組1:1 2 3 4 5
組2: 1 3 5 7 9
交集:1 3 5
並集:1 2 3 4 5 7 9
差集:2 4 7 9
# 建立集合
127.0.0.1:6379> sadd zls_fans wyk hl
(
integer
) 2
127.0.0.1:6379> sadd tly_fans mls gaofei wyk
(
integer
) 3
# 查
## 查詢集合中的元素,有就返回1 沒有就返回0
127.0.0.1:6379> sismember zls_fans hl
(
integer
) 1
127.0.0.1:6379> sismember zls_fans cls
(
integer
) 0
## 查詢集合中的所有元素
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
## 查詢集合中元素數量
127.0.0.1:6379> scard zls_fans
(
integer
) 2
127.0.0.1:6379> scard tly_fans
(
integer
) 3
## 比較集合中的差異,取前面集合的不同之處
127.0.0.1:6379> sdiff hl_fans tly_fans zls_fans
1)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"mls"
3)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS tly_fans
1)
"wyk"
2)
"mls"
3)
"gaofei"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
127.0.0.1:6379> sdiff hl_fans tly_fans
1)
"hl"
2)
"xiaoxuesheng"
127.0.0.1:6379> sdiff tly_fans hl_fans
1)
"wyk"
2)
"gaofei"
127.0.0.1:6379> sdiff tly_fans hl_fans zls_fans
1)
"gaofei"
## 比較之後的差異,存放到新的集合中
127.0.0.1:6379> sdiffstore resault tly_fans hl_fans
(
integer
) 2
127.0.0.1:6379> KEYS *
1)
"hl_fans"
2)
"resault"
3)
"name2"
4)
"tly_fans"
5)
"name1"
6)
"wechat"
7)
"zls_fans"
127.0.0.1:6379>
type
resault
set
127.0.0.1:6379> scard resault
(
integer
) 2
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS resault
1)
"wyk"
2)
"gaofei"
## 交集(共同好友)
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"mls"
3)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
127.0.0.1:6379> sinter hl_fans zls_fans
1)
"hl"
## 並集
127.0.0.1:6379> sunion hl_fans zls_fans
1)
"hl"
2)
"wyk"
3)
"mls"
4)
"xiaoxuesheng"
## 將交集存入新集合
127.0.0.1:6379> sinterstore new_set hl_fans zls_fans
(
integer
) 1
127.0.0.1:6379> SMEMBERS new_set
1)
"hl"
## 將並集存入新集合
127.0.0.1:6379> sunionstore new_set2 hl_fans zls_fans
(
integer
) 4
127.0.0.1:6379> SMEMBERS new_set2
1)
"hl"
2)
"wyk"
3)
"mls"
4)
"xiaoxuesheng"
## 取隨機值
127.0.0.1:6379> srandmember zls_fans
"mls"
## 改
## 將集合的元素存入另一個集合 SMOVE
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"mls"
3)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
127.0.0.1:6379> SMOVE hl_fans zls_fans mls
(
integer
) 1
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
3)
"mls"
## 刪
127.0.0.1:6379> spop hl_fans
"hl"
127.0.0.1:6379> SMEMBERS hl_fans
1)
"xiaoxuesheng"
## 刪除指定元素,返回刪除元素的個數
127.0.0.1:6379> srem zls_fans hl
(
integer
) 1
127.0.0.1:6379> SMEMBERS zls_fans
1)
"wyk"
2)
"mls"
127.0.0.1:6379> srem zls_fans xxx
(
integer
) 0
#### 無序的
127.0.0.1:6379> sadd zls_fans 111 222 333 444 555 666
(
integer
) 6
127.0.0.1:6379> SMEMBERS zls_fans
1)
"abc"
2)
"111"
3)
"mls"
4)
"123"
5)
"def"
6)
"333"
7)
"222"
8)
"456"
9)
"444"
10)
"555"
11)
"666"
12)
"wyk"
sorted-set(有序集合)
# 增
127.0.0.1:6379> zadd myzset 1
"one"
2
"two"
3
"three"
# 查
## 只查詢成員
127.0.0.1:6379> zrange chengji 0 -1
1)
"wyk"
2)
"hl"
3)
"zls"
## 查詢成員和分數
127.0.0.1:6379> zrange chengji 0 -1 WITHSCORES
1)
"wyk"
2)
"1"
3)
"hl"
4)
"38"
5)
"zls"
6)
"100"
## 查詢成員的索引
127.0.0.1:6379> zrank chengji wyk
(
integer
) 0
127.0.0.1:6379> zrank chengji zls
(
integer
) 2
## 查詢成員數量
127.0.0.1:6379> zcard chengji
(
integer
) 3
## 檢視分數是指定範圍的成員個數 30 <= score <=40
127.0.0.1:6379> zcount chengji 30 40
(
integer
) 1
127.0.0.1:6379> zcount chengji 30 101
(
integer
) 2
## 獲取指定成員分數
127.0.0.1:6379> ZSCORE chengji wyk
"1"
127.0.0.1:6379> ZSCORE chengji zls
"100"
## 檢視分數是指定範圍的成員名
127.0.0.1:6379> zcount chengji 30 40
(
integer
) 1
127.0.0.1:6379> zrangebyscore chengji 30 40
1)
"hl"
## +inf表示最後一個成員,-inf表示第一個成員,意思是:檢索所有資料,然後從下標為2的資料開始再往後輸出3:(N-1)個數據
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 3
1)
"hjx"
2)
"xwq"
3)
"zls"
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 2
1)
"hjx"
2)
"xwq"
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 1
1)
"hjx"
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 4
1)
"hjx"
2)
"xwq"
3)
"zls"
# 刪
## 刪除指定分數範圍的成員,並返回刪除的個數
127.0.0.1:6379> zrangebyscore chengji 30 40
1)
"hl"
127.0.0.1:6379> zremrangebyscore chengji 30 40
(
integer
) 1
## 按指定索引範圍刪除,返回刪除個數
127.0.0.1:6379> zremrangebyrank chengji 0 2
(
integer
) 3
## 倒序排名
127.0.0.1:6379> zrevrange chengji 0 -1 WITHSCORES
1)
"zls"
2)
"100"
3)
"hjx"
4)
"60"
5)
"xwq"
6)
"50"
7)
"hl"
8)
"38"
9)
"wyk"
10)
"1"
## 按照分數段查詢成員,並倒序排序
127.0.0.1:6379> zrevrangebyscore chengji 101 30 WITHSCORES
1)
"zls"
2)
"100"
3)
"hjx"
4)
"60"
5)
"xwq"
6)
"50"
7)
"hl"
8)
"38"
## 按照分數查詢成員,並倒序排序,然後按照索引號,取出指定的資料
127.0.0.1:6379> zrevrangebyscore chengji 101 30 WITHSCORES
limit
1 2
1)
"hjx"
2)
"60"
3)
"xwq"
4)
"50"
127.0.0.1:6379> zrevrangebyscore chengji 101 30 WITHSCORES
limit
1 3
1)
"hjx"
2)
"60"
3)
"xwq"
4)
"50"
5)
"hl"
6)
"38"
通用操作
# 判斷key是否存在
127.0.0.1:6379> EXISTS name
(
integer
) 1
127.0.0.1:6379> EXISTS name1
(
integer
) 0
# 修改key名字
127.0.0.1:6379> RENAME name name2
OK
# 檢視指定key的資料型別
127.0.0.1:6379>
type
name2
string
127.0.0.1:6379>
type
PHPREDIS_SESSION:5555fb82ae8b698030baad3da87f9a8b
string
# 刪除key
127.0.0.1:6379> del name2
(
integer
) 1
# 檢視一個key的生存時間
127.0.0.1:6379> ttl age
(
integer
) -1
127.0.0.1:6379> ttl PHPREDIS_SESSION:5555fb82ae8b698030baad3da87f9a8b
(
integer
) 1212
# 以秒為單位設定生存時間
127.0.0.1:6379> EXPIRE aaa 100
# 以毫秒為單位設定生存時間
127.0.0.1:6379> PEXPIRE aaa 100
# 取消生存時間
127.0.0.1:6379> PERSIST aaa
字串型別操作(strings)
增
# 設定key
127.0.0.1:6379>
set
name zls
OK
# 設定多個key
127.0.0.1:6379> mset name zls name1 huanglong name2 wuyangke
OK
# 檢視多個key值
127.0.0.1:6379> MGET name name1 name2
1)
"zls"
2)
"huanglong"
3)
"wuyangke"
# 先獲取一個key的值,再設定或者修改key值
127.0.0.1:6379> getset xxx 123
(nil)
127.0.0.1:6379> get xxx
"123"
# 設定key同時設定生存時間(秒為單位)
127.0.0.1:6379>
set
book hongloumeng ex 100
OK
# 設定key同時設定生存時間(毫秒為單位)
127.0.0.1:6379>
set
book hongloumeng px 100
OK
# 字串自增
127.0.0.1:6379> incr zan
(
integer
) 1
# 指定增加數量
127.0.0.1:6379> incrby fans 10000
(
integer
) 10023
# 自減
127.0.0.1:6379> decr fans
(
integer
) 10012
# 執行自減數量
127.0.0.1:6379> decrby fans 1000
(
integer
) 9012
# 按照小數自增
127.0.0.1:6379> incrbyfloat zls 0.1
"1.4"
刪
127.0.0.1:6379> del zls
(
integer
) 1
改
## 字串追加
127.0.0.1:6379> APPEND name1 liquanyi
(
integer
) 17
127.0.0.1:6379> get name1
"huanglongliquanyi"
## 修改第N個字元
127.0.0.1:6379> setrange name1 5 L
(
integer
) 17
127.0.0.1:6379> get name1
"huangLongliquanyi"
查
# 檢視一個key
127.0.0.1:6379> get name1
"huangLongliquanyi"
# 檢視多個key
127.0.0.1:6379> mget name1 name2
1)
"huangLongliquanyi"
2)
"wuyangke"
# 檢視字串的長度
127.0.0.1:6379> strlen name1
(
integer
) 17
127.0.0.1:6379> strlen name2
(
integer
) 8
# 檢視生存時間
127.0.0.1:6379> ttl name1 // 秒
(
integer
) 93
127.0.0.1:6379> pttl name1 // 毫秒
(
integer
) 91417
# 字串擷取
127.0.0.1:6379> getrange name1 0 4
"huang"
hash型別(字典型別)
# 建立key
hset keyname field value
hset student_id_1 name zls
hset student_id_1 name zls age 18 gender m
hmset student_id_1 name zls age 18 gender m
# 查詢
hget keyname field
hget student_id_1 name
hgetall keyname
hgetall student_id_1
hmget student_id_1 name age
# 刪除
127.0.0.1:6379> hdel student_id_1 age gender
(
integer
) 2
127.0.0.1:6379> hgetall student_id_1
127.0.0.1:6379> del student_id_1
# 改
127.0.0.1:6379> hset student_id_1 name zls age 18 gender m
(
integer
) 3
127.0.0.1:6379> hgetall student_id_1
1)
"fans"
2)
"8"
3)
"zan"
4)
"9"
5)
"name"
6)
"zls"
7)
"age"
8)
"18"
9)
"gender"
10)
"m"
list型別(列表型別)
-
RabbitMQ(最早金融公司,OpenStack)
-
ZeroMQ(SaltStack)
-
RocketMQ
-
Kafka(Java)
-
Redis(訊息佇列)
kafka
在kafka中可以建立topic會話
topic會話組,可以重複消費,消費者取資料根據topic的偏移量來取
公司的OA系統使用Jira
公司的文件系統使用 confluence
127.0.0.1:6379> lpush access_log
'10.0.0.1 - - [03/Sep/2022:03:38:41 +0800] "POST /index.php HTTP/1.1" 200 1500 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" "-"'
127.0.0.1:6379> lpush access_log
'10.0.0.1 - - [03/Sep/2022:03:54:09 +0800] "POST /index.php HTTP/1.1" 200 1500 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" "-"'
# 將資料新增在可以的最右邊
127.0.0.1:6379> rpush name1 hl
(
integer
) 1
127.0.0.1:6379> rpush name1 wyk
(
integer
) 2
127.0.0.1:6379> rpush name1 zls
(
integer
) 3
# 如果資料不存在則不建立
lpushx name2 hl
# 將資料插入到key的任意位置
127.0.0.1:6379> linsert name2 after hl hjx
after:在...之後
before:在...之前
# 檢視key中的指定範圍資料
127.0.0.1:6379> lrange name 0 2
(empty array)
127.0.0.1:6379> lrange name1 0 2
1)
"hl"
2)
"wyk"
3)
"zls"
# 從key的最左邊取出資料
127.0.0.1:6379> lpop name1
"hl"
127.0.0.1:6379> lpop name1
"wyk"
127.0.0.1:6379> lpop name1
"zls"
# 檢視key的長度
127.0.0.1:6379> llen name1
lpush:左邊寫入
rpush:右邊寫入
lpop:左邊取出
rpop:右邊取出
# 將name1右邊的資料取出,寫入name2左邊
127.0.0.1:6379> lrange name1 0 2
1)
"wyk"
2)
"hjx"
3)
"xwq"
127.0.0.1:6379> lrange name2 0 2
1)
"hl"
2)
"hjx"
3)
"zls"
127.0.0.1:6379> rpoplpush name1 name2
"xwq"
127.0.0.1:6379> lrange name2 0 2
1)
"xwq"
2)
"hl"
3)
"hjx"
127.0.0.1:6379> lrange name2 0 3
1)
"xwq"
2)
"hl"
3)
"hjx"
4)
"zls"
## 刪除
# 從左邊開始刪除指定個數元素 lrem
127.0.0.1:6379> lrange name2 0 100
1)
"hl"
2)
"hl"
3)
"hl"
4)
"hl"
5)
"hl"
6)
"hl"
7)
"xwq"
8)
"hl"
9)
"hjx"
10)
"zls"
11)
"hl"
12)
"hl"
13)
"hl"
14)
"hl"
127.0.0.1:6379> lrem name2 7 hl
(
integer
) 7
127.0.0.1:6379> lrange name2 0 100
1)
"xwq"
2)
"hjx"
3)
"zls"
4)
"hl"
5)
"hl"
6)
"hl"
7)
"hl"
# 保留指定下標的元素,其餘的都刪除
127.0.0.1:6379> lrange name2 0 100
1)
"xwq"
2)
"hjx"
3)
"zls"
4)
"hl"
5)
"hl"
6)
"hl"
7)
"hl"
127.0.0.1:6379> ltrim name2 0 1
OK
127.0.0.1:6379> lrange name2 0 100
1)
"xwq"
2)
"hjx"
## 改
127.0.0.1:6379> lrange name1 0 100
1)
"wyk"
2)
"hjx"
127.0.0.1:6379> lset name1 0 xwq
OK
127.0.0.1:6379> lrange name1 0 100
1)
"xwq"
2)
"hjx"
## 查
# 按照索引查詢
127.0.0.1:6379> lindex name1 1
"hjx"
127.0.0.1:6379> lindex name1 0
"xwq"
127.0.0.1:6379> lindex name1 2
(nil)
127.0.0.1:6379> lindex name1 -1 //最後一個元素
"hjx"
## 發朋友圈
127.0.0.1:6379> lpush wechat
'monday hl sb'
(
integer
) 1
127.0.0.1:6379> lpush wechat
'tiusiday hl yiran sb'
(
integer
) 2
127.0.0.1:6379> lpush wechat
'wensiday hl always sb'
(
integer
) 3
## 檢視朋友圈
127.0.0.1:6379> lrange wechat 0 -1
1)
"wensiday hl always sb"
2)
"tiusiday hl yiran sb"
3)
"monday hl sb"
set(集合型別)
組1:1 2 3 4 5
組2: 1 3 5 7 9
交集:1 3 5
並集:1 2 3 4 5 7 9
差集:2 4 7 9
# 建立集合
127.0.0.1:6379> sadd zls_fans wyk hl
(
integer
) 2
127.0.0.1:6379> sadd tly_fans mls gaofei wyk
(
integer
) 3
# 查
## 查詢集合中的元素,有就返回1 沒有就返回0
127.0.0.1:6379> sismember zls_fans hl
(
integer
) 1
127.0.0.1:6379> sismember zls_fans cls
(
integer
) 0
## 查詢集合中的所有元素
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
## 查詢集合中元素數量
127.0.0.1:6379> scard zls_fans
(
integer
) 2
127.0.0.1:6379> scard tly_fans
(
integer
) 3
## 比較集合中的差異,取前面集合的不同之處
127.0.0.1:6379> sdiff hl_fans tly_fans zls_fans
1)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"mls"
3)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS tly_fans
1)
"wyk"
2)
"mls"
3)
"gaofei"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
127.0.0.1:6379> sdiff hl_fans tly_fans
1)
"hl"
2)
"xiaoxuesheng"
127.0.0.1:6379> sdiff tly_fans hl_fans
1)
"wyk"
2)
"gaofei"
127.0.0.1:6379> sdiff tly_fans hl_fans zls_fans
1)
"gaofei"
## 比較之後的差異,存放到新的集合中
127.0.0.1:6379> sdiffstore resault tly_fans hl_fans
(
integer
) 2
127.0.0.1:6379> KEYS *
1)
"hl_fans"
2)
"resault"
3)
"name2"
4)
"tly_fans"
5)
"name1"
6)
"wechat"
7)
"zls_fans"
127.0.0.1:6379>
type
resault
set
127.0.0.1:6379> scard resault
(
integer
) 2
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS resault
1)
"wyk"
2)
"gaofei"
## 交集(共同好友)
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"mls"
3)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
127.0.0.1:6379> sinter hl_fans zls_fans
1)
"hl"
## 並集
127.0.0.1:6379> sunion hl_fans zls_fans
1)
"hl"
2)
"wyk"
3)
"mls"
4)
"xiaoxuesheng"
## 將交集存入新集合
127.0.0.1:6379> sinterstore new_set hl_fans zls_fans
(
integer
) 1
127.0.0.1:6379> SMEMBERS new_set
1)
"hl"
## 將並集存入新集合
127.0.0.1:6379> sunionstore new_set2 hl_fans zls_fans
(
integer
) 4
127.0.0.1:6379> SMEMBERS new_set2
1)
"hl"
2)
"wyk"
3)
"mls"
4)
"xiaoxuesheng"
## 取隨機值
127.0.0.1:6379> srandmember zls_fans
"mls"
## 改
## 將集合的元素存入另一個集合 SMOVE
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"mls"
3)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
127.0.0.1:6379> SMOVE hl_fans zls_fans mls
(
integer
) 1
127.0.0.1:6379> SMEMBERS hl_fans
1)
"hl"
2)
"xiaoxuesheng"
127.0.0.1:6379> SMEMBERS zls_fans
1)
"hl"
2)
"wyk"
3)
"mls"
## 刪
127.0.0.1:6379> spop hl_fans
"hl"
127.0.0.1:6379> SMEMBERS hl_fans
1)
"xiaoxuesheng"
## 刪除指定元素,返回刪除元素的個數
127.0.0.1:6379> srem zls_fans hl
(
integer
) 1
127.0.0.1:6379> SMEMBERS zls_fans
1)
"wyk"
2)
"mls"
127.0.0.1:6379> srem zls_fans xxx
(
integer
) 0
#### 無序的
127.0.0.1:6379> sadd zls_fans 111 222 333 444 555 666
(
integer
) 6
127.0.0.1:6379> SMEMBERS zls_fans
1)
"abc"
2)
"111"
3)
"mls"
4)
"123"
5)
"def"
6)
"333"
7)
"222"
8)
"456"
9)
"444"
10)
"555"
11)
"666"
12)
"wyk"
sorted-set(有序集合)
# 增
127.0.0.1:6379> zadd myzset 1
"one"
2
"two"
3
"three"
# 查
## 只查詢成員
127.0.0.1:6379> zrange chengji 0 -1
1)
"wyk"
2)
"hl"
3)
"zls"
## 查詢成員和分數
127.0.0.1:6379> zrange chengji 0 -1 WITHSCORES
1)
"wyk"
2)
"1"
3)
"hl"
4)
"38"
5)
"zls"
6)
"100"
## 查詢成員的索引
127.0.0.1:6379> zrank chengji wyk
(
integer
) 0
127.0.0.1:6379> zrank chengji zls
(
integer
) 2
## 查詢成員數量
127.0.0.1:6379> zcard chengji
(
integer
) 3
## 檢視分數是指定範圍的成員個數 30 <= score <=40
127.0.0.1:6379> zcount chengji 30 40
(
integer
) 1
127.0.0.1:6379> zcount chengji 30 101
(
integer
) 2
## 獲取指定成員分數
127.0.0.1:6379> ZSCORE chengji wyk
"1"
127.0.0.1:6379> ZSCORE chengji zls
"100"
## 檢視分數是指定範圍的成員名
127.0.0.1:6379> zcount chengji 30 40
(
integer
) 1
127.0.0.1:6379> zrangebyscore chengji 30 40
1)
"hl"
## +inf表示最後一個成員,-inf表示第一個成員,意思是:檢索所有資料,然後從下標為2的資料開始再往後輸出3:(N-1)個數據
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 3
1)
"hjx"
2)
"xwq"
3)
"zls"
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 2
1)
"hjx"
2)
"xwq"
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 1
1)
"hjx"
127.0.0.1:6379> zrangebyscore chengji -inf +inf
limit
2 4
1)
"hjx"
2)
"xwq"
3)
"zls"
# 刪
## 刪除指定分數範圍的成員,並返回刪除的個數
127.0.0.1:6379> zrangebyscore chengji 30 40
1)
"hl"
127.0.0.1:6379> zremrangebyscore chengji 30 40
(
integer
) 1
## 按指定索引範圍刪除,返回刪除個數
127.0.0.1:6379> zremrangebyrank chengji 0 2
(
integer
) 3
## 倒序排名
127.0.0.1:6379> zrevrange chengji 0 -1 WITHSCORES
1)
"zls"
2)
"100"
3)
"hjx"
4)
"60"
5)
"xwq"
6)
"50"
7)
"hl"
8)
"38"
9)
"wyk"
10)
"1"
## 按照分數段查詢成員,並倒序排序
127.0.0.1:6379> zrevrangebyscore chengji 101 30 WITHSCORES
1)
"zls"
2)
"100"
3)
"hjx"
4)
"60"
5)
"xwq"
6)
"50"
7)
"hl"
8)
"38"
## 按照分數查詢成員,並倒序排序,然後按照索引號,取出指定的資料
127.0.0.1:6379> zrevrangebyscore chengji 101 30 WITHSCORES
limit
1 2
1)
"hjx"
2)
"60"
3)
"xwq"
4)
"50"
127.0.0.1:6379> zrevrangebyscore chengji 101 30 WITHSCORES
limit
1 3
1)
"hjx"
2)
"60"
3)
"xwq"
4)
"50"
5)
"hl"
6)
"38"

文末福利

即將步入2025年,不少小夥伴在考慮來年的工作方向。
僅目前來說,傳統運維衝擊年薪30W+的轉型方向就是SRE&DevOps崗位。









