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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.eip4.platform.metadata.perf;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.dsasp.server.common.dataDic.bean.AbstractPOJOInfo;
|
||||
import com.dsasp.server.common.dataDic.bean.POJOInfo;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
|
||||
/**
|
||||
* P1-4 单测:POInfo 缓存失效钩子(C1 坑 3 回归修复)。
|
||||
*
|
||||
* <p>验证 evict 调用会同步失效白名单缓存;按 entityId 删除走粗粒度全量失效。
|
||||
* 引擎侧 {@code setChangePOInfo} 的精确清缓存逻辑已由引擎自身保证,此处只验证现代层
|
||||
* 钩子的转发契约(不触 DB/Spring)。
|
||||
*/
|
||||
class PoInfoCacheInvalidatorTest {
|
||||
|
||||
@AfterEach
|
||||
@SuppressWarnings("unchecked")
|
||||
void clearStaticCache() throws Exception {
|
||||
Field f = AbstractPOJOInfo.class.getDeclaredField("s_cache");
|
||||
f.setAccessible(true);
|
||||
((Map<String, POJOInfo>) f.get(null)).clear();
|
||||
Field idf = AbstractPOJOInfo.class.getDeclaredField("id_cache");
|
||||
idf.setAccessible(true);
|
||||
((Map<String, String>) idf.get(null)).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void 按entityId失效_精确表名时只evict该表() {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
|
||||
inv.evictByEntityId("entity-123", "EIP4PT_ORDER");
|
||||
|
||||
verify(whitelist).evict("EIP4PT_ORDER");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 按entityId失效_无表名时全量evict() {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
|
||||
inv.evictByEntityId("entity-123", null);
|
||||
|
||||
verify(whitelist).evictAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void evict钩子调setChangePOInfo清引擎缓存() throws Exception {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
|
||||
// 预置 id_cache[entityId]→tableName 与 s_cache[tableName],模拟缓存已热
|
||||
Field idf = AbstractPOJOInfo.class.getDeclaredField("id_cache");
|
||||
idf.setAccessible(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, String> idCache = (Map<String, String>) idf.get(null);
|
||||
idCache.put("entity-123", "EIP4PT_ORDER");
|
||||
|
||||
Field sf = AbstractPOJOInfo.class.getDeclaredField("s_cache");
|
||||
sf.setAccessible(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, POJOInfo> sCache = (Map<String, POJOInfo>) sf.get(null);
|
||||
sCache.put("EIP4PT_ORDER", mock(POJOInfo.class));
|
||||
|
||||
inv.evictByEntityId("entity-123", "EIP4PT_ORDER");
|
||||
|
||||
// 引擎钩子 setChangePOInfo 应已把 s_cache[tableName] 清掉
|
||||
assertNull(sCache.get("EIP4PT_ORDER"));
|
||||
verify(whitelist).evict("EIP4PT_ORDER");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 空入参不抛异常() {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
inv.evictByTable(null);
|
||||
inv.evictByTable(" ");
|
||||
inv.evictByEntityId(null, null);
|
||||
// 不抛即通过
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user