redis geo操作
package club.newtech.qbike.trip.domain.service;
import club.newtech.qbike.trip.domain.core.Status;
import club.newtech.qbike.trip.domain.core.root.DriverStatus;
import club.newtech.qbike.trip.domain.core.vo.Driver;
import club.newtech.qbike.trip.domain.core.vo.Position;
import club.newtech.qbike.trip.domain.repository.DriverStatusRepo;
import club.newtech.qbike.trip.domain.repository.PositionRepository;
import club.newtech.qbike.trip.infrastructure.UserRibbonHystrixApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import static java.util.stream.Collectors.toList;
@Service
@Transactional
public class PositionService {
private static final Logger LOGGER = LoggerFactory.getLogger(PositionService.class);
@Autowired
DriverStatusRepo driverStatusRepo;
@Autowired
UserRibbonHystrixApi userService;
@Autowired
RedisTemplate<String, String> redisTemplate;
@Autowired
PositionRepository positionRepository;
/**
* 上传位置
*
* @param driverId
* @param longitude
* @param latitude
*/
public void updatePosition(Integer driverId, Double longitude, Double latitude) {
//先记录轨迹
Date current = new Date();
Position position = new Position();
position.setDriverId(String.valueOf(driverId));
position.setPositionLongitude(longitude);
position.setPositionLatitude(latitude);
//TODO 目前没有上传上下线状态
position.setStatus(Status.ONLINE);
position.setUploadTime(current);
//保存位置信息
positionRepository.save(position);
//更新状态表
DriverStatus driverStatus = driverStatusRepo.findByDriver_Id(driverId);
if (driverStatus != null) {
driverStatus.setCurrentLongitude(longitude);
driverStatus.setCurrentLatitude(latitude);
driverStatus.setUpdateTime(current);
driverStatusRepo.save(driverStatus);
} else {
Driver driver = userService.findById(driverId);
driverStatus = new DriverStatus();
driverStatus.setDriver(driver);
driverStatus.setCurrentLongitude(longitude);
driverStatus.setCurrentLatitude(latitude);
driverStatus.setUpdateTime(current);
driverStatus.setStatus(Status.ONLINE);
driverStatus.setDId(driverId);
driverStatusRepo.save(driverStatus);
}
//更新Redis中的坐标数据,GeoHash
redisTemplate.opsForGeo().geoAdd("Drivers", new Point(longitude, latitude), String.valueOf(driverId));
LOGGER.info("position update " + driverStatus);
}
/**
* 匹配司机
*
* @param longitude
* @param latitude
* @return
*/
public Collection<DriverStatus> matchDriver(double longitude, double latitude) {
Circle circle = new Circle(new Point(longitude, latitude), //
new Distance(500, RedisGeoCommands.DistanceUnit.METERS));
GeoResults<RedisGeoCommands.GeoLocation<String>> result =
redisTemplate.opsForGeo().geoRadius("Drivers", circle);
if (result.getContent().size() == 0) {
LOGGER.info("没找到匹配司机");
return new ArrayList<>();
} else {
List<String> drivers = result.getContent()
.stream()
.map(x -> x.getContent().getName())
.collect(toList());
LOGGER.info("获取附近司机为{}", drivers);
return drivers.stream().map(Integer::parseInt)
.map(id -> driverStatusRepo.findByDriver_Id(id)).collect(toList());
}
}
}
redis geo操作的更多相关文章
- Redis数据类型:Hashes、Geo操作指令
Redis数据类型:Hashes.Geo操作指令 Hashes常用操作指令 Redis Hashes是一个键值对的映射表,最对能存储2^32-1(约40亿)个键值对. HSET HGET HSET:将 ...
- Redis GEO 功能使用场景
本文来源:https://www.dazhuanlan.com/2020/02/05/5e3a0a3110649/ 背景 前段时间自己在做附近直播相关业务,其中有一个核心的点就是检索用户附近的主播,也 ...
- Redis GEO 地理位置
目录 GEO指令 GEOADD GEODIST GEOPOP GEOHASH GEORADIUS GEORADIUSBYMEMBER 指令补充 删除操作 避免单集合数量过多 存储原理 GEOADD存储 ...
- Redis Geo: Redis新增位置查询功能
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/144.html 移动互联网增进了人与人之间的联系,其中基于位置信息的服务( ...
- spring data redis RedisTemplate操作redis相关用法
http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...
- Spring Framework 中启动 Redis 事务操作
背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- 转:Redis Geo: Redis新增位置查询功能
原文来自于:http://www.infoq.com/cn/news/2015/07/redis-geo 移动互联网增进了人与人之间的联系,其中基于位置信息的服务(Location Based Ser ...
- 【springboot】【redis】springboot结合redis,操作List集合实现时间轴功能
springboot结合redis,操作List集合实现时间轴功能
随机推荐
- HTML5页面如何在手机端浏览器调用相机、相册功能
最近在做一个公司的保险信息处理系统项目,开发微信端浏览器访问的HTML5的页面,页面中有一个<input id="input" type="file"/& ...
- ACM-ICPC 2018 徐州赛区网络预赛 I. query 树状数组
I. query 题目链接: Problem Description Given a permutation \(p\) of length \(n\), you are asked to answe ...
- DP单调队列--斜率优化P3195
题意:https://www.luogu.com.cn/problem/P3195 思路:https://www.luogu.com.cn/problemnew/solution/P3195 #def ...
- 消息服务百科全书——Kafka基本原理介绍
架构 1.1 总体架构 因为Kafka内在就是分布式的,一个Kafka集群通常包括多个代理. 为了均衡负载,将话题分成多个分区,每个代理存储一或多个分区.多个生产者和消费者能够同时生产和获取消息. 一 ...
- SpringBoot中获取spring.profiles.active的值
一.网上很多采用@Profile("dev")的方式获取,但是这个是类级别的 二.开发中可能需要代码级别 1.刚开始我想通过classpath下的文件读取方式,麻烦死了,于是换了个 ...
- SqlServer学习之触发器
什么是触发器? 根据百度百科的解释,触发器是SqlServer提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,他的执行不是有程序调用,也不是手工启动,而是由事件来 ...
- 【温故知新】php 魔术方法
<?php class Magic{ private $name; /** *构造方法,在类被实例化时自动调用,一般用于初始化操作 */ public function __construct( ...
- JavaScript基本使用
基本使用 1.JavaScript组成 ECMAScript+BOM+DOM BOM的思想(重点) DOM的思想(重点) 2.使用<script></script>标签 doc ...
- canvas-绘制矩形-读书笔记
使用<canvas>元素,必须先设置其width和height属性,指定可以绘图的区域大小. 要在画布上绘图,需要取得绘图上下文,也就是要调用getContext()方法并传入上下文的名字 ...
- python使用openpyxl操作execl
openpyxl openpyxl可以用来对excel进行操作,但只能操作xlsx文件而不能操作xls文件. 主要用到三个概念:Workbooks,Sheets,Cells.Workbook就是一个e ...