1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
@Service public class RedisLocationQueryService { @Autowired private RedisLocationQueryProperties properties; @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private RedisLocationStorageService redisLocationStorageService; @Autowired private RedisLocationMonitorService redisLocationMonitorService; private static final Logger logger = LoggerFactory.getLogger(RedisLocationQueryService.class);
public RedisLocationStorageResult storeLocationData(LocationData locationData) { logger.info("存储位置数据,对象ID: {}, 经度: {}, 纬度: {}", locationData.getObjectId(), locationData.getLongitude(), locationData.getLatitude()); RedisLocationStorageResult result = new RedisLocationStorageResult(); result.setObjectId(locationData.getObjectId()); result.setStartTime(System.currentTimeMillis()); try { if (!locationData.validate()) { result.setSuccess(false); result.setError("位置数据验证失败"); result.setEndTime(System.currentTimeMillis()); return result; } result = redisLocationStorageService.storeLocationData(locationData); redisLocationMonitorService.recordLocationStorage(locationData.getObjectId(), true); logger.info("位置数据存储完成,对象ID: {}, 成功: {}, 耗时: {}ms", locationData.getObjectId(), result.isSuccess(), result.getDuration()); return result; } catch (Exception e) { logger.error("位置数据存储异常,对象ID: {}", locationData.getObjectId(), e); result.setSuccess(false); result.setError("位置数据存储异常: " + e.getMessage()); result.setEndTime(System.currentTimeMillis()); redisLocationMonitorService.recordLocationStorage(locationData.getObjectId(), false); return result; } }
public RedisLocationQueryResult queryNearbyLocations(Double longitude, Double latitude, Double radius, String objectType) { logger.info("查询附近位置,中心点: ({}, {}), 半径: {}m, 对象类型: {}", longitude, latitude, radius, objectType); RedisLocationQueryResult result = new RedisLocationQueryResult(); result.setLongitude(longitude); result.setLatitude(latitude); result.setRadius(radius); result.setStartTime(System.currentTimeMillis()); try { if (longitude == null || latitude == null || radius == null) { result.setSuccess(false); result.setError("查询参数不能为空"); result.setEndTime(System.currentTimeMillis()); return result; } if (radius > properties.getMaxQueryRadius()) { radius = properties.getMaxQueryRadius(); } result = redisLocationStorageService.queryNearbyLocations(longitude, latitude, radius, objectType); redisLocationMonitorService.recordLocationQuery(longitude, latitude, result.getLocationDataList().size(), true); logger.info("附近位置查询完成,中心点: ({}, {}), 结果数量: {}, 耗时: {}ms", longitude, latitude, result.getLocationDataList().size(), result.getDuration()); return result; } catch (Exception e) { logger.error("附近位置查询异常,中心点: ({}, {})", longitude, latitude, e); result.setSuccess(false); result.setError("附近位置查询异常: " + e.getMessage()); result.setEndTime(System.currentTimeMillis()); redisLocationMonitorService.recordLocationQuery(longitude, latitude, 0, false); return result; } }
public RedisLocationQueryResult getCurrentLocation(String objectId) { logger.info("获取对象当前位置,对象ID: {}", objectId); RedisLocationQueryResult result = new RedisLocationQueryResult(); result.setObjectId(objectId); result.setStartTime(System.currentTimeMillis()); try { result = redisLocationStorageService.getCurrentLocation(objectId); redisLocationMonitorService.recordLocationQuery(0.0, 0.0, result.getLocationDataList().size(), true); logger.info("当前位置查询完成,对象ID: {}, 结果数量: {}, 耗时: {}ms", objectId, result.getLocationDataList().size(), result.getDuration()); return result; } catch (Exception e) { logger.error("当前位置查询异常,对象ID: {}", objectId, e); result.setSuccess(false); result.setError("当前位置查询异常: " + e.getMessage()); result.setEndTime(System.currentTimeMillis()); redisLocationMonitorService.recordLocationQuery(0.0, 0.0, 0, false); return result; } }
public double calculateDistance(Double longitude1, Double latitude1, Double longitude2, Double latitude2) { logger.info("计算两点间距离,点1: ({}, {}), 点2: ({}, {})", longitude1, latitude1, longitude2, latitude2); try { String key = properties.getLocationKeyPrefix() + "distance_calc"; redisTemplate.opsForGeo().add(key, new Point(longitude1, latitude1), "point1"); redisTemplate.opsForGeo().add(key, new Point(longitude2, latitude2), "point2"); Distance distance = redisTemplate.opsForGeo().distance(key, "point1", "point2", Metrics.KILOMETERS); redisTemplate.delete(key); double distanceInMeters = distance.getValue() * 1000; logger.info("距离计算完成,距离: {}m", distanceInMeters); return distanceInMeters; } catch (Exception e) { logger.error("距离计算异常", e); return 0.0; } } }
|