1. 程式人生 > 其它 >Redis從0到精通--Zset

Redis從0到精通--Zset

Zset(有序集合)

在set的基礎上,增加了一個值,set k1 v1 zset k1 score1 v1
127.0.0.1:6379> hvals myhash # 只獲得所有value
1) "world"
2) "hello"
##########################################################################
incr decr
127.0.0.1:6379> hset myhash field3 5 #指定增量!
(integer) 1
127.0.0.1:6379> HINCRBY myhash field3 1
(integer) 6
127.0.0.1:6379> HINCRBY myhash field3 -1
(integer) 5
127.0.0.1:6379> hsetnx myhash field4 hello # 如果不存在則可以設定
(integer) 1
127.0.0.1:6379> hsetnx myhash field4 world # 如果存在則不能設定
(integer) 0
127.0.0.1:6379> zadd myset 1 one # 新增一個值
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three # 新增多個值
(integer) 2
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"
##########################################################################
排序如何實現
127.0.0.1:6379> zadd salary 2500 xiaohong # 新增三個使用者
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 500 kaungshen
(integer) 1
# ZRANGEBYSCORE key min max
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf # 顯示全部的使用者 從小到大!
1) "kaungshen"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZREVRANGE salary 0 -1 # 從大到進行排序!
1) "zhangsan"
2) "kaungshen"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores # 顯示全部的使用者並且附帶成
1) "kaungshen"
2) "500"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores # 顯示工資小於2500員工的升
序排序!
1) "kaungshen"
2) "500"
3) "xiaohong"
4) "2500"
##########################################################################
# 移除rem中的元素
127.0.0.1:6379> zrange salary 0 -1
1) "kaungshen"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> zrem salary xiaohong # 移除有序集合中的指定元素
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "kaungshen"
2) "zhangsan"
127.0.0.1:6379> zcard salary # 獲取有序集合中的個數
(integer) 2
##########################################################################
127.0.0.1:6379> zadd myset 1 hello
(integer) 1
127.0.0.1:6379> zadd myset 2 world 3 kuangshen
(integer) 2
127.0.0.1:6379> zcount myset 1 3 # 獲取指定區間的成員數量!
(integer) 3
127.0.0.1:6379> zcount myset 1 2
(integer) 2

其與的一些API,通過我們的學習嗎,你們剩下的如果工作中有需要,這個時候你可以去查檢視官方文 檔! 案例思路:set 排序 儲存班級成績表,工資表排序! 普通訊息,1, 重要訊息 2,帶權重進行判斷! 排行榜應用實現,取Top N 測試!