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);
}
相关推荐
C雨后彩虹1 天前
任务最优调度
java·数据结构·算法·华为·面试
heartbeat..1 天前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
麦聪聊数据1 天前
MySQL并发与锁:从“防止超卖”到排查“死锁”
数据库·sql·mysql
AC赳赳老秦1 天前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
一条大祥脚1 天前
26.1.9 轮廓线dp 状压最短路 构造
数据结构·c++·算法
YMatrix 官方技术社区1 天前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix
辞砚技术录1 天前
MySQL面试题——索引2nd
数据库·mysql·面试
linweidong1 天前
C++thread pool(线程池)设计应关注哪些扩展性问题?
java·数据库·c++
cpp_25011 天前
P2708 硬币翻转
数据结构·c++·算法·题解·洛谷
程序猿阿伟1 天前
《Python复杂结构静态分析秘籍:递归类型注解的深度实践指南》
java·数据结构·算法