修复设备缓存BUG

This commit is contained in:
谷成伟 2024-12-25 10:32:23 +08:00
parent fa5d39677d
commit 16a6ae336e

View File

@ -19,13 +19,12 @@ public class EquipmentCacheImpl implements EquipmentCache {
@Autowired
SysEquipmentMapper sysEquipmentMapper;
private final List<DeviceInfoCache> deviceInfoCaches = Collections.synchronizedList(new ArrayList<>());
/**
* 设备CODE索引用于通过设备CODE访问设备缓存信息
*/
private final ConcurrentHashMap<String, Integer> deviceCodeIndex = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, Integer> deviceIdIndex = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, DeviceInfoCache> deviceCodeIndex = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, DeviceInfoCache> deviceIdIndex = new ConcurrentHashMap<>();
/**
* 初始化设备缓存
@ -44,11 +43,10 @@ public class EquipmentCacheImpl implements EquipmentCache {
deviceInfoCache.setMadeinFactory(equipment.getMadeinFactory());
deviceInfoCache.setParentDeviceId(equipment.getParentEquipmentId());
deviceInfoCache.setIotModelId(equipment.getIotModelId());
deviceInfoCaches.add(deviceInfoCache);
//创建Code索引
deviceCodeIndex.put(deviceInfoCache.getDeviceCode(),i);
deviceCodeIndex.put(deviceInfoCache.getDeviceCode(),deviceInfoCache);
//创建Id索引
deviceIdIndex.put(equipment.getId(),i);
deviceIdIndex.put(equipment.getId(),deviceInfoCache);
}
}
@ -57,8 +55,8 @@ public class EquipmentCacheImpl implements EquipmentCache {
*/
@PreDestroy
private void freeDeviceInfoCaches() {
deviceInfoCaches.clear();
deviceCodeIndex.clear();
deviceIdIndex.clear();
}
@ -68,7 +66,7 @@ public class EquipmentCacheImpl implements EquipmentCache {
*/
@Override
public List<DeviceInfoCache> getDevicesCache() {
return Collections.unmodifiableList(deviceInfoCaches);
return deviceIdIndex.values().stream().toList();
}
@Override
@ -84,49 +82,29 @@ public class EquipmentCacheImpl implements EquipmentCache {
deviceInfoCache.setObjectType(equipment.getObjectType());
deviceInfoCache.setParentDeviceId(equipment.getParentEquipmentId());
deviceInfoCache.setIotModelId(equipment.getIotModelId());
//如果是已经缓存过的设备直接缓存
Integer index = deviceIdIndex.get(deviceId);
if (index != null) {
deviceInfoCaches.set(index, deviceInfoCache);
}
else{
deviceInfoCaches.add(deviceInfoCache);
index = deviceInfoCaches.size() - 1;
deviceCodeIndex.put(deviceInfoCache.getDeviceCode(),index);
deviceIdIndex.put(equipment.getId(),index);
}
deviceIdIndex.put(deviceId,deviceInfoCache);
deviceCodeIndex.put(deviceInfoCache.getDeviceCode(),deviceInfoCache);
}
}
@Override
public DeviceInfoCache getDeviceInfoCacheByCode(String deviceCode) {
Integer index = deviceCodeIndex.get(deviceCode);
if (index != null) {
return deviceInfoCaches.get(index);
}
return null;
return deviceCodeIndex.get(deviceCode);
}
@Override
public DeviceInfoCache getDeviceInfoCacheById(Long deviceId) {
Integer index = deviceIdIndex.get(deviceId);
if (index != null) {
return deviceInfoCaches.get(index);
}
return null;
return deviceIdIndex.get(deviceId);
}
@Override
public void removeDeviceCache(Long deviceId) {
Integer index = deviceIdIndex.get(deviceId);
if (index != null) {
deviceInfoCaches.remove(index);
//重新刷新索引
for (int i = index; i < deviceInfoCaches.size(); i++) {
DeviceInfoCache deviceInfoCache = deviceInfoCaches.get(i);
deviceIdIndex.put(deviceInfoCache.getDeviceId(),i);
deviceCodeIndex.put(deviceInfoCache.getDeviceCode(),i);
}
DeviceInfoCache cache = deviceIdIndex.get(deviceId);
if (cache != null) {
deviceIdIndex.remove(deviceId);
deviceCodeIndex.remove(cache.getDeviceCode());
}
}