【Redis学习笔记】08 Redis 有序集合基本操作
2022-03-15 本文已影响0人
夏胖运维
1. 有序集合常用命令
命令 | 含义 | 用法 |
---|---|---|
ZADD | 创建有序集合 | ZADD key |
ZCARD | 查看有序集合成员格式 | ZCARD key |
SDIFF | 查看成员 | ZSCORE key member |
ZRANK | 按照降序查看成员名次 | ZRANK key member |
ZREVRANK | 按照升序查看成员名次 | ZREVRANK key member |
ZREM | 删除有序集合中成员 | ZREM key member [member ...] |
ZRANGE | 查看有序集合中指定范围集合 | ZRANGE key start stop [WITHSCORES] |
ZRANGEBYSCORE | 返回指定分数范围的成员 | ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] |
ZINCRBY | 增加成员分数 | ZINCRBY key increment member |
2. 添加有序集合
语法:``
ZADD class1:redis 100 zhangsan
ZADD class1:redis 20 lisi
ZADD class1:redis 99 wangwu
3. 计算成员个数
语法:ZCARD key
127.0.0.1:6379> ZCARD class1:redis
(integer) 3
4. 查看成员分数
语法:ZSCORE key member
127.0.0.1:6379> ZSCORE class1:redis lisi
"20"
5. 按照降序查看成员名次(从0开始)
语法:ZRANK key member
127.0.0.1:6379> ZRANK class1:redis lisi
(integer) 0
6. 按照升序查看成员名次(从0开始)
语法:ZREVRANK key member
127.0.0.1:6379> ZREVRANK class1:redis lisi
(integer) 2
7. 删除成员
语法:ZREM key member [member ...]
127.0.0.1:6379> ZREM class1:redis lisi
(integer) 1
8. 返回指定排名范围的成员
语法:ZRANGE key start stop [WITHSCORES]
127.0.0.1:6379> ZRANGE class1:redis 0 -1
1) "wangwu"
2) "zhangsan"
127.0.0.1:6379> ZRANGE class1:redis 0 -1 WITHSCORES
1) "wangwu"
2) "99"
3) "zhangsan"
4) "100"
9. 返回指定分数范围的成员
语法:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:6379> ZRANGEBYSCORE class1:redis 10 99
1) "wangwu"
127.0.0.1:6379> ZRANGEBYSCORE class1:redis 10 99 WITHSCORES
1) "wangwu"
2) "99"
10. 增加成员分数
语法:ZINCRBY key increment member
127.0.0.1:6379> ZINCRBY class1:redis 50 tom
"50"
127.0.0.1:6379> ZSCORE class1:redis tom
"50"