redis: 其他数据类型(八)

1、geospatial 地理位置

有效的经度从-180度到180度

有效的纬度从-85.05112878度到85.05112878度

当坐标位置超出上述指定范围时,该命令将会返回一个错误

底层实现原理是Zset

设置成员的经纬度:geoadd china:city 116.408 39.904 beijing

127.0.0.1:6379> geoadd china:city 116.408 39.904 beijing #精度 维度 北京
(integer) 1
127.0.0.1:6379> geoadd china:city 121.445 31.213 shanghai #精度 维度 上海
(integer) 1
127.0.0.1:6379> geoadd china:city 117.246 39.117 tianjin #精度 维度 天津
(integer) 1
127.0.0.1:6379> geoadd china:city 117.009 36.663 jinan #精度 维度 济南
(integer) 1

获取指定成员的经纬度:geopos china:city beijing

127.0.0.1:6379> geopos china:city beijing #获取北京经纬度
1) 1) "116.40800267457962036"
   2) "39.90399988166036138"
127.0.0.1:6379> geopos china:city chongqing #获取重庆经纬度 不存在返回null
1) (nil)

计算两个成员之间的距离(默认为米):geodist china:city beijing shanghai

127.0.0.1:6379> geodist china:city beijing shanghai #北京上海直线距离 米
"1068232.0171"
127.0.0.1:6379> geodist china:city beijing shanghai km #北京上海直线距离 千米
"1068.2320"

获取附近的人(经纬度):georadius china:city 116.408 39.904 300 km withdist
当前精度 维度 半径距离 单位

127.0.0.1:6379> georadius china:city 116.408 39.904 300 km withdist
1) 1) "tianjin"
   2) "113.2839"
2) 1) "beijing"
   2) "0.0002"

获取附近的人(经纬度指定数量):georadius china:city 116.408 39.904 300 km withdist count 1

127.0.0.1:6379> georadius china:city 116.408 39.904 300 km withdist count 1
1) 1) "beijing"
   2) "0.0002"

获取附近的人(成员):georadiusbymember china:city beijing 300 km withdist
当前精度 维度 半径距离 单位

127.0.0.1:6379> georadiusbymember china:city beijing 300 km withdist
1) 1) "tianjin"
   2) "113.2837"
2) 1) "beijing"
   2) "0.0000"

获取附近的人(成员指定数量):georadiusbymember china:city beijing 300 km withdist count 1

127.0.0.1:6379> georadiusbymember china:city beijing 300 km withdist count 1
1) 1) "beijing"
   2) "0.0000"

查看所有的成员:zrange china:city 0 -1

127.0.0.1:6379> zrange china:city 0 -1
1) "shanghai"
2) "jinan"
3) "tianjin"
4) "beijing"

删除指定成员:zrem china:city jinan

127.0.0.1:6379> zrem china:city jinan #删除济南
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1 
1) "shanghai"
2) "tianjin"
3) "beijing"

2、hyperloglogs 基数统计

基数:不重复的元素

A{1、3、5、7、7、9}
不记录重复的元素 = 5{1、3、5、7、9}
比如:统计网站的UV(独立访客)
传统方式可以用set集合 但比较浪费空间 而且相对比较麻烦

存值:pfadd view a a b c d e f g h i j
统计数量:pfcount view

127.0.0.1:6379> pfadd view a a b c d e f g h i j #存入11个值 其中a是重复的
(integer) 1
127.0.0.1:6379> pfcount view #返回10 不统计重复的值
(integer) 10 

不重复并集:pfmerge otherview view view2

127.0.0.1:6379> pfadd view a a b c d e f g h i j 
(integer) 1
127.0.0.1:6379> pfcount view #view中有10个
(integer) 10
127.0.0.1:6379> pfadd view2 h i j k
(integer) 1
127.0.0.1:6379> pfcount view2 #view2中有4个 但h i j与view中是重复的
(integer) 4
127.0.0.1:6379> pfmerge otherview view view2 #把view和view2合并到otherview 
OK
127.0.0.1:6379> pfcount otherview #11个 已去掉重复的
(integer) 11

3、Bitmap 位图

二进制 只有0和1两个值 比如统计活跃 不活跃用户 登录 未登录用户 打卡状态等

存值:setbit sign 20200406 1

127.0.0.1:6379> setbit sign 20200406 1 #4月6日已打卡
(integer) 0
127.0.0.1:6379> setbit sign 20200407 0 #4月7日未打卡
(integer) 0
127.0.0.1:6379> setbit sign 20200408 0 #4月8日未打卡
(integer) 0

查看某一天打卡状态:getbit sign 20200407

127.0.0.1:6379> getbit sign 20200407
(integer) 0
127.0.0.1:6379> getbit sign 20200406
(integer) 1

统计打卡天数(只能统计为1的):**bitcount ** sign

127.0.0.1:6379> bitcount sign

猜你喜欢

转载自www.cnblogs.com/applesnt/p/12642319.html