feat(): 优化缓存结构,新增实时数据开关接口
parent
033c0aa512
commit
ace9b38f5a
|
@ -21,7 +21,8 @@ public enum CacheNameEnums {
|
||||||
FAULT("fault", "故障"),
|
FAULT("fault", "故障"),
|
||||||
FENCE("fence", "围栏"),
|
FENCE("fence", "围栏"),
|
||||||
WARMING("warming", "预警"),
|
WARMING("warming", "预警"),
|
||||||
REALTIME("realTime", "实时信息");
|
REALTIME("realTime", "实时信息"),
|
||||||
|
START("start", "状态表示");
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private final String info;
|
private final String info;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.muyu.data.processing.utils;
|
package com.muyu.common.caffeine.utils;
|
||||||
|
|
||||||
import com.github.yulichang.toolkit.SpringContentUtils;
|
import com.github.yulichang.toolkit.SpringContentUtils;
|
||||||
|
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
|
@ -34,6 +35,24 @@ public class CacheUtils {
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除缓存 - 根据缓存分区名称
|
||||||
|
* @param cacheName 缓存分区名称
|
||||||
|
* @return 操作结果
|
||||||
|
*/
|
||||||
|
public void delCache(String cacheName) {
|
||||||
|
getCache(cacheName).invalidate();
|
||||||
|
log.info("缓存清除成功:分区-{}", cacheName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除缓存 - 根据键值
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
public void delCacheValueAll(String key) {
|
||||||
|
CacheNameEnums.getCodes().forEach(cacheName -> delCacheValue(cacheName, key));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置缓存值
|
* 设置缓存值
|
||||||
* @param cacheName 缓存分区名称
|
* @param cacheName 缓存分区名称
|
||||||
|
@ -76,4 +95,17 @@ public class CacheUtils {
|
||||||
public <T> T getCacheValue(String cacheName, String key, Class<T> type) {
|
public <T> T getCacheValue(String cacheName, String key, Class<T> type) {
|
||||||
return getCache(cacheName).get(key, type);
|
return getCache(cacheName).get(key, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除缓存
|
||||||
|
* @param cacheName 缓存分区名称
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void delCacheValue(String cacheName, String key) {
|
||||||
|
getCache(cacheName).retrieve(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
package com.muyu.data.processing.controller;
|
package com.muyu.data.processing.controller;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.rabbit.config.RabbitmqConfig;
|
import com.muyu.common.rabbit.config.RabbitmqConfig;
|
||||||
import com.muyu.data.processing.service.DataProcessingService;
|
import com.muyu.data.processing.service.DataProcessingService;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import lombok.NonNull;
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -33,4 +35,15 @@ public class DataProcessingController {
|
||||||
rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.email", vin);
|
rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.email", vin);
|
||||||
log.info("发送消息成功:{}",vin);
|
log.info("发送消息成功:{}",vin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 车辆实时数据订阅开关
|
||||||
|
* @param vin 车辆vin码
|
||||||
|
* @param status 开关状态
|
||||||
|
* @return 返回结果
|
||||||
|
*/
|
||||||
|
@GetMapping("/carRealTimeStatus")
|
||||||
|
public Result<String> carRealTimeStatus(@RequestParam("vin") @NonNull String vin, @RequestParam("status") @NonNull Boolean status) {
|
||||||
|
return Result.success(service.carRealTimeStatus(vin, status));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
package com.muyu.data.processing.service;
|
package com.muyu.data.processing.service;
|
||||||
|
|
||||||
|
|
||||||
import com.muyu.data.processing.domain.BasicData;
|
|
||||||
import com.muyu.data.processing.domain.CarData;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据处理业务层
|
* 数据处理业务层
|
||||||
*
|
*
|
||||||
|
@ -19,4 +13,12 @@ import java.util.List;
|
||||||
|
|
||||||
public interface DataProcessingService{
|
public interface DataProcessingService{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动或停止实时数据
|
||||||
|
*
|
||||||
|
* @param vin 车辆vin码
|
||||||
|
* @param status 开关状态
|
||||||
|
* @return 返回结果
|
||||||
|
*/
|
||||||
|
String carRealTimeStatus(String vin, Boolean status);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,9 @@ package com.muyu.data.processing.service.impl;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||||
|
import com.muyu.common.caffeine.utils.CacheUtils;
|
||||||
import org.apache.iotdb.session.pool.SessionPool;
|
import org.apache.iotdb.session.pool.SessionPool;
|
||||||
import org.springframework.cache.Cache;
|
|
||||||
import org.springframework.cache.CacheManager;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.muyu.data.processing.mapper.DataProcessingMapper;
|
import com.muyu.data.processing.mapper.DataProcessingMapper;
|
||||||
|
@ -29,9 +29,23 @@ public class DataProcessingServiceImpl implements DataProcessingService {
|
||||||
@Resource
|
@Resource
|
||||||
private SessionPool sessionPool;
|
private SessionPool sessionPool;
|
||||||
@Resource
|
@Resource
|
||||||
private CacheManager cacheManager;
|
private CacheUtils cacheUtils;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动或停止实时数据
|
||||||
|
*
|
||||||
|
* @param vin 车辆vin码
|
||||||
|
* @param status 开关状态
|
||||||
|
* @return 返回结果
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String carRealTimeStatus(String vin, Boolean status) {
|
||||||
|
if (status){
|
||||||
|
cacheUtils.setCacheValue(CacheNameEnums.REALTIME.getCode(), vin, "Y");
|
||||||
|
}else {
|
||||||
|
cacheUtils.delCacheValue(CacheNameEnums.REALTIME.getCode(), vin);
|
||||||
|
}
|
||||||
|
return "实时数据状态更改成功,当前状态: " + status;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,8 @@
|
||||||
package com.muyu.data.processing.strategy;
|
package com.muyu.data.processing.strategy;
|
||||||
|
|
||||||
import com.github.yulichang.toolkit.SpringContentUtils;
|
import com.muyu.common.caffeine.utils.CacheUtils;
|
||||||
import com.muyu.data.processing.utils.CacheUtils;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.cache.CacheManager;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
/**
|
/**
|
||||||
* 抽象策略路由
|
* 抽象策略路由
|
||||||
* @Author: 胡杨
|
* @Author: 胡杨
|
||||||
|
|
|
@ -41,10 +41,8 @@ public class BasicStrategy extends abstractStrategyRouter<HashMap<String, BasicD
|
||||||
log.info("开始执行基础校验节点。。。");
|
log.info("开始执行基础校验节点。。。");
|
||||||
basicDataMap.put(CacheNameEnums.STORAGE.getCode(), null);
|
basicDataMap.put(CacheNameEnums.STORAGE.getCode(), null);
|
||||||
CacheNameEnums.getCodes().forEach(code-> {
|
CacheNameEnums.getCodes().forEach(code-> {
|
||||||
// 通过VIN码获取所有事件的缓存信息
|
|
||||||
Object cacheValue = cacheUtils.getCacheValue(code, basicDataMap.get("VIN").getKey());
|
|
||||||
// 如果缓存信息不为空,则说明车辆需要处理该事件
|
// 如果缓存信息不为空,则说明车辆需要处理该事件
|
||||||
if (ObjectUtils.isNotEmpty(cacheValue)){
|
if (ObjectUtils.isNotEmpty(cacheUtils.hasKey(code, basicDataMap.get("VIN").getKey()))){
|
||||||
basicDataMap.put(code, null);
|
basicDataMap.put(code, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue