#
vincentlu
2026-04-07 41e582b9181035a4602e656da9d70f0f9bf343ea
zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/CodeServiceImpl.java
@@ -14,11 +14,13 @@
import com.zy.acs.manager.manager.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@Service("codeService")
@@ -31,14 +33,13 @@
    private static final double STRAIGHT_LINE_SIN_THRESHOLD = 1e-3;
    public static final Map<Long, Code> CODE_ID_CACHE = new HashMap<>();
    public static final Map<String, Code> CODE_DATA_CACHE = new HashMap<>();
    public static final Map<Long, Code> CODE_ID_CACHE = new ConcurrentHashMap<>();
    public static final Map<String, Code> CODE_DATA_CACHE = new ConcurrentHashMap<>();
    @PostConstruct
    public void init() {
        for (Code code : this.list()) {
            CODE_ID_CACHE.put(code.getId(), code);
            CODE_DATA_CACHE.put(code.getData(), code);
            this.cacheCode(code);
        }
        log.info("The code cache was initialized...");
    }
@@ -50,26 +51,65 @@
    @Override
    public Code getCacheById(Long id) {
        if (id == null) {
            return null;
        }
        Code code = CODE_ID_CACHE.get(id);
        if (code == null) {
            code = this.getById(id);
            if (code != null) {
                CODE_ID_CACHE.put(id, code);
                this.cacheCode(code);
            }
        }
        return code;
        return this.copyCode(code);
    }
    @Override
    public Code getCacheByData(String data) {
        if (data == null) {
            return null;
        }
        Code code = CODE_DATA_CACHE.get(data);
        if (code == null) {
            code = this.selectByData(data);
            if (null != code) {
                CODE_DATA_CACHE.put(data, code);
                this.cacheCode(code);
            }
        }
        return code;
        return this.copyCode(code);
    }
    @Override
    public void refreshCacheById(Long codeId) {
        if (codeId == null) {
            return;
        }
        Code latestCode = this.getById(codeId);
        this.evictCacheById(codeId, null);
        if (latestCode != null) {
            this.cacheCode(latestCode);
        }
    }
    @Override
    public void evictCacheById(Long codeId, String codeData) {
        if (codeId != null) {
            CODE_ID_CACHE.remove(codeId);
        }
        if (codeData != null) {
            CODE_DATA_CACHE.remove(codeData);
        }
        List<String> staleKeys = new ArrayList<>();
        for (Map.Entry<String, Code> entry : CODE_DATA_CACHE.entrySet()) {
            Code cachedCode = entry.getValue();
            if (cachedCode != null && Objects.equals(cachedCode.getId(), codeId)) {
                staleKeys.add(entry.getKey());
            }
        }
        for (String staleKey : staleKeys) {
            CODE_DATA_CACHE.remove(staleKey);
        }
    }
    @Override
@@ -80,7 +120,7 @@
        Date now = new Date();
        Set<Long> targets = new HashSet<>(codeIds);
        for (Long codeId : targets) {
            Code code = this.getCacheById(codeId);
            Code code = this.getById(codeId);
            if (null == code) {
                continue;
            }
@@ -92,9 +132,8 @@
                if (!this.updateById(code)) {
                    throw new BusinessException(code.getData() + "拐角标记更新失败");
                }
                CODE_ID_CACHE.put(code.getId(), code);
                CODE_DATA_CACHE.put(code.getData(), code);
            }
            this.refreshCacheById(code.getId());
        }
    }
@@ -284,4 +323,21 @@
        return codeList;
    }
    private void cacheCode(Code code) {
        if (code == null || code.getId() == null || code.getData() == null) {
            return;
        }
        CODE_ID_CACHE.put(code.getId(), this.copyCode(code));
        CODE_DATA_CACHE.put(code.getData(), this.copyCode(code));
    }
    private Code copyCode(Code code) {
        if (code == null) {
            return null;
        }
        Code target = new Code();
        BeanUtils.copyProperties(code, target);
        return target;
    }
}