Redis三种特殊类型

geospatial地理位置

添加地理数据

  • 有效的经度从-180度到180度。
  • 有效的纬度从-85.05112878度到85.05112878度。
1
2
3
4
5
6
7
8
9
# 可通过java程序一次性道路
127.0.0.1:6379> geoadd china:city 113.16 30.65 tianmen
(integer) 1
127.0.0.1:6379> geoadd china:city 112.19 30.35 jinzhou
(integer) 1
127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing
(integer) 1
127.0.0.1:6379> geoadd china:city 112.19 31.23 shanghai
(integer) 1

获取指定的经纬度

1
2
3
127.0.0.1:6379> geopos china:city beijing
1) 1) "116.39999896287918"
2) "39.900000091670925"

返回两个给定位置之间的距离

1
geodist china:city beijing shanghai km # 两地的直线距离

以给定的经纬度为中心, 找出某一半径内的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "jinzhou"
2) "shanghai"
3) "tianmen"
georadius china:city 110 30 500 km withdist
1) 1) "jinzhou"
2) "214.1425"
2) 1) "shanghai"
2) "250.3140"
3) 1) "tianmen"
2) "311.8670"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord
1) 1) "jinzhou"
2) 1) "112.18999832868576"
2) "30.349999617100856"
2) 1) "shanghai"
2) 1) "112.18999832868576"
2) "31.229999039757836"
3) 1) "tianmen"
2) 1) "113.15999776124954"
2) "30.649999074635581"
127.0.0.1:6379> georadius china:city 110 30 1000 km count 2
1) "jinzhou"
2) "shanghai"

底层是zset,可以使用zset的一些操作

1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6379> zrange china:city 0 -1
1) "jinzhou"
2) "shanghai"
3) "tianmen"
4) "beijing"
127.0.0.1:6379> zrem china:city tianmen
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "jinzhou"
2) "shanghai"
3) "beijing"

hyperloglog

主要用于基数统计

1
2
3
4
pfadd mykey q w e r t y
(integer) 1
127.0.0.1:6379> pfcount mykey
(integer) 6

Bitmap

位存储,一般用于用户是否登录,统计用户是否活跃,统计打卡

使用bitmap记录一周的打卡:周一:1 周二:0 ……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 1
(integer) 0
127.0.0.1:6379> setbit sign 5 1
(integer) 0
127.0.0.1:6379> setbit sign 6 0
(integer) 0

查看是否打卡

1
2
127.0.0.1:6379> getbit sign 0 # 查看星期一是否打卡
(integer) 1

统计打卡天数

1
2
127.0.0.1:6379> bitcount sign  # 统计一周的打卡次数
(integer) 4