|
|
@@ -1,26 +1,37 @@
|
|
|
package com.kym.service.cache;
|
|
|
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import com.kym.entity.admin.EquipmentRelation;
|
|
|
+import com.kym.entity.admin.Station;
|
|
|
+import com.kym.entity.common.RedisKeys;
|
|
|
+import com.kym.service.admin.EquipmentRelationService;
|
|
|
+import com.kym.service.admin.StationService;
|
|
|
+import org.springframework.boot.context.event.ApplicationStartedEvent;
|
|
|
+import org.springframework.context.ApplicationListener;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author skyline
|
|
|
- * @description
|
|
|
+ * @description 缓存
|
|
|
* @date 2023-08-25 22:58
|
|
|
*/
|
|
|
public enum KymCache {
|
|
|
INSTANCE;
|
|
|
|
|
|
+
|
|
|
private static ConcurrentHashMap<String, String> CONNECTOR_ID_SHORT_ID_MAPPING = new ConcurrentHashMap<>();
|
|
|
- private static ConcurrentHashMap<String, String> CONNECTOR_ID_STATION_ID_MAPPING = new ConcurrentHashMap<>();
|
|
|
- private static ConcurrentHashMap<String, String> STATION_MAPPING = new ConcurrentHashMap<>();
|
|
|
- private static ConcurrentHashMap<Long, String> ADMIN_USER_STATION = new ConcurrentHashMap<>();
|
|
|
private static ConcurrentHashMap<Long, List<String>> ADMIN_USER_STATIONS = new ConcurrentHashMap<>();
|
|
|
- private static ConcurrentHashMap<String, String> EQUIPMENT_ID_PARKING_NO_MAPPING = new ConcurrentHashMap<>();
|
|
|
|
|
|
- public void putShort2EquipmentId(Map<String, String> map) {
|
|
|
+
|
|
|
+ public void putConnectorId2ShortId(Map<String, String> map) {
|
|
|
CONNECTOR_ID_SHORT_ID_MAPPING.putAll(map);
|
|
|
+ map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_SHORT_ID + k, v));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -31,20 +42,12 @@ public enum KymCache {
|
|
|
*/
|
|
|
public String getShortIdByEquipmentIdOrConnectorId(String id) {
|
|
|
var connectorId = getConnectorId(id);
|
|
|
- var res = CONNECTOR_ID_SHORT_ID_MAPPING.get(connectorId);
|
|
|
-// if (res == null) {
|
|
|
-// var relation = relationService.lambdaQuery().eq(EquipmentRelation::getEquipmentId, equipmentId).one();
|
|
|
-// if (relation != null) {
|
|
|
-// res = relation.getShortId();
|
|
|
-// CONNECTOR_ID_SHORT_ID_MAPPING.put(equipmentId, res);
|
|
|
-// }
|
|
|
-// }
|
|
|
- return res;
|
|
|
+ return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_SHORT_ID + connectorId);
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void putConnector2Station(Map<String, String> map) {
|
|
|
- CONNECTOR_ID_STATION_ID_MAPPING.putAll(map);
|
|
|
+ public void putConnectorId2StationId(Map<String, String> map) {
|
|
|
+ map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_STATION_ID + k, v));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -55,19 +58,11 @@ public enum KymCache {
|
|
|
*/
|
|
|
public String getStationIdByEquipmentIdOrConnectorId(String id) {
|
|
|
var connectorId = getConnectorId(id);
|
|
|
- var res = CONNECTOR_ID_STATION_ID_MAPPING.get(connectorId);
|
|
|
-// if (res == null) {
|
|
|
-// var relation = relationService.lambdaQuery().eq(EquipmentRelation::getConnectorId, connectorId).one();
|
|
|
-// if (relation != null) {
|
|
|
-// res = relation.getStationId();
|
|
|
-// CONNECTOR_STATION_MAPPING.put(connectorId, res);
|
|
|
-// }
|
|
|
-// }
|
|
|
- return res;
|
|
|
+ return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_STATION_ID + connectorId);
|
|
|
}
|
|
|
|
|
|
public void putStationId2Name(Map<String, String> map) {
|
|
|
- STATION_MAPPING.putAll(map);
|
|
|
+ map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.STATION_ID_TO_NAME + k, v));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -77,7 +72,7 @@ public enum KymCache {
|
|
|
* @return
|
|
|
*/
|
|
|
public String getStationNameById(String stationId) {
|
|
|
- return STATION_MAPPING.get(stationId);
|
|
|
+ return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_TO_NAME + stationId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -86,23 +81,13 @@ public enum KymCache {
|
|
|
* @param map
|
|
|
*/
|
|
|
public void putAdminUser2Stations(Map<Long, List<String>> map) {
|
|
|
- ADMIN_USER_STATIONS.putAll(map);
|
|
|
+ map.forEach((k, v) -> {
|
|
|
+ String key = RedisKeys.ADMIN_USER_STATION_IDS + k;
|
|
|
+ String[] values = v.toArray(new String[0]); // 优化数组创建方式,使用varargs来避免不必要的数组拷贝
|
|
|
+ KymCacheInjector.redisTemplate.opsForSet().add(key, values);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取运营平台操作员有权限的站点ID
|
|
|
- *
|
|
|
- * @param adminUserId
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String getAdminUserStationId(Long adminUserId) {
|
|
|
- var res = ADMIN_USER_STATION.get(adminUserId);
|
|
|
-// if (res == null) {
|
|
|
-// var adminUserStation = adminUserStationService.lambdaQuery().eq(AdminUserStation::getAdminUserId, adminUserId).one();
|
|
|
-// res = adminUserStation == null ? null : adminUserStation.getStationId();
|
|
|
-// }
|
|
|
- return res;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 获取运营平台操作员有权限的站点ID
|
|
|
@@ -111,11 +96,11 @@ public enum KymCache {
|
|
|
* @return
|
|
|
*/
|
|
|
public List<String> getAdminUserStationIds(Long adminUserId) {
|
|
|
- return ADMIN_USER_STATIONS.get(adminUserId);
|
|
|
+ return KymCacheInjector.redisTemplate.opsForSet().members(RedisKeys.ADMIN_USER_STATION_IDS + adminUserId).stream().toList();
|
|
|
}
|
|
|
|
|
|
public void putConnectorId2ParkingNo(Map<String, String> map) {
|
|
|
- EQUIPMENT_ID_PARKING_NO_MAPPING.putAll(map);
|
|
|
+ map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_PARKING_NO + k, v));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -124,7 +109,7 @@ public enum KymCache {
|
|
|
*/
|
|
|
public String getParkNoByEquipmentIdOrConnectorId(String id) {
|
|
|
var connectorId = getConnectorId(id);
|
|
|
- return EQUIPMENT_ID_PARKING_NO_MAPPING.get(connectorId);
|
|
|
+ return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_PARKING_NO + connectorId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -142,4 +127,41 @@ public enum KymCache {
|
|
|
default -> null;
|
|
|
};
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ @Component
|
|
|
+ public static class KymCacheInjector implements ApplicationListener<ApplicationStartedEvent> {
|
|
|
+
|
|
|
+ private static final StringRedisTemplate redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
|
|
|
+
|
|
|
+ private final EquipmentRelationService equipmentRelationService;
|
|
|
+ private final StationService stationService;
|
|
|
+
|
|
|
+
|
|
|
+ private KymCacheInjector(EquipmentRelationService equipmentRelationService, StationService stationService) {
|
|
|
+ this.equipmentRelationService = equipmentRelationService;
|
|
|
+ this.stationService = stationService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onApplicationEvent(ApplicationStartedEvent event) {
|
|
|
+ // 将数据库数据缓存到redis
|
|
|
+ var equipmentRelations = equipmentRelationService.list();
|
|
|
+
|
|
|
+ var connectorId2ShortId = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getShortId));
|
|
|
+ KymCache.INSTANCE.putConnectorId2ShortId(connectorId2ShortId);
|
|
|
+ connectorId2ShortId.forEach((k, v) -> redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_SHORT_ID + k, v));
|
|
|
+
|
|
|
+ var connector2Station = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getStationId));
|
|
|
+ KymCache.INSTANCE.putConnectorId2StationId(connector2Station);
|
|
|
+ connector2Station.forEach((k, v) -> redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_STATION_ID + k, v));
|
|
|
+
|
|
|
+ var stationId2Name = stationService.list().stream().collect(Collectors.toMap(Station::getStationId, Station::getStationName));
|
|
|
+ KymCache.INSTANCE.putStationId2Name(stationId2Name);
|
|
|
+ stationId2Name.forEach((k, v) -> redisTemplate.opsForValue().set(RedisKeys.STATION_ID_TO_NAME + k, v));
|
|
|
+
|
|
|
+ var connectorId2ParkingNo = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getParkingNo));
|
|
|
+ KymCache.INSTANCE.putConnectorId2ParkingNo(connectorId2ParkingNo);
|
|
|
+ connectorId2ParkingNo.forEach((k, v) -> redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_PARKING_NO + k, v));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|