实时轨迹事件缓存代码更新优化
parent
ef1a65d841
commit
17c2990300
5
pom.xml
5
pom.xml
|
@ -116,6 +116,11 @@
|
||||||
<artifactId>god-common-datasource</artifactId>
|
<artifactId>god-common-datasource</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- God Common DataScope -->
|
<!-- God Common DataScope -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.god</groupId>
|
<groupId>com.god</groupId>
|
||||||
|
|
|
@ -8,8 +8,10 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实时轨迹事件
|
* 实时轨迹事件
|
||||||
|
@ -23,13 +25,14 @@ public class RealTimeTrajectoryEvent implements EventService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
// @Autowired
|
|
||||||
// private RedisTemplate<String,Object> redisTemplate;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RabbitTemplate rabbitTemplate;
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实时轨迹标识
|
||||||
|
*/
|
||||||
public static final String LOCUS = "locus";
|
public static final String LOCUS = "locus";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加时间
|
* 添加时间
|
||||||
*/
|
*/
|
||||||
|
@ -49,20 +52,45 @@ public class RealTimeTrajectoryEvent implements EventService {
|
||||||
carMessage.getLongitude(),
|
carMessage.getLongitude(),
|
||||||
carMessage.getLatitude()
|
carMessage.getLatitude()
|
||||||
);
|
);
|
||||||
//判断是否有key 有则更新数据
|
//新建一个用于存经纬度的集合 保存精度和维度
|
||||||
if (redisService.hasKey(LOCUS + carMessage.getVin())){
|
ArrayList<BigDecimal> bigDecimals = new ArrayList<>();
|
||||||
//获取所有车辆数据
|
//增加经纬度数据
|
||||||
List<Object> cacheList = redisService.getCacheList(carMessage.getVin());
|
bigDecimals.add(carMessage.getLongitude());
|
||||||
//添加车辆信息
|
bigDecimals.add(carMessage.getLatitude());
|
||||||
cacheList.add(carMessage);
|
//预防redis空值 避免查询不存在的key返回导致无法操作
|
||||||
//redis更新
|
if (redisService.hasKey(LOCUS + carMessage.getVin())){//vin轨迹的键存在
|
||||||
redisService.setCacheList(LOCUS + carMessage.getVin(), cacheList);;
|
//获取键 对应的 值
|
||||||
}else {//没有创建对应缓存
|
Map<String, Object> cacheMap = redisService.getCacheMap(LOCUS + carMessage.getVin());
|
||||||
ArrayList<CarMessage> carMessages = new ArrayList<>();
|
//前端需要格式 [ [..] , [..] ] 所以需要再次新建一个集合来存之前的集合
|
||||||
carMessages.add(carMessage);
|
|
||||||
redisService.setCacheList(LOCUS + carMessage.getVin(),carMessages);
|
ArrayList<ArrayList<BigDecimal>> orDefault = (ArrayList<ArrayList<BigDecimal>>) cacheMap.getOrDefault(carMessage.getVin(), new ArrayList<ArrayList<BigDecimal>>());
|
||||||
|
addLocusRedisInfo(cacheMap,
|
||||||
|
orDefault,
|
||||||
|
bigDecimals,
|
||||||
|
carMessage.getVin());
|
||||||
|
}else {
|
||||||
|
//代表当前vin没有实时轨迹相关数据 直接新建集合存储即可
|
||||||
|
addLocusRedisInfo(new HashMap<>(),
|
||||||
|
new ArrayList<>(),
|
||||||
|
bigDecimals,
|
||||||
|
carMessage.getVin());
|
||||||
}
|
}
|
||||||
//rabbit进行通知开启实时轨迹
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装公共存取redis方法
|
||||||
|
* @param map
|
||||||
|
* @param lists
|
||||||
|
* @param list
|
||||||
|
* @param vin
|
||||||
|
*/
|
||||||
|
private void addLocusRedisInfo(Map<String,Object> map,
|
||||||
|
ArrayList<ArrayList<BigDecimal>> lists,
|
||||||
|
ArrayList<BigDecimal> list,
|
||||||
|
String vin) {
|
||||||
|
lists.add(list);
|
||||||
|
map.put(vin,lists);
|
||||||
|
redisService.setCacheMap(LOCUS + vin,map);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
package com.god.data.test;
|
||||||
|
|
||||||
|
import com.god.common.redis.service.RedisService;
|
||||||
|
import com.god.data.common.domain.CarMessage;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* @Author: sun-cool-boy
|
||||||
|
* @Date: 2023/12/1
|
||||||
|
* @info:
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
public class SJZTest {
|
||||||
|
/**
|
||||||
|
* 实时轨迹存储
|
||||||
|
*/
|
||||||
|
public static final String ALL_LOCUS_INFO = "all_locus_info";
|
||||||
|
/**
|
||||||
|
* 实时轨迹标识
|
||||||
|
*/
|
||||||
|
public static final String LOCUS = "locus";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
@Test
|
||||||
|
public void testRedis(){
|
||||||
|
ArrayList<BigDecimal> bigDecimals = new ArrayList<>();
|
||||||
|
bigDecimals.add(BigDecimal.valueOf(12.222));
|
||||||
|
bigDecimals.add(BigDecimal.valueOf(35.232));
|
||||||
|
ArrayList<ArrayList<BigDecimal>> arrayLists = new ArrayList<>();
|
||||||
|
arrayLists.add(bigDecimals);
|
||||||
|
HashMap<String, ArrayList<ArrayList<BigDecimal>>> map = new HashMap<>();
|
||||||
|
map.put("vim",arrayLists);
|
||||||
|
redisService.setCacheMap("123",map);
|
||||||
|
|
||||||
|
Map<String, Object> cacheMap = redisService.getCacheMap("123");
|
||||||
|
cacheMap.forEach((key,value)->{
|
||||||
|
System.out.println(key + " " +value);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2(){
|
||||||
|
CarMessage one = CarMessage.builder()
|
||||||
|
.vin("vin5211314")
|
||||||
|
.longitude(BigDecimal.valueOf(22.4512439))
|
||||||
|
.latitude(BigDecimal.valueOf(23.49785))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
CarMessage two = CarMessage.builder()
|
||||||
|
.vin("vin5211314")
|
||||||
|
.longitude(BigDecimal.valueOf(44.91230))
|
||||||
|
.latitude(BigDecimal.valueOf(55.45456))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
CarMessage three = CarMessage.builder()
|
||||||
|
.vin("vin5211314")
|
||||||
|
.longitude(BigDecimal.valueOf(29.41235))
|
||||||
|
.latitude(BigDecimal.valueOf(36.41235))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ArrayList<CarMessage> list = new ArrayList<>();
|
||||||
|
list.add(one);
|
||||||
|
list.add(two);
|
||||||
|
list.add(three);
|
||||||
|
for (CarMessage carMessage : list) {
|
||||||
|
//新建一个用于存经纬度的集合 保存精度和维度
|
||||||
|
ArrayList<BigDecimal> bigDecimals = new ArrayList<>();
|
||||||
|
bigDecimals.add(carMessage.getLongitude());
|
||||||
|
bigDecimals.add(carMessage.getLatitude());
|
||||||
|
if (redisService.hasKey(ALL_LOCUS_INFO)){//vin轨迹的键存在
|
||||||
|
//获取键 对应的 值
|
||||||
|
Map<String, Object> cacheMap = redisService.getCacheMap(ALL_LOCUS_INFO);
|
||||||
|
//前端需要格式 [ [..] , [..] ] 所以需要再次新建一个集合来存之前的集合
|
||||||
|
ArrayList<ArrayList<BigDecimal>> orDefault = (ArrayList<ArrayList<BigDecimal>>) cacheMap.getOrDefault(carMessage.getVin(), new ArrayList<ArrayList<BigDecimal>>());
|
||||||
|
addLocusRedisInfo(cacheMap,
|
||||||
|
orDefault,
|
||||||
|
bigDecimals,
|
||||||
|
carMessage.getVin());
|
||||||
|
}else {
|
||||||
|
//代表当前vin没有实时轨迹相关数据 直接新建集合存储即可
|
||||||
|
addLocusRedisInfo(new HashMap<>(),
|
||||||
|
new ArrayList<>(),
|
||||||
|
bigDecimals,
|
||||||
|
carMessage.getVin());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void addLocusRedisInfo(Map<String, Object> map,
|
||||||
|
ArrayList<ArrayList<BigDecimal>> lists,
|
||||||
|
ArrayList<BigDecimal> list,
|
||||||
|
String vin) {
|
||||||
|
lists.add(list);
|
||||||
|
map.put(vin,lists);
|
||||||
|
redisService.setCacheMap(ALL_LOCUS_INFO,map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testThree(){
|
||||||
|
Map<String, Object> cacheMap = redisService.getCacheMap(ALL_LOCUS_INFO);
|
||||||
|
ArrayList<ArrayList<BigDecimal>> orDefault = (ArrayList<ArrayList<BigDecimal>>) cacheMap.getOrDefault("vin123123123", new ArrayList<ArrayList<BigDecimal>>());
|
||||||
|
ArrayList<BigDecimal> bigDecimals = orDefault.get(orDefault.size() - 1);
|
||||||
|
bigDecimals.forEach(obj -> {
|
||||||
|
System.out.println(obj);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,16 @@
|
||||||
package com.god.data.test;
|
package com.god.data.test;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.god.common.redis.service.RedisService;
|
||||||
import com.god.data.common.domain.CarMessage;
|
import com.god.data.common.domain.CarMessage;
|
||||||
import com.god.data.utils.AnalyzeUtils;
|
import com.god.data.utils.AnalyzeUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 解析测试
|
* @description: 解析测试
|
||||||
|
@ -12,6 +19,7 @@ import org.junit.jupiter.api.Test;
|
||||||
*/
|
*/
|
||||||
public class Test007 {
|
public class Test007 {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试方法:showStr
|
* 测试方法:showStr
|
||||||
*/
|
*/
|
||||||
|
@ -31,4 +39,5 @@ public class Test007 {
|
||||||
System.out.println(carMessage);
|
System.out.println(carMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue