Redis(三)

Redis(三)

本文分为以下部分:

geospatial 地理位置

hyperloglog 计算基数

bitmap 比特

三种特殊类型

geospatial 地理位置

geoadd 添加地理位置

127.0.0.1:6379> FLUSHDB
OK
#常用格式
#geoadd key 经度 维度 名称
127.0.0.1:6379> GEOADD city 115.8921 28.6764 nanchang
(integer) 1
127.0.0.1:6379> geoadd city 121.4726 31.2317 shanghai
(integer) 1
127.0.0.1:6379> geoadd city 116.4052 39.9049 beijing 117.1901 39.1255 tianjin 112.9822 28.1940 changsha
(integer) 3

zrange、zrem

底层为zset,所以同样用 zrange 查看,zrem 来删除

127.0.0.1:6379> geoadd city 10 10 test
(integer) 1
127.0.0.1:6379> zrange city 0 -1
1) "test"
2) "changsha"
3) "nanchang"
4) "shanghai"
5) "tianjin"
6) "beijing"
127.0.0.1:6379> zrem city test
(integer) 1
127.0.0.1:6379> zrange city 0 -1
1) "changsha"
2) "nanchang"
3) "shanghai"
4) "tianjin"
5) "beijing"

geopos 获取城市经纬度

127.0.0.1:6379> GEOPOS city beijing
1) 1) "116.40520244836807251"
   2) "39.90489970767192318"
127.0.0.1:6379> GEOPOS city beijing tianjin
1) 1) "116.40520244836807251"
   2) "39.90489970767192318"
2) 1) "117.19010263681411743"
   2) "39.12550083310794236"

geodist 获取城市之间距离

默认单位为m

  • m 米
  • km 千米
  • ft 英尺
  • mi 英里
#geodist key member1 member2 单位
127.0.0.1:6379> geodist city tianjin beijing km
"109.7760"

georadius 中心点多少以内的城市

6.2.0 后弃用,采用 geosearch

#georadius key 经度 纬度 距离 单位
127.0.0.1:6379> GEORADIUS city 120 40 500 km
1) "tianjin"
2) "beijing"
127.0.0.1:6379> GEORADIUS city 120 40 200 km
(empty array)
127.0.0.1:6379> GEORADIUS city 120 40 300 km
1) "tianjin"

geosearch

#GEOSEARCH key [FROMMEMBER 成员] [FROMLONLAT 中心点经度 中心点纬度] [BYRADIUS 半径 单位] [BYBOX 宽 高 单位] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH]
#以 120 40 为经纬度,查询 500km 以内的城市
127.0.0.1:6379> geosearch city fromlonlat 120 40 byradius 500 km
1) "tianjin"
2) "beijing"
#以 120 40 为经纬度,查询 500km 以内的城市 带直线距离 withdist 和 经纬度 withcoord
127.0.0.1:6379> geosearch city fromlonlat 120 40 byradius 500 km withdist withcoord
1) 1) "tianjin"
   2) "259.8183"
   3) 1) "117.19010263681411743"
      2) "39.12550083310794236"
2) 1) "beijing"
   2) "306.6670"
   3) 1) "116.40520244836807251"
      2) "39.90489970767192318"
#以 120 40 为经纬度,查询 500km 以内的城市 带直线距离 withdist 和 经纬度 withcoord 倒序排序第一个 count 1
127.0.0.1:6379> geosearch city fromlonlat 120 40 byradius 500 km withdist withcoord desc count 1
1) 1) "beijing"
   2) "306.6670"
   3) 1) "116.40520244836807251"
      2) "39.90489970767192318"
#以北京为中心,city中的member
127.0.0.1:6379> geosearch city frommember beijing  byradius 500 km withdist withcoord desc count 1
1) 1) "tianjin"
   2) "109.7760"
   3) 1) "117.19010263681411743"
      2) "39.12550083310794236"

hyperloglog 计算基数

基数:不重复的元素

pfadd 添加元素

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> pfadd hyper 1 2 3 4 5 5 6 6 
(integer) 1

pfcount 计算基数

127.0.0.1:6379> pfcount hyper
(integer) 6

pfmerge 合并

#pfmerge 目标key 源key
127.0.0.1:6379> pfadd hyper1 6 6 7 8 9 10
(integer) 1
127.0.0.1:6379> pfcount hyper1
(integer) 5
127.0.0.1:6379> pfmerge hyper1 hyper
OK
127.0.0.1:6379> pfcount hyper1
(integer) 10
127.0.0.1:6379> pfcount hyper
(integer) 6

bitmap 比特

setbit 设置值

#setbit key offset 0|1
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> setbit sign 1 1
(integer) 0
127.0.0.1:6379> setbit sign 2 1
(integer) 0
127.0.0.1:6379> setbit sign 3 0
(integer) 0
127.0.0.1:6379> setbit sign 4 1
(integer) 0
127.0.0.1:6379> setbit sign 5 1
(integer) 0

getbit 获取值

#getbit key offset
127.0.0.1:6379> getbit sign 1
(integer) 1

bitcount 获取为1的个数

#bitcount key [start end]
127.0.0.1:6379> bitcount sign
(integer) 4
posted @ 2021-07-20 17:35  抱糖果彡  阅读(31)  评论(0编辑  收藏  举报