레디스의 z계열 커맨드가 어떻게 동작하는지 까먹어서 공부를 했다.
127.0.0.1:6379> zadd number 1000 samuel
(integer) 1
127.0.0.1:6379> zadd number 2000 matt
(integer) 1
127.0.0.1:6379> zadd number 3000 jonathan
(integer) 1
zscore는 스코어를 본다.
127.0.0.1:6379> zscore number jonathan
"3000"
127.0.0.1:6379> zscore number samuel
"1000"
zrange 커맨드 (-1 매개 변수는 마지막)
127.0.0.1:6379> zrange number 0 100
1) "samuel"
2) "matt"
3) "jonathan"
127.0.0.1:6379> zrange number 0 -1
1) "samuel"
2) "matt"
3) "jonathan"
127.0.0.1:6379> zrange number 0 1
1) "samuel"
2) "matt"
점수도 함께 표현 (withscores)
127.0.0.1:6379> zrange number 0 -1 withscores
1) "samuel"
2) "1000"
3) "matt"
4) "2000"
5) "jonathan"
6) "3000"
zrem은 삭제
127.0.0.1:6379> zrem number "matt"
(integer) 1
127.0.0.1:6379> zrange number 0 -1 withscores
1) "samuel"
2) "1000"
3) "jonathan"
4) "3000"
zrangebyscore 스코어로 범위 지정하는 커맨드
127.0.0.1:6379> zrangebyscore number 3000 4000
1) "jonathan"
zounct는 score 범위로 몇개가 있는 계산하는 커맨드 (-inf:음의 무한대, +inf: 양의 무한대)
127.0.0.1:6379> zrange number 0 -1
1) "samuel"
2) "jonathan"
127.0.0.1:6379> zcount number -inf +inf
(integer) 2
127.0.0.1:6379> zcount number 0 4000
(integer) 2
127.0.0.1:6379> zcount number -inf 1000
(integer) 1
zrangebylex는 member로 범위 지정
127.0.0.1:6379> zadd number 5000 jack
(integer) 1
127.0.0.1:6379> zadd number 6000 soa
(integer) 1
127.0.0.1:6379> zrangebylex number - +
1) "samuel"
2) "jonathan"
3) "jack"
4) "soa"
127.0.0.1:6379> zrangebylex number "[soa" +
1) "soa"
zlexcount는 zrangebylex와 같이 개수 구하는 커맨드
127.0.0.1:6379> zlexcount number - +
(integer) 4
127.0.0.1:6379> zlexcount number "[soa" +
(integer) 1
zrangebyscore는 스코어를 기반으로 값을 검색한다.
127.0.0.1:6379> zrangebyscore number -inf inf
1) "samuel"
2) "jonathan"
3) "jack"
4) "soa"
127.0.0.1:6379> zrangebyscore number -inf inf withscores
1) "samuel"
2) "1000"
3) "jonathan"
4) "3000"
5) "jack"
6) "5000"
7) "soa"
8) "6000"
127.0.0.1:6379> zrangebyscore number -inf 5000 withscores
1) "samuel"
2) "1000"
3) "jonathan"
4) "3000"
5) "jack"
6) "5000"
127.0.0.1:6379> zrangebyscore number -inf (4000 withscores
1) "samuel"
2) "1000"
3) "jonathan"
4) "3000"
127.0.0.1:6379> zrangebyscore number 100 3000 withscores
1) "samuel"
2) "1000"
3) "jonathan"
4) "3000"
zremrnagebyrank는 스코어 단위로 삭제한다.
127.0.0.1:6379> zrangebyscore number -inf inf withscores
1) "samuel"
2) "1000"
3) "jonathan"
4) "3000"
5) "jack"
6) "5000"
7) "soa"
127.0.0.1:6379> zremrangebyrank number 1 1
(integer) 1
127.0.0.1:6379> zrange number 0 -1
1) "samuel"
2) "jack"
3) "soa"
zremrangebylex는 zrangebylex 기반과 비슷하며 삭제한다.
127.0.0.1:6379> zrangebylex number - +
1) "samuel"
2) "jack"
3) "soa"
127.0.0.1:6379> zrangebylex number "(jack" "(soa"
1) "samuel"
2) "jack"
127.0.0.1:6379> zremrangebylex number "(jack" "(soa"
(integer) 2
127.0.0.1:6379> zrangebylex number - +
1) "soa"
다시 재구성한 한 후.. zremrangebylex 3 -1 는 3개 빼고 다 삭제한다. zremrangebylex 1 -1 는 1개 빼고 다 삭제한다.
127.0.0.1:6379> zrevrange number 0 -1 withscores
1) "soa"
2) "6000"
3) "jonathan"
4) "3000"
5) "matt"
6) "2000"
7) "samuel"
8) "1000"
127.0.0.1:6379> zremrangebyrank number 3 -1
(integer) 1
127.0.0.1:6379> zrevrange number 0 -1 withscores
1) "jonathan"
2) "3000"
3) "matt"
4) "2000"
5) "samuel"
6) "1000"
127.0.0.1:6379> zremrangebyrank number 1 -1
(integer) 2
127.0.0.1:6379> zrevrange number 0 -1 withscores
1) "samuel"
2) "1000"
일부러 음수로 저장하면 또 다른 의미로 rank로 최신 데이터만 가져올 수 있다.
127.0.0.1:6379> zadd feed -900 1st
(integer) 1
127.0.0.1:6379> zadd feed -950 2nd
(integer) 1
127.0.0.1:6379> zadd feed -955 3rd
(integer) 1
127.0.0.1:6379> zrevrange feed 0 -1 withscores
1) "1st"
2) "-900"
3) "2nd"
4) "-950"
5) "3rd"
6) "-955"
127.0.0.1:6379> zremrangebyrank feed 2 -1
(integer) 1
127.0.0.1:6379> zrevrange feed 0 -1 withscores
1) "2nd"
2) "-950"
3) "3rd"
4) "-955"
zrank는 오름차순 순서를 보여준다.
127.0.0.1:6379> zrange number 0 -1 withscores
1) "samuel"
2) "1000"
3) "matt"
4) "2000"
5) "jonathan"
6) "3000"
7) "soa"
8) "6000"
127.0.0.1:6379> zrank number "jonathan"
(integer) 2
127.0.0.1:6379> zrank number "soa"
(integer) 3
점수 높은것부터 보려면 zrevrank를 호출한다.
127.0.0.1:6379> zrevrange number 0 -1 withscores
1) "soa"
2) "6000"
3) "jonathan"
4) "3000"
5) "matt"
6) "2000"
7) "samuel"
8) "1000"
127.0.0.1:6379> zrevrank number "soa"
(integer) 0
127.0.0.1:6379> zrevrank number "samuel"
(integer) 3