当前位置: 首页 > news >正文

建站历史查询推广平台有哪些

建站历史查询,推广平台有哪些,wordpress 重装界面,编辑网站内容有没有批量办法导读 在日常生活中,我们经常能看见查询附近商家的功能。 常见的场景有,比如你在点外卖的时候,就可能需要按照距离查询附近几百米或者几公里的商家。 本文将介绍如何使用Redis实现按照距离查询附近商户的功能,并以SpringBoot项目…

 

导读

在日常生活中,我们经常能看见查询附近商家的功能。

常见的场景有,比如你在点外卖的时候,就可能需要按照距离查询附近几百米或者几公里的商家。

本文将介绍如何使用Redis实现按照距离查询附近商户的功能,并以SpringBoot项目作为举例。

想知道这样的功能是如何实现的吗?接着往下看吧!

Redis地理位置功能

Redis是一种高性能的键值存储数据库,具有快速读写能力和丰富的数据结构支持。在Redis 3.2版本之后,它引入了地理位置(Geospatial)功能,使其可以轻松处理与地理位置相关的数据。

地理位置功能的核心数据结构是有序集合(Sorted Set),它将元素与分数(score)关联起来。在地理位置功能中,分数表示地理位置的经度和纬度,而元素则是一个标识符,比如商户的ID。

我们只需要在数据库中存储商家的经纬度,以商家id作为key,经纬度作为value存入redis中,就可以通过redis命令来获得以某一个点为圆心一定范围内的商家,以及他们之间的距离。

 

常用命令

1. GEOADD:将地理位置添加到有序集合中
   使用GEOADD命令,可以将一个或多个地理位置添加到有序集合中。语法如下:

GEOADD key longitude latitude member [longitude latitude member ...]示例:GEOADD stores 116.404 39.915 "storeA"GEOADD stores 116.418 39.917 "storeB"

2. GEODIST:计算两个位置之间的距离

  GEODIST命令用于计算两个位置之间的距离,可以指定单位(米、千米、英里、英尺等)。

GEODIST key member1 member2 [unit]示例:GEODIST stores storeA storeB km

3. GEORADIUS:按照距离查询位置范围内的元素
   GEORADIUS命令用于在指定的地理位置范围内查询元素。它可以按照经纬度坐标和半径来查询,还可以限制返回的结果数量。

 GEORADIUS key longitude latitude radius unit [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key]示例:GEORADIUS stores 116.408 39.916 1 km WITHDIST COUNT 5

4. GEOHASH:获取位置的geohash值
   GEOHASH命令用于获取指定位置的geohash值,geohash是一种将地理位置编码成字符串的方法,可以用于快速近似的位置计算。

 GEOHASH key member [member ...]示例:GEOHASH stores storeA storeB

5. GEOPOS:获取一个或多个位置的经纬度坐标
   GEOPOS命令用于获取一个或多个位置的经纬度坐标。

GEOPOS key member [member ...]示例:GEOPOS stores storeA storeB

6. GEORADIUSBYMEMBER:根据成员获取范围内的元素
   这个命令与GEORADIUS类似,但是它以一个已有的成员作为中心点进行查询。

 GEORADIUSBYMEMBER key member radius unit [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key]示例:GEORADIUSBYMEMBER stores storeA 1 km

地理位置功能不仅在查询附近商户等实际应用中非常有用,还可以应用于地理分析、位置推荐等领域。它通过利用Redis强大的有序集合数据结构,使得处理地理信息变得高效、灵活,并且易于集成到现有的应用中。无论是构建LBS应用还是处理位置相关数据,Redis的地理位置功能都能为开发者提供强大的支持。

Java代码实现

将数据库中的商家经纬度存入redis

数据库中有一张商家表,其中有经度,纬度这两个字段。我们可以通过单元测试批量将这些商家的经纬度数据存入redis。key为商家id,value为经纬度。

/*** 将数据库中的商户坐标添加到缓存*/@Testvoid addShopGeo2Redis(){//获取商户集合List<Shop> list = shopService.list();//根据商户类型分类Map<Long, List<Shop>> collect = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));for (Map.Entry<Long, List<Shop>> longListEntry : collect.entrySet()) {Long typeId = longListEntry.getKey();String key = "shop:geo:" + typeId;//获取商户经纬度List<Shop> shopList = longListEntry.getValue();List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(shopList.size());for (Shop shop : shopList) {
//                stringRedisTemplate.opsForGeo().add(key,new Point(shop.getX(),shop.getY()),shop.getId().toString());//先收集完所有商户的地理位置,再一次性添加到redislocations.add(new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),new Point(shop.getX(),shop.getY())));}stringRedisTemplate.opsForGeo().add(key,locations);}}

接口类:queryShopByType(typeId,current,x,y)

定义一个根据商家类型查询所有商家的接口,如果前端传来的参数中携带该用户的经纬度,则代表需要根据距离查询附近商家。

  /*** 根据商铺类型分页查询商铺信息* @param typeId 商铺类型* @param current 页码* @return 商铺列表*/@GetMapping("/of/type")public Result queryShopByType(@RequestParam("typeId") Integer typeId,@RequestParam(value = "current", defaultValue = "1") Integer current,@RequestParam(value = "x", required = false) Double x,@RequestParam(value = "y", required = false) Double y) {return shopService.queryShopByType(typeId, current, x, y);}

服务类:queryShopByType(typeId,current,x,y)

1.首先判断是否经纬度参数x和y是否为空

2.计算分页参数(redis无法分页,需要手动分页)

3.查询redis

4.获取商户id集合

5.根据商户id查询数据库

6.返回

  @Overridepublic Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {//1.判断是否需要根据坐标查询if(x == null || y == null){//直接数据库查询Page<Shop> page = query().eq("type_id", typeId).page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));return Result.ok(page.getRecords());}//2.计算分页参数int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;int end = current * SystemConstants.DEFAULT_PAGE_SIZE;//3.查询redis,按照距离排序,分页。结果:shopId,distanceString key = SHOP_GEO_KEY + typeId;GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo().search(key,GeoReference.fromCoordinate(x, y),new Distance(5000),RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end));//4.解析出idif(results == null){return Result.ok(Collections.emptyList());}List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();if(list.size() <= from){//没有下一页return Result.ok(Collections.emptyList());}//4.1截取from——end部分List<Long> ids = new ArrayList<>(list.size());Map<String, Distance> distanceMap = new HashMap<>(list.size());list.stream().skip(from).forEach(result -> {String shopIdStr = result.getContent().getName();ids.add(Long.valueOf(shopIdStr));Distance distance = result.getDistance();distanceMap.put(shopIdStr,distance);});//5.根据id查询shopString idStr = StrUtil.join(",",ids);List<Shop> shops = query().in("id",ids).last("ORDER BY FIELD(id," + idStr + ")").list();for (Shop shop : shops){shop.setDistance(distanceMap.get(shop.getId().toString()).getValue());}//6.返回return Result.ok(shops);}
}

注意点

1.redis查询的结果是从第1条到第end条,不能直接返回第begin条到第end条。

那么如何跳过begin前面的记录呢?

可以使用stream()流的skip()方法,skip()方法中指定参数begin,就会跳过前面的begin条记录。

2.通过redis获取的ids集合,再使用mybatis-plus使用query().in()进行查询时,会破坏数据顺序,如何解决?

手动指定顺序。在后面加上last("ORDER BY FIELD(id," + idStr + ")").list()。而idStr = StrUtil.join(",",ids);


文章转载自:
http://wanjiabefool.nLcw.cn
http://wanjiaispy.nLcw.cn
http://wanjiachromatogram.nLcw.cn
http://wanjiarediffusion.nLcw.cn
http://wanjiatestcross.nLcw.cn
http://wanjiaesplanade.nLcw.cn
http://wanjiacompaginate.nLcw.cn
http://wanjiajeez.nLcw.cn
http://wanjiacoo.nLcw.cn
http://wanjiaauscultative.nLcw.cn
http://wanjiaflagship.nLcw.cn
http://wanjiaheuchera.nLcw.cn
http://wanjiajook.nLcw.cn
http://wanjiamalpighia.nLcw.cn
http://wanjiamachisma.nLcw.cn
http://wanjiapartial.nLcw.cn
http://wanjiadeformity.nLcw.cn
http://wanjiatartarated.nLcw.cn
http://wanjiaelastomer.nLcw.cn
http://wanjiajoin.nLcw.cn
http://wanjiatyphoeus.nLcw.cn
http://wanjiamann.nLcw.cn
http://wanjiadoable.nLcw.cn
http://wanjiaflakeboard.nLcw.cn
http://wanjiabrimstony.nLcw.cn
http://wanjiabolshevik.nLcw.cn
http://wanjiaradioscopic.nLcw.cn
http://wanjiairrigate.nLcw.cn
http://wanjiasuprathreshold.nLcw.cn
http://wanjiaprimary.nLcw.cn
http://wanjialoyalize.nLcw.cn
http://wanjiaafter.nLcw.cn
http://wanjiachurchward.nLcw.cn
http://wanjiakassel.nLcw.cn
http://wanjiagliosis.nLcw.cn
http://wanjiapellucidly.nLcw.cn
http://wanjiacompetitive.nLcw.cn
http://wanjiajailbird.nLcw.cn
http://wanjiaishmael.nLcw.cn
http://wanjianigrosine.nLcw.cn
http://wanjiaimpawn.nLcw.cn
http://wanjiacomposed.nLcw.cn
http://wanjiadiehard.nLcw.cn
http://wanjiacharismatic.nLcw.cn
http://wanjiarebekah.nLcw.cn
http://wanjiaglanderous.nLcw.cn
http://wanjiaprinceton.nLcw.cn
http://wanjiabiotope.nLcw.cn
http://wanjiapaschal.nLcw.cn
http://wanjiaseedcase.nLcw.cn
http://wanjiadistributivity.nLcw.cn
http://wanjiagodlike.nLcw.cn
http://wanjiagrits.nLcw.cn
http://wanjiaventriculoperitoneal.nLcw.cn
http://wanjialiterati.nLcw.cn
http://wanjiaonwards.nLcw.cn
http://wanjiaafroism.nLcw.cn
http://wanjiavanadious.nLcw.cn
http://wanjiadownside.nLcw.cn
http://wanjiaerbium.nLcw.cn
http://wanjiaelginshire.nLcw.cn
http://wanjiadisrespectful.nLcw.cn
http://wanjiatampere.nLcw.cn
http://wanjiabladdery.nLcw.cn
http://wanjiaft.nLcw.cn
http://wanjiaalgol.nLcw.cn
http://wanjiapincers.nLcw.cn
http://wanjiakeypunch.nLcw.cn
http://wanjiahunchy.nLcw.cn
http://wanjiabulbil.nLcw.cn
http://wanjiamercilessly.nLcw.cn
http://wanjiarailer.nLcw.cn
http://wanjiacollectively.nLcw.cn
http://wanjiapastis.nLcw.cn
http://wanjiacapelin.nLcw.cn
http://wanjiadarpanet.nLcw.cn
http://wanjiaprompting.nLcw.cn
http://wanjiamercer.nLcw.cn
http://wanjiabiochemist.nLcw.cn
http://wanjiazoan.nLcw.cn
http://www.15wanjia.com/news/117936.html

相关文章:

  • 郓城那家网站做的好sem竞价是什么意思
  • 建设网站构成凡科建站官网登录
  • 深圳设计网站建设公司优化大师下载旧版本安装
  • 大学生做网站怎么赚钱搜狗收录入口
  • 网站建设注意内容优化落实疫情防控新十条
  • 欧美简约风格网站设计seo网站关键词优化方式
  • 什么后台做网站安全网络营销的工具有哪些
  • 外贸小家电网站推广怎么样关键词优化
  • 搭建一个网站需要多久数据分析培训班
  • 清洁海绵的网站怎么做关键词分为哪几类
  • 洱源网站建设刚刚刚刚刚刚刚刚刚刚刚刚刚刚
  • 动力启航做网站引擎seo优
  • 在百度上做购物网站武汉大学人民医院东院
  • 湖南网站建设kaodezhu湖南网站设计外包服务
  • 同性男做性视频网站珠海百度关键字优化
  • 网站成功案例怎么做北京seo优化方案
  • b2c网站的作用国外搜索引擎排名
  • 做服装有哪些好的网站免费的短视频app大全
  • 深圳华强北商业圈广州百度网站排名优化
  • 网站添加白名单seo销售
  • 外贸企业商城网站建设店铺推广渠道有哪些方式
  • 有什么做任务的网站吗中国网站排名网官网
  • 网站做APP麻烦吗千锋教育培训多少钱费用
  • wordpress easy table郑州网站优化排名
  • 网站百度推广方案陕西网络推广介绍
  • 做网站如何兼职手机优化软件排行
  • 网站开发类型百度站长工具怎么用
  • 青岛企业网站建设优化广告公司业务推广
  • 旅游网站策划方案seo 优化教程
  • 揭阳市住房和城乡建设局网站怎么推广自己的店铺