Redis:GEO(Geolocation)数据结构使用

22 篇文章 0 订阅
文章介绍了如何在SpringBoot应用中排除默认的spring-data-redis和lettuce-core依赖,然后手动引入特定版本的库。接着展示了如何利用SpringDataRedis的opsForGeo方法添加和查询地理位置数据,包括按距离排序和分页的方法。
摘要由CSDN通过智能技术生成

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-data-redis</artifactId>
            <groupId>org.springframework.data</groupId>
        </exclusion>
        <exclusion>
            <artifactId>lettuce-core</artifactId>
            <groupId>io.lettuce</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.6.2</version>
</dependency>
<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>6.1.6.RELEASE</version>
</dependency>

import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;

// 向redis添加
stringRedisTemplate.opsForGeo().add(key, new Point(shop.getX(), shop.getY()), shopId);
// 或者
List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(value.size());
locations.add(new RedisGeoCommands.GeoLocation<>(
        shop.getId().toString(),
        new Point(shop.getX(), shop.getY())
));
stringRedisTemplate.opsForGeo().add(key, locations);


// 查询redis、按照距离排序、分页。结果:shopId、distance
// 计算分页参数 current当前页
int from = (current - 1) * 5;
int end = current * 5;

GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo() 
        .search(
               key,
               GeoReference.fromCoordinate(x, y),
               new Distance(5000),                                                
               RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end)
        );

// 判断是否有数据
if (results == null) {
    return Result.ok(Collections.emptyList());
}

List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();
if (list.size() <= from) {
    // 没有下一页了,结束
    return Result.ok(Collections.emptyList());
}

// 截取 from ~ end的部分
List<Long> ids = new ArrayList<>(list.size());
Map<String, Distance> distanceMap = new HashMap<>(list.size());
list.stream().skip(from).forEach(result -> {
    // 获取shopId
    String shopIdStr = result.getContent().getName();
    ids.add(Long.valueOf(shopIdStr));
    // 获取距离
    Distance distance = result.getDistance();
    distanceMap.put(shopIdStr, distance);
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值