复习总结一下redis, 为之后的大规模分布式爬虫做准备
@
前戏
进入命令行模式
redis-cli
查询所有key
127.0.0.1:6379> KEYS *
1) "proxies:universal"
查询key 存储在 redis服务器中的类型,确认是有序集合ZSET类型
127.0.0.1:6379> TYPE proxies:universal
zset
插入
zadd
语法:zadd key score member [[score member] [score member] ...]
解释:增加一个或多个member (根据score排序)到有序集key中,如果member已经存在,只更新score
。返回增加member个数,不包含已经存在的member
127.0.0.1:6379> ZADD temp 100
127.0.0.1:6379> ZADD temp 10 haha
查询
zrange
语法:zrange key start stop [withscores]
解释:返回有序集key中指定范围(通过索引 start stop)的member[及其score]
127.0.0.1:6379> ZRANGE temp 0 -1 withscores # 类似于Python里的索引
1) "100"
2) "0"
3) "haha"
4) "10"
zcount
语法:zcount key min max
解释:统计key中score值介于min 和max之间的member个数
127.0.0.1:6379> ZCOUNT proxies:universal 90 100
(integer) 264
zscore
语法:zscore key member
解释:返回有集key中member的score
127.0.0.1:6379> ZSCORE proxies:universal 149.28.17.159:3128
"100"
zrevrange
语法:zrevrange key start stop [withscores]
解释:返回有序集key中指定范围[通过索引start stop]的member[及score],返回member根据score按降序排列
127.0.0.1:6379> ZREVRANGE proxies:universal 200 205 WITHSCORES
1) "110.243.24.21:9999"
2) "97"
3) "110.243.16.9:9999"
4) "97"
5) "110.243.15.222:9999"
6) "97"
7) "110.243.13.30:9999"
8) "97"
9) "110.243.1.53:9999"
10) "97"
11) "58.250.21.56:3128"
12) "96"
zrangebyscore
语法:zrangebyscore key min max [withscores] [limit offset count]
解释:返回有序集key中,score大于等于min并且小于等于max的member。返回结果按照score递增
的次序排列。可选withscores决定返回结果集中仅仅返回member还是返回member和score;可选参数limit 指定返回结果的数量区间
。
127.0.0.1:6379> ZRANGEBYSCORE proxies:universal 90 100 WITHSCORES LIMIT 1 5
1) "110.243.1.192:9999"
2) "91"
3) "110.243.1.41:9999"
4) "91"
5) "148.70.251.117:8133"
6) "91"
7) "182.46.203.184:9999"
8) "91"
9) "192.46.231.208:8080"
10) "91"
zrevrangebyscore
语法:zrevrangescore key max min [withscores] [limit offset count]
解释:返回有序集key中score<=max并且score>=min 的元素,返回结果根据score从大到小顺序排列。可选参数withscores决定结果集中是否包含score,可选参数limit 指定返回结果集范围。很明显是与zrangebyscore相反的函数
127.0.0.1:6379> ZREVRANGEBYSCORE proxies:universal 92 90 WITHSCORES LIMIT 1 5
1) "182.46.111.216:9999"
2) "92"
3) "171.35.212.72:9999"
4) "92"
5) "122.5.97.104:9999"
6) "92"
7) "118.212.106.139:9999"
8) "92"
9) "113.121.66.141:9999"
10) "92"
zrank
语法:zrank key member
解释:根据score从低到高
,返回member在有序集中的index, 和Python中的索引是一样的
127.0.0.1:6379> ZRANGE proxies:universal 0 5
1) "1.197.203.14:8460"
2) "103.153.40.13:8080"
3) "110.243.13.118:9999"
4) "110.243.2.248:9999"
5) "110.243.3.170:9999"
6) "110.243.3.241:9999"
127.0.0.1:6379> ZRANK proxies:universal 1.197.203.14:8460
(integer) 0
127.0.0.1:6379> ZRANK proxies:universal 110.243.3.241:9999
(integer) 5
zrevrank
语法:zrevrank key member
解释:根据score从高到低排序,返回member在有序集key中的index,很明显是上面zrank的反序
127.0.0.1:6379> ZREVRANK proxies:universal 1.197.203.14:8460
(integer) 3655
127.0.0.1:6379> ZREVRANK proxies:universal 110.243.3.241:9999
(integer) 3650
zcard
语法:zcard key
解释:返回有序集key的基数,类似于Python集合的长度
127.0.0.1:6379> ZCARD proxies:universal
(integer) 3656
跟新
zincrby
语法:zincrby key increment member
解释:有序集key的member增加增量increment,返回增加后的score
127.0.0.1:6379> ZSCORE proxies:universal 103.153.40.13:8080
"1"
127.0.0.1:6379> ZINCRBY proxies:universal 9 103.153.40.13:8080
"10"
127.0.0.1:6379> ZSCORE proxies:universal 103.153.40.13:8080
"10"
删除
zrem
语法:zrem key member [member ...]
解释:移除有序集中的一个或多个member,返回移除member的个数
127.0.0.1:6379> ZSCORE proxies:universal 1.197.203.14:8460
"1"
127.0.0.1:6379> ZREM proxies:universal 1.197.203.14:8460
(integer) 1
127.0.0.1:6379> ZSCORE proxies:universal 1.197.203.14:8460
(nil)
zremrangebyrank
语法:zremrangebyrank key start stop
解释:移除有序集中指定排名范围【start stop】的元素,返回移除元素个数
127.0.0.1:6379> ZRANGE proxies:universal 0 5 WITHSCORES
1) "103.153.40.13:8080"
2) "1"
3) "110.243.13.118:9999"
4) "1"
5) "110.243.2.248:9999"
6) "1"
7) "110.243.3.170:9999"
8) "1"
9) "110.243.3.241:9999"
10) "1"
11) "110.243.4.86:8232"
12) "1"
127.0.0.1:6379> ZREMRANGEBYRANK proxies:universal 0 5
(integer) 6
127.0.0.1:6379> ZRANGE proxies:universal 0 5 WITHSCORES
1) "110.243.5.104:9999"
2) "1"
3) "112.95.22.17:8820"
4) "1"
5) "113.121.36.100:8236"
6) "1"
7) "113.121.36.100:8772"
8) "1"
9) "113.121.36.100:8856"
10) "1"
11) "113.121.73.60:9999"
12) "1"
zremrangebyscore
语法:zremrangebyscore key min max
解释:移除有序集中的member,移除member的score大于等于min小于等于max;返回移除元素个数
127.0.0.1:6379> ZRANGEBYSCORE proxies:universal 1 5 WITHSCORES LIMIT 1 5
1) "112.95.22.17:8820"
2) "1"
3) "113.121.36.100:8236"
4) "1"
5) "113.121.36.100:8772"
6) "1"
7) "113.121.36.100:8856"
8) "1"
9) "113.121.73.60:9999"
10) "1"
127.0.0.1:6379> ZREMRANGEBYSCORE proxies:universal 1 5
(integer) 1450
其他
zinterstore
语法:zinterstore destination numkeys key[key...] [weights weight] [aggregate sum|min|max]
解释:统计多个有序集的交集
zunionstore
语法:zunionstore destination numkeys key[key...] [weights weight] [aggregate sum|min|max]
解释:统计多个有序集的并集