课程名称:多端全栈项目实战:商业级代驾全流程落地
课程章节: 华夏代驾全栈小程序实战
课程讲师: 神思者
课程内容:
位置服务缓存司机定时定位

司机端子系统通过fegin远程调用的方式 通过updateLocationCache 方法调用seriver
并缓存到redis里面 这是需要设置两个缓存 一个是geo缓存 一个是司机上线缓存

当司机进行停止接单后 我们需要删除缓存
创建两个抽象接口 分别是
public void updateLocationCache(Map param);
下面的form 将装缓存map对象 所以这里使用map
至于需要传什么值 通过form类里面的值来
@Data@Schema(description = "更新司机GPS坐标缓存的表单")public class UpdateLocationCacheForm {
@NotNull(message = "driverId不能为空")
@Min(value = 1, message = "driverId不能小于1")
@Schema(description = "司机ID")
private Long driverId;
@NotBlank(message = "latitude不能为空")
@Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "latitude内容不正确")
@Schema(description = "纬度")
private String latitude;
@NotBlank(message = "longitude不能为空")
@Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "longitude内容不正确")
@Schema(description = "经度")
private String longitude;
@NotNull(message = "rangeDistance不能为空")
@Range(min = 1, max = 5, message = "rangeDistance范围错误")
@Schema(description = "接收几公里内的订单")
private Integer rangeDistance;
@NotNull(message = "orderDistance不能为空")
@Schema(description = "接收代驾里程几公里以上的订单")
@Range(min = 0, max = 30, message = "orderDistance范围错误")
private Integer orderDistance;
@Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "orientateLongitude内容不正确")
@Schema(description = "定向接单的经度")
private String orientateLongitude;
@Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "orientateLatitude内容不正确")
@Schema(description = "定向接单的纬度")
private String orientateLatitude;}public void removeLocationCache(long driverId);
这个抽象方法用来删除两种缓存
设置第一种缓存 首先需要把取到得值 进行转义
long driverId = MapUtil.getLong(param, "driverId"); String latitude = MapUtil.getStr(param, "latitude"); String longitude = MapUtil.getStr(param, "longitude"); //接单范围 int rangeDistance = MapUtil.getInt(param, "rangeDistance"); //订单里程范围 int orderDistance = MapUtil.getInt(param, "orderDistance");
geo里面要求的经纬度必须是double类型的所以我们需要在进行一次
//封装成Point对象才能缓存到Redis里面
Point point = new Point(Convert.toDouble(longitude), Convert.toDouble(latitude));
/*
* 把司机实时定位缓存到Redis里面,便于Geo定位计算
* Geo是集合形式,如果设置过期时间,所有司机的定位缓存就全都失效了
* 正确做法是司机上线后,更新GEO中的缓存定位
*/
redisTemplate.opsForGeo().add("driver_location", point, driverId + "");获取定向接单的经纬度 (司机 定制 想要去哪里的 定向)
//定向接单地址的经度
String orientateLongitude = null;
if (param.get("orientateLongitude") != null) {
orientateLongitude = MapUtil.getStr(param, "orientateLongitude");
}
//定向接单地址的纬度
String orientateLatitude = null;
if (param.get("orientateLatitude") != null) {
orientateLatitude = MapUtil.getStr(param, "orientateLatitude");
}
//定向接单经纬度的字符串
String orientation = "none";
if (orientateLongitude != null && orientateLatitude != null) {
orientation = orientateLatitude + "," + orientateLongitude;
}为司机设置一个超时60秒缓存 如果超过一分钟没有 那就代表他下线了
/*
* 为了解决判断哪些司机在线,我们还要单独弄一个上线缓存
* 缓存司机的接单设置(定向接单、接单范围、订单总里程),便于系统判断该司机是否符合接单条件
*/
String temp = rangeDistance + "#" + orderDistance + "#" + orientation;
redisTemplate.opsForValue().set("driver_online#" + driverId, temp, 60, TimeUnit.SECONDS);接下来写两个controller
@PostMapping("/updateLocationCache")
@Operation(summary = "更新司机GPS定位缓存")
public R updateLocationCache(@RequestBody @Valid UpdateLocationCacheForm form) {
//更新新的定位缓存
Map param = BeanUtil.beanToMap(form);
driverLocationService.updateLocationCache(param);
return R.ok();
}
@PostMapping("/removeLocationCache")
@Operation(summary = "删除司机GPS定位缓存")
public R removeLocationCache(@RequestBody @Valid RemoveLocationCacheForm form) {
driverLocationService.removeLocationCache(form.getDriverId());
return R.ok();
}
随时随地看视频