首页 / REDIS / redis geo操作
redis geo操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了redis geo操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3615字,纯文字阅读大概需要6分钟。
内容图文
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());
}
}
}
原文:https://www.cnblogs.com/java-le/p/12085102.html
内容总结
以上是互联网集市为您收集整理的redis geo操作全部内容,希望文章能够帮你解决redis geo操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。