feat(metadata): P1-4 CCache 换 Caffeine + POInfo 缓存失效钩子(修 C1 坑3)
CCache 内部由极简 HashMap 换 Caffeine(版本走 Spring Boot BOM),公共签名一行不动: 仍 extends HashMap + 双构造器 + getName/reset,get/put/remove/size/clear 覆写代理到 内部 Caffeine Cache,4 处 call site 零改动全部受益。 PoInfoCacheInvalidator 现代层显式 evict(不改 com.dsasp):先 getPOInfo 装载映射再调 引擎公共钩子 setChangePOInfo,保证冷态命中——修复 C1 坑3「元数据变更不失效须重启」。 单测 10 个(CCache 行为 6 + 失效钩子 4)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.eip4.platform.metadata.perf;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.dsasp.server.common.dataDic.bean.AbstractPOJOInfo;
|
||||
import com.dsasp.server.common.dataDic.bean.POJOInfo;
|
||||
import com.dsasp.server.dataDic.bean.POInfo;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
|
||||
/**
|
||||
* POInfo 缓存失效钩子(P1-4,迁移手册 C1 坑 3「POInfo 缓存元数据变更后不失效须重启」的回归修复)。
|
||||
*
|
||||
* <p>现代层在元数据写路径之后<b>显式</b>调用本钩子失效引擎 POInfo 缓存 + 白名单缓存。
|
||||
* 选「显式 evict」而非 AOP 的理由:侵入最小——不引 aspectj 织入、<b>不改 com.dsasp 源码</b>,
|
||||
* 失效点就在 com.eip4 的元数据写调用点旁,可读可测。
|
||||
*
|
||||
* <p>失效经引擎既有公共静态钩子 {@link AbstractPOJOInfo#setChangePOInfo(String)}
|
||||
* (它清 {@code id_cache[entityId]} 并据此清 {@code s_cache[tableName]})。该钩子依赖
|
||||
* {@code id_cache} 已装载映射;为保证缓存冷态下也能命中,本类先 {@code POInfo.getPOInfo(table)}
|
||||
* 把映射装载(顺带拿到 entityId),再调钩子——全程只用引擎公共 API,保真层零修改。
|
||||
*/
|
||||
@Component
|
||||
public class PoInfoCacheInvalidator {
|
||||
|
||||
private final IdentifierWhitelist identifierWhitelist;
|
||||
|
||||
public PoInfoCacheInvalidator(IdentifierWhitelist identifierWhitelist) {
|
||||
this.identifierWhitelist = identifierWhitelist;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按表名失效该实体的 POInfo 缓存与白名单缓存。
|
||||
* 元数据「创建实体 / 修改属性」后调用,使后续读立即拿到新元数据,无需重启。
|
||||
*
|
||||
* @param table 实体表名(任意大小写)
|
||||
*/
|
||||
public void evictByTable(String table) {
|
||||
if (table == null || table.isBlank()) {
|
||||
return;
|
||||
}
|
||||
String upper = table.toUpperCase();
|
||||
try {
|
||||
// 装载映射并拿 entityId(getPOInfo 顺带把 id_cache 填好,保证下方钩子能命中)
|
||||
POJOInfo info = POInfo.getPOInfo(upper);
|
||||
if (info != null) {
|
||||
String entityId = info.getAD_Table_ID();
|
||||
if (entityId != null && !entityId.isBlank()) {
|
||||
AbstractPOJOInfo.setChangePOInfo(entityId);
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException ignore) {
|
||||
// 表已删除 / 元数据查不到时 getPOInfo 可能抛异常;此处失效是「尽力而为」,
|
||||
// 白名单缓存仍按表名失效(下行),不让引擎侧异常阻断现代层流程。
|
||||
}
|
||||
identifierWhitelist.evict(upper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 entityId 失效(删除实体等已知 entityId 的场景,直接走引擎钩子)。
|
||||
* 同时全量失效白名单缓存(删除是粗粒度变更,表名可能已不可查)。
|
||||
*
|
||||
* @param entityId SYS_ENTITY 主键
|
||||
* @param table 被变更的表名(用于精确失效白名单缓存,可空)
|
||||
*/
|
||||
public void evictByEntityId(String entityId, String table) {
|
||||
if (entityId != null && !entityId.isBlank()) {
|
||||
AbstractPOJOInfo.setChangePOInfo(entityId);
|
||||
}
|
||||
if (table != null && !table.isBlank()) {
|
||||
identifierWhitelist.evict(table);
|
||||
} else {
|
||||
identifierWhitelist.evictAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user