From 4c9af8edd4a3edcb41388a4a85569861722aabf0 Mon Sep 17 00:00:00 2001 From: zhengwr Date: Thu, 11 Jun 2026 12:10:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(metadata):=20P1-4=20CCache=20=E6=8D=A2=20C?= =?UTF-8?q?affeine=20+=20POInfo=20=E7=BC=93=E5=AD=98=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E9=92=A9=E5=AD=90(=E4=BF=AE=20C1=20=E5=9D=913)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../server/common/dataDic/bean/CCache.java | 90 ++++++++++++++++-- .../dataDic/bean/CCacheCaffeineTest.java | 80 ++++++++++++++++ .../metadata/perf/PoInfoCacheInvalidator.java | 75 +++++++++++++++ .../perf/PoInfoCacheInvalidatorTest.java | 91 +++++++++++++++++++ 4 files changed, 330 insertions(+), 6 deletions(-) create mode 100644 platform-legacy-core/src/test/java/com/dsasp/server/common/dataDic/bean/CCacheCaffeineTest.java create mode 100644 platform-metadata/src/main/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidator.java create mode 100644 platform-metadata/src/test/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidatorTest.java diff --git a/platform-legacy-core/src/main/java/com/dsasp/server/common/dataDic/bean/CCache.java b/platform-legacy-core/src/main/java/com/dsasp/server/common/dataDic/bean/CCache.java index 4b63720..ebcb339 100644 --- a/platform-legacy-core/src/main/java/com/dsasp/server/common/dataDic/bean/CCache.java +++ b/platform-legacy-core/src/main/java/com/dsasp/server/common/dataDic/bean/CCache.java @@ -1,26 +1,51 @@ package com.dsasp.server.common.dataDic.bean; import java.util.HashMap; +import java.util.concurrent.TimeUnit; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; /** * eip4-poc 移植替换类:原版为 Adempiere CCache(HashMap + 过期 + CacheMgt 全局注册, - * GPL-2 头)。移植代码只使用其 Map API(get/put/containsKey),故重写为最小实现, - * 去除 CacheInterface/CacheMgt 依赖与 GPL 来源代码。构造器签名保持兼容。 + * GPL-2 头)。移植代码只使用其 Map API(get/put/remove/size/clear)与构造器/getName/reset。 + * + *

P1-4 加固:内部实现由「极简 HashMap 版」改为 Caffeine(语义=进程内本地缓存, + * 接替 CCache 的语义保持不变)。公共签名一行不动——仍 {@code extends HashMap}、 + * 两个构造器、{@code getName}/{@code reset} 原样,故 {@code s_cache.get/put/remove} 等 + * 调用点零改动。HashMap 父类不再承载数据,所有读写经下方 Caffeine {@link #delegate} 代理。 + * + *

容量/过期默认值给合理缺省:默认无过期(元数据缓存语义,靠失效钩子 {@code reset/remove} + * 主动失效,与原 PoC 行为一致);当构造器显式传 {@code expireMinutes>0} 时启用写后过期。 + * {@code maximumSize} 取构造入参 {@code initialCapacity} 与下限 256 的较大值,避免过小驱逐。 + * + *

架构第三节留口:多实例横扩时「走 Spring Cache 抽象换 Redis」。CCache 的公共签名 + * ({@code extends HashMap} + 静态字段直引)锁死了无法在不破坏签名前提下套 Spring Cache + * 抽象,故此处为 Caffeine 直用;与该条款的差距及后续路径见 P1加固实测报告。 */ public class CCache extends HashMap { private static final long serialVersionUID = 1L; + private static final int MIN_MAX_SIZE = 256; + private final String name; + private final transient Cache delegate; + public CCache(String name, int initialCapacity) { - super(initialCapacity); - this.name = name; + this(name, initialCapacity, 0); } public CCache(String name, int initialCapacity, int expireMinutes) { - this(name, initialCapacity); - // PoC:元数据缓存不做过期;如需失效调用 reset() + super(1); + this.name = name; + long maxSize = Math.max(initialCapacity, MIN_MAX_SIZE); + Caffeine builder = Caffeine.newBuilder().maximumSize(maxSize); + if (expireMinutes > 0) { + builder.expireAfterWrite(expireMinutes, TimeUnit.MINUTES); + } + this.delegate = builder.build(); } public String getName() { @@ -32,4 +57,57 @@ public class CCache extends HashMap { clear(); return size; } + + @Override + @SuppressWarnings("unchecked") + public V get(Object key) { + if (key == null) { + return null; + } + return delegate.getIfPresent((K) key); + } + + @Override + public V put(K key, V value) { + if (key == null || value == null) { + return null; + } + V old = delegate.getIfPresent(key); + delegate.put(key, value); + return old; + } + + @Override + @SuppressWarnings("unchecked") + public V remove(Object key) { + if (key == null) { + return null; + } + K k = (K) key; + V old = delegate.getIfPresent(k); + delegate.invalidate(k); + return old; + } + + @Override + @SuppressWarnings("unchecked") + public boolean containsKey(Object key) { + return key != null && delegate.getIfPresent((K) key) != null; + } + + @Override + public int size() { + // asMap().size() 是精确值(estimatedSize 异步,可能滞后);size 用于 reset 返回原大小 + return delegate.asMap().size(); + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public void clear() { + delegate.invalidateAll(); + } } diff --git a/platform-legacy-core/src/test/java/com/dsasp/server/common/dataDic/bean/CCacheCaffeineTest.java b/platform-legacy-core/src/test/java/com/dsasp/server/common/dataDic/bean/CCacheCaffeineTest.java new file mode 100644 index 0000000..8a13ca2 --- /dev/null +++ b/platform-legacy-core/src/test/java/com/dsasp/server/common/dataDic/bean/CCacheCaffeineTest.java @@ -0,0 +1,80 @@ +package com.dsasp.server.common.dataDic.bean; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +/** + * P1-4 单测:CCache 内部换 Caffeine 后,公共签名行为不变。 + * 覆盖 get/put/remove/reset/getName/size/containsKey 与可选写后过期(构造器第三参)。 + * 这些正是移植 call site(AbstractPOJOInfo / Enumerate / SysConfig)实际用到的契约。 + */ +class CCacheCaffeineTest { + + @Test + void put_get_往返() { + CCache c = new CCache<>("POInfo", 200); + assertNull(c.get("k")); + assertNull(c.put("k", "v")); // 首次 put 返回旧值 null + assertEquals("v", c.get("k")); + assertTrue(c.containsKey("k")); + assertEquals(1, c.size()); + } + + @Test + void remove_清除条目() { + CCache c = new CCache<>("TableName", 200); + c.put("e1", "T1"); + assertEquals("T1", c.remove("e1")); // remove 返回被删旧值 + assertNull(c.get("e1")); + assertFalse(c.containsKey("e1")); + assertEquals(0, c.size()); + } + + @Test + void reset_清空并返回原大小() { + CCache c = new CCache<>("POInfo", 200); + c.put("a", "1"); + c.put("b", "2"); + assertEquals(2, c.size()); + int cleared = c.reset(); + assertEquals(2, cleared); + assertEquals(0, c.size()); + assertTrue(c.isEmpty()); + assertNull(c.get("a")); + } + + @Test + void getName_保持构造名() { + assertEquals("POInfo", new CCache<>("POInfo", 200).getName()); + assertEquals("Enumerate", new CCache<>("Enumerate", 2000, 30).getName()); + } + + @Test + void null_键值安全() { + CCache c = new CCache<>("POInfo", 200); + assertNull(c.get(null)); + assertNull(c.put(null, "v")); + assertNull(c.put("k", null)); + assertNull(c.remove(null)); + assertFalse(c.containsKey(null)); + assertEquals(0, c.size()); + } + + @Test + @Timeout(value = 5, unit = TimeUnit.SECONDS) + void 写后过期可配() throws InterruptedException { + // 第三参 expireMinutes>0 启用写后过期;这里用反射不便,改用极短过期不可行(分钟粒度), + // 故只验证「带过期构造器」仍正常读写(语义=本地缓存),过期策略本身由 Caffeine 保证。 + CCache c = new CCache<>("Enumerate", 2000, 30); + c.put("YESNO", "items"); + assertEquals("items", c.get("YESNO")); + assertEquals(1, c.size()); + } +} diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidator.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidator.java new file mode 100644 index 0000000..a420e5f --- /dev/null +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidator.java @@ -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 缓存元数据变更后不失效须重启」的回归修复)。 + * + *

现代层在元数据写路径之后显式调用本钩子失效引擎 POInfo 缓存 + 白名单缓存。 + * 选「显式 evict」而非 AOP 的理由:侵入最小——不引 aspectj 织入、不改 com.dsasp 源码, + * 失效点就在 com.eip4 的元数据写调用点旁,可读可测。 + * + *

失效经引擎既有公共静态钩子 {@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(); + } + } +} diff --git a/platform-metadata/src/test/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidatorTest.java b/platform-metadata/src/test/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidatorTest.java new file mode 100644 index 0000000..3c77b1e --- /dev/null +++ b/platform-metadata/src/test/java/com/eip4/platform/metadata/perf/PoInfoCacheInvalidatorTest.java @@ -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 回归修复)。 + * + *

验证 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) f.get(null)).clear(); + Field idf = AbstractPOJOInfo.class.getDeclaredField("id_cache"); + idf.setAccessible(true); + ((Map) 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 idCache = (Map) idf.get(null); + idCache.put("entity-123", "EIP4PT_ORDER"); + + Field sf = AbstractPOJOInfo.class.getDeclaredField("s_cache"); + sf.setAccessible(true); + @SuppressWarnings("unchecked") + Map sCache = (Map) 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); + // 不抛即通过 + } +}