Redis restudy 4 Zset Geospatial Hyperloglog Bitmaps
uwupu 啦啦啦啦啦

Zset(有序集合)

与set基本一致,在zset里,元素是有序的;

对于每一个元素,都有一个score,来表现其排序先后。

zadd, zrange

zadd key score member [score member ...]:score为优先级,会影响排序顺序。

zrange key start stop:查询索引为start-stop的所有值

1
2
3
4
5
6
7
8
127.0.0.1:6379> zadd z1 1 one  # 添加值
(integer) 1
127.0.0.1:6379> zadd z1 3 three 2 two #添加多个值
(integer) 2
127.0.0.1:6379> zrange z1 0 -1 #查询范围
1) "one"
2) "two"
3) "three"

zrangebyscore, zrevrange

zrangebyscore key min max [WITHSCORES] [LIMIT offset count]:排序查询set内值,升序排序

  • min,max:最小值,最大值
    • 可以为 -inf +inf,无穷小,无穷大
  • WITHSCORES:返回结果包含score
  • LIMIT offset count:获取从指定位置开始的指定数量。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1:6379> zadd salary 2500 ZhangSan
(integer) 1
127.0.0.1:6379> zadd salary 5000 Lisi
(integer) 1
127.0.0.1:6379> zadd salary 1234 yn
(integer) 1
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores # 查询salary在-inf到+inf之间的值 并返回score
1) "yn"
2) "1234"
3) "ZhangSan"
4) "2500"
5) "Lisi"
6) "5000"
127.0.0.1:6379> zrangebyscore salary -inf 2600 withscores # 查询salary在-inf到2600之间的值 并返回score
1) "yn"
2) "1234"
3) "ZhangSan"
4) "2500"

zrevrange key start stop [WITHSCORES]:查询有序集合中,降序排序之后,元素索引在start-stop之间的值。

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> ZREVRANGE salary 0 -1 withscores
1) "Lisi"
2) "5000"
3) "ZhangSan"
4) "2500"
5) "yn"
6) "1234"
127.0.0.1:6379> ZREVRANGE salary 1 -1 withscores
1) "ZhangSan"
2) "2500"
3) "yn"
4) "1234"

zrem

zrem salary value:移除有序集合的指定元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379> zrange salary 0 -1 withscores
1) "yn"
2) "1234"
3) "ZhangSan"
4) "2500"
5) "Lisi"
6) "5000"
127.0.0.1:6379> zrem salary yn
(integer) 1
127.0.0.1:6379> zrange salary 0 -1 withscores
1) "ZhangSan"
2) "2500"
3) "Lisi"
4) "5000"

zcard

zcard key:获取有序集合元素个数

1
2
127.0.0.1:6379> zcard salary
(integer) 2

zcount

zcount key min max:获取score在min-max之间的值的数量。

1
2
3
4
127.0.0.1:6379> zcount salary 2400 2600
(integer) 1
127.0.0.1:6379> zcount salary 2400 6000
(integer) 2

应用场景

班级成绩表,工资表排序

带权重消息列表

排行榜

geospatial 地理位置

Redis 的 Geo

特性:两级无法直接添加

可以进行地理位置信息的计算,如两地之间的距离,方圆几里的人

有效经度:-180~+180

有效纬度:-85.05112878~+85.05112878

geoadd, geodist, geohash, geopos, georaduis, georadiusbymember

geo的底层实现是zset,可以使用zset的方式修改geo的元素。

geoadd

geoadd key longitude latitude member [longitude latitude member ...]:添加地理位置

  • longitude,latitude:经度,纬度
  • member:成员
1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> geoadd china:city 116.40 39.90 BeiJing
(integer) 1
127.0.0.1:6379> geoadd china:city 121.47 31.23 ShangHai
(integer) 1
127.0.0.1:6379> geoadd china:city 106.50 29.53 ChongQing 114.05 22.52 ShenZhen
(integer) 2
127.0.0.1:6379> geoadd china:city 120.15 30.28 ZheJiang
(integer) 1
127.0.0.1:6379> geoadd china:city 108.96 34.26 XiAn
(integer) 1

geopos

geopos key member [member ...]:查询值

1
2
3
4
5
127.0.0.1:6379> geopos china:city BeiJing ChongQing
1) 1) "116.39999896287918091"
2) "39.90000009167092543"
2) 1) "106.49999767541885376"
2) "29.52999957900659211"

geodist

geodist key member1 member2 [m|km|ft|mi] 获取两地之间距离

  • m 米;km千米;ft英尺;mi英里
1
2
3
4
127.0.0.1:6379> geodist china:city BeiJing ChongQing km  #BeiJing到ChongQing距离
"1464.0708"
127.0.0.1:6379> geodist china:city ShangHai ShenZhen km
"1215.9224"

georadius

在指定的经度和纬度对应位置上,查询指定半径上的成员

georadius key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]

  • radius,半径
  • WITHCOORD:包含经度纬度
  • WITHDIST:包含距离
  • WITHHASH:包含hash值
  • COUNT count:数量
1
2
3
4
5
127.0.0.1:6379> georadius china:city 110 30 1000 km #获取以经纬度为110,30位置为中心,查询1000km附近的城市
1) "ChongQing"
2) "XiAn"
3) "ShenZhen"
4) "ZheJiang"
1
2
3
4
5
6
127.0.0.1:6379> georadius china:city 110 30 500 km WITHCOORD WITHDIST WITHHASH count 1 #获取500km,返回包含经纬度,距离和Hash,数量指定1.
1) 1) "ChongQing"
2) "341.9374"
3) (integer) 4026042091628984
4) 1) "106.49999767541885376"
2) "29.52999957900659211"

georadiusbymember

georadiusbymember key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]

以member为中心,查询指定半径范围内的其他member。

1
2
3
127.0.0.1:6379> georadiusbymember china:city ChongQing 1000 km
1) "ChongQing"
2) "XiAn"

geohash

geohash key member [member ...]:查询member的geohash。

1
2
3
127.0.0.1:6379> geohash china:city ChongQing BeiJing
1) "wm5xzrybty0" #11位
2) "wx4fbxxfke0"

将二维的经纬度转换为一维的字符串。

zset相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> zrange china:city 0 -1
1) "ChongQing"
2) "XiAn"
3) "ShenZhen"
4) "ZheJiang"
5) "ShangHai"
6) "BeiJing"
127.0.0.1:6379> zrem china:city BeiJing
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "ChongQing"
2) "XiAn"
3) "ShenZhen"
4) "ZheJiang"
5) "ShangHai"

Hyperloglog基数统计

基数

不重复的元素数量

应用场景

不重复用户访问量

为什么使用Hyperloglog

网站UV:一个用户访问算作一个访问量,UV指的是访问过的人数;

在传统的实现方式中,使用set集合存放用户的id,然后以set集合的元素数量作为判断。

但是,这个方法不好!因为这个功能的目的是为了实现计数,而不是保存用户ID;

基数统计,统计的结果不一定准确,但不需要保存用户ID。

优点

  • 占用内存固定
    • 如2^63不同的元素,这里只需要12KB内存

特性

  • 误差0.81%
    • 若统计一些允许这样误差的信息,则是可以的。
  • 最大数量2^63,占用内容12KB

使用

pfadd key element [element ...]:向Hyperloglog的key中添加元素。

pfcount key:查询数量

1
2
3
4
5
6
7
8
127.0.0.1:6379> pfadd p1 a b c d e f g h i j k l m n
(integer) 1
127.0.0.1:6379> pfcount p1
(integer) 14
127.0.0.1:6379> pfadd p1 a b c d e o p q
(integer) 1
127.0.0.1:6379> pfcount p1
(integer) 17

pfmerge destkey sourcekey [sourcekey ...]:合并两个基数统计集合

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> pfadd p1 a b c d
(integer) 1
127.0.0.1:6379> pfadd p2 c d e f
(integer) 1
127.0.0.1:6379> pfmerge p3 p1 p2 # 合并p1和p2,存放到p3
OK
127.0.0.1:6379> pfcount p1
(integer) 4
127.0.0.1:6379> pfcount p2
(integer) 4
127.0.0.1:6379> pfcount p3
(integer) 6

Bitmaps

位存储

操作二进制位记录,只有0和1两个状态。

使用

setbit key offset value:设置key的offset位置的值为value;

  • value只可以是0或1;
1
2
3
4
5
6
127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 0
(integer) 0
127.0.0.1:6379> setbit sign 2 1
(integer) 0

getbit key offset:获取key的offset位置的值;

1
2
3
4
5
6
127.0.0.1:6379> getbit sign 0
(integer) 1
127.0.0.1:6379> getbit sign 1
(integer) 0
127.0.0.1:6379> getbit sign 2
(integer) 1

bitcount key [start end]:查询位为start-end中1的数量

1
2
127.0.0.1:6379> bitcount sign
(integer) 2

应用场景

统计打卡天数;

  • 设sign为 010,表示 周一没打卡,周二打卡了,周三没打卡
  • bitcount sign:统计打卡天数。
 评论