redis GEO数据结构、实现附近商铺功能

目录

一、GEO

Grolocation意为地理坐标,允许存储地理坐标信息,完成地理坐标之间距离的计算。

  • geoadd key longitude1 latitude1 field1 longitude2 latitude2 field2...:向图key中添加多个member,包含经度longitude、纬度latitude 、member唯一标识field
  • geodist key field1 field2 [m,km]:返回两个member之间的距离
  • geohash key field1:将指定member的坐标转为hash字符串形式并返回
  • geopos key field1:返回指定member的坐标
  • geoseartch key FROMLONLAT longitude1 latitude1 [BYRADIUS 10km] [ASC/DESC] [WITHDIST]:给定圆心和半径,按照升序或降序返回指定范围内的member,并返回距离具体值WITHDIST
  • geosearchstore:同geoseratch,并将结果存储到指定的key中

GEO底层是用sortedSet实现,其中经纬度会被换算为score字段(score=a+b的意思),member唯一标识作为value字段。

二、redis实现查询附近商铺功能:

1.按照商铺类型导入商铺信息:

java 复制代码
@Resource
StringRedisTemplate stringRedisTemplate;
@Resource
IShopService shopService;
@Test
public void testLoadShopData(){
    List<Shop> shops = shopService.query().list();
    for (Shop shop : shops) {
        stringRedisTemplate.opsForGeo().add(
                "geo:shop:"+shop.getTypeId(),
                new RedisGeoCommands.GeoLocation<>(
                        shop.getId().toString(),
                        new Point(shop.getX().doubleValue(),shop.getY().doubleValue())));
    }

2.根据商铺类型和距离查询附近商铺:

java 复制代码
@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
    if (x==null || y==null){
        // 根据类型分页查询
        Page<Shop> page = query()
                .eq("type_id", typeId)
                .page(new Page<>(current, 5));
        // 返回数据
        return Result.ok(page.getRecords());
    }
    // 该页起始和终止元素
    int from = (current-1)*5;
    int end = from + 5;
    // 查询距离内的店铺
    GeoResults<RedisGeoCommands.GeoLocation<String>> radius = stringRedisTemplate.opsForGeo().radius(
            "geo:shop:" + typeId,
            new Circle(new Point(x, y), new Distance(5000)),
            RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance());//指定圆心半径m查相关店铺,并返回店铺距离圆心的距离
    // 判空
    if (radius==null){
        return Result.ok(Collections.emptyList());
    }
    // 这里看类的结构只能getContent
    List<GeoResult<RedisGeoCommands.GeoLocation<String>>> results = radius.getContent();

    // 判该页是否有元素
    if (results.size()<=from){
        return Result.ok(Collections.emptyList());
    }

    // 截取from~end部分实现分页
    List<GeoResult<RedisGeoCommands.GeoLocation<String>>> results1 = results.subList(from, Math.min(end, results.size()-1));

    ArrayList<Long> shopIds = new ArrayList<>();
    HashMap<Long, Distance> longDistanceHashMap = new HashMap<>();
    results1.forEach(r ->{
        String shopId = r.getContent().getName();
        shopIds.add(Long.valueOf(shopId));
        Distance distance = r.getDistance();
        longDistanceHashMap.put(Long.valueOf(shopId), distance);
    });

    List<Shop> shops = listByIds(shopIds);
    for (Shop s : shops) {
        double value = longDistanceHashMap.get(s.getId()).getValue();
        s.setDistance(value);
    }

    return Result.ok(shops);
}
相关推荐
睡不醒男孩0308234 小时前
第二篇:深入探索开源数据库高可用:构建基于CLup的PostgreSQL生产级高可用与读写分离架构
数据库·postgresql·开源·clup
Micro麦可乐6 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
码农阿豪6 小时前
从零到一:Spring Boot快速接入金仓数据库实战
数据库·spring boot·后端
鼎讯信通7 小时前
风电光缆运维提质增效:G-4000A 光缆故障追踪仪破解风场巡检难题
运维·网络·数据库
三十..7 小时前
MySQL 从入门到高可用架构实战精要
运维·数据库·mysql
cfm_29148 小时前
Redis五大基本数据结构底层了解
数据结构·数据库·redis
如竟没有火炬8 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
真实的菜8 小时前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
你想考研啊8 小时前
mysql数据库导出导入
数据库·mysql·oracle
十年编程老舅9 小时前
Linux DRM:底层逻辑与实践架构
数据库·mysql