Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > redis GEO数据结构、附近商铺功能

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

作者:姓蔡小朋友

文章介绍了Redis中的GEO命令及其用途,包括地理坐标存储、距离计算、坐标转换和位置搜索等功能,还分享了如何使用Redis实现查询附近商铺的功能,包括导入商铺信息和根据类型及距离进行搜索

一、GEO

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

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

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

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

@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.根据商铺类型和距离查询附近商铺

@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);
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文