理解 Redis(8) - Ordered set 值
程序员文章站
2022-10-25 08:11:46
ordered set 是根据 score值有序排列的数据集合. 首先还是清空数据, 并清屏, 此步骤省略~~~~ 新建一条 ordered set 数据 myset1, 并存入4个字符串, score 的排列顺序为1-4: 查看这个数据: 给 myset1 的值里新加一个字符e, score=10 ......
ordered set 是根据 score值有序排列的数据集合.
首先还是清空数据, 并清屏, 此步骤省略~~~~
新建一条 ordered set 数据 myset1, 并存入4个字符串, score 的排列顺序为1-4:
127.0.0.1:6379> zadd myset1 1 a 2 b 3 c 4 d (integer) 4
查看这个数据:
127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "b" 3) "c" 4) "d"
给 myset1 的值里新加一个字符e, score=100:
127.0.0.1:6379> zadd myset1 100 e (integer) 1
查看最新的 myset1 的值:
127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e"
查看 myset1 的值里一共有几条数据:
127.0.0.1:6379> zcard myset1 (integer) 5
根据指定的 score 的范围查看 myset1 的值里一共有几条数据:
127.0.0.1:6379> zcount myset1 1 3 (integer) 3 127.0.0.1:6379> zcount myset1 1 5 (integer) 4 127.0.0.1:6379> zcount myset1 1 55 (integer) 4 127.0.0.1:6379> zcount myset1 1 101 (integer) 5 127.0.0.1:6379> zcount myset1 1 100 (integer) 5 127.0.0.1:6379> zcount myset1 1 99 (integer) 4
删除 myset1 值里的某个数据:
127.0.0.1:6379> zrem myset1 b (integer) 1 127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "c" 3) "d" 4) "e" 127.0.0.1:6379> zrem myset1 d (integer) 1 127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "c" 3) "e"
查看某个数据的索引值:
127.0.0.1:6379> zrank myset1 e (integer) 2 127.0.0.1:6379> zrank myset1 a (integer) 0
反排序后查询索引:
127.0.0.1:6379> zrevrank myset1 a (integer) 2 127.0.0.1:6379> zrevrank myset1 e (integer) 0
查询 score值, 没有的返回 nil:
127.0.0.1:6379> zscore myset1 e "100" 127.0.0.1:6379> zscore myset1 a "1" 127.0.0.1:6379> zscore myset1 jsjs (nil)
支持对同一个 score值设置多个不同的数据:
127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "c" 3) "e" 127.0.0.1:6379> zadd myset1 1 aaaa (integer) 1 127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "aaaa" 3) "c" 4) "e" 127.0.0.1:6379> zadd myset1 1 bbbb (integer) 1 127.0.0.1:6379> zrange myset1 0 -1 1) "a" 2) "aaaa" 3) "bbbb" 4) "c" 5) "e"
查询 score值, 再次证明 ordered set 支持对同一个 score值设置多个不同的数据:
127.0.0.1:6379> zscore myset1 a "1" 127.0.0.1:6379> zscore myset1 aaaa "1" 127.0.0.1:6379> zscore myset1 bbbb "1"
查询索引:
127.0.0.1:6379> zrank myset1 a (integer) 0 127.0.0.1:6379> zrank myset1 aaaa (integer) 1 127.0.0.1:6379> zrank myset1 bbbb (integer) 2
根据指定的 score值的范围查出数据:
127.0.0.1:6379> zrangebyscore myset1 1 2 1) "a" 2) "aaaa" 3) "bbbb" 127.0.0.1:6379> zrangebyscore myset1 1 4 1) "a" 2) "aaaa" 3) "bbbb" 4) "c"
以上, 就是关于 ordered set 的相关命令, enjoy~~~~
上一篇: 办公室瑜伽五个招式 有效缓解白领慢性病
下一篇: 男人做这些动作,,,竟能一辈子都不长白发