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:
@@ -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。
|
||||
*
|
||||
* <p>P1-4 加固:内部实现由「极简 HashMap 版」改为 <b>Caffeine</b>(语义=进程内本地缓存,
|
||||
* 接替 CCache 的语义保持不变)。<b>公共签名一行不动</b>——仍 {@code extends HashMap}、
|
||||
* 两个构造器、{@code getName}/{@code reset} 原样,故 {@code s_cache.get/put/remove} 等
|
||||
* 调用点零改动。HashMap 父类不再承载数据,所有读写经下方 Caffeine {@link #delegate} 代理。
|
||||
*
|
||||
* <p>容量/过期默认值给合理缺省:默认无过期(元数据缓存语义,靠失效钩子 {@code reset/remove}
|
||||
* 主动失效,与原 PoC 行为一致);当构造器显式传 {@code expireMinutes>0} 时启用写后过期。
|
||||
* {@code maximumSize} 取构造入参 {@code initialCapacity} 与下限 256 的较大值,避免过小驱逐。
|
||||
*
|
||||
* <p>架构第三节留口:多实例横扩时「走 Spring Cache 抽象换 Redis」。CCache 的公共签名
|
||||
* ({@code extends HashMap} + 静态字段直引)锁死了无法在不破坏签名前提下套 Spring Cache
|
||||
* 抽象,故此处为 Caffeine 直用;与该条款的差距及后续路径见 P1加固实测报告。
|
||||
*/
|
||||
public class CCache<K, V> extends HashMap<K, V> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final int MIN_MAX_SIZE = 256;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final transient Cache<K, V> 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<Object, Object> 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<K, V> extends HashMap<K, V> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user