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