feature #IAY66T 生命周期实现为单例

This commit is contained in:
DaleLee
2025-02-26 22:40:09 +08:00
parent cbba750da1
commit ecbea88004
6 changed files with 30 additions and 39 deletions

View File

@@ -662,8 +662,8 @@ public class FlowExecutor {
boolean exist = lifeCycleList.stream()
.anyMatch(lifeCycle -> lifeCycle instanceof RuleCacheLifeCycle);
if (!exist) {
RuleCacheLifeCycle ruleCacheLifeCycle = new RuleCacheLifeCycle(capacity);
LifeCycleHolder.addLifeCycle(ruleCacheLifeCycle);
RuleCacheLifeCycle.initIfAbsent(capacity);
LifeCycleHolder.addLifeCycle(RuleCacheLifeCycle.getLifeCycle());
}
// 执行时才解析chain
liteflowConfig.setParseMode(ParseModeEnum.PARSE_ONE_ON_FIRST_EXEC);

View File

@@ -27,7 +27,12 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle {
*/
private final Cache<String, ChainState> cache;
public RuleCacheLifeCycle(int capacity) {
/**
* 实例
*/
private static RuleCacheLifeCycle INSTANCE;
private RuleCacheLifeCycle(int capacity) {
this.cache = Caffeine.newBuilder()
.maximumSize(capacity)
.evictionListener(new ChainRemovalListener())
@@ -146,4 +151,22 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle {
chain.setCompiled(false);
chain.setConditionList(null);
}
/**
* 初始化生命周期实例
* @param capacity 缓存容量
*/
public synchronized static void initIfAbsent(int capacity) {
if (ObjectUtil.isNull(INSTANCE)) {
INSTANCE = new RuleCacheLifeCycle(capacity);
}
}
/**
* 获取生命周期实例
* @return lifeCycle
*/
public static RuleCacheLifeCycle getLifeCycle() {
return INSTANCE;
}
}

View File

@@ -222,15 +222,7 @@ public class RuleCacheTest extends BaseTest {
}
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
List<PostProcessChainExecuteLifeCycle> lifeCycleList
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
return ruleCacheLifeCycle.getCache();
}
}
return null;
return RuleCacheLifeCycle.getLifeCycle().getCache();
}
// 获得淘汰的chain传入淘汰前的chain集合

View File

@@ -215,15 +215,7 @@ public class RuleCacheSolonTest extends BaseTest {
}
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
List<PostProcessChainExecuteLifeCycle> lifeCycleList
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
return ruleCacheLifeCycle.getCache();
}
}
return null;
return RuleCacheLifeCycle.getLifeCycle().getCache();
}
// 获得淘汰的chain传入淘汰前的chain集合

View File

@@ -218,15 +218,7 @@ public class RuleCacheSpringbootTest extends BaseTest {
}
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
List<PostProcessChainExecuteLifeCycle> lifeCycleList
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
return ruleCacheLifeCycle.getCache();
}
}
return null;
return RuleCacheLifeCycle.getLifeCycle().getCache();
}
// 获得淘汰的chain传入淘汰前的chain集合

View File

@@ -215,15 +215,7 @@ public class RuleCacheSpringTest extends BaseTest {
}
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
List<PostProcessChainExecuteLifeCycle> lifeCycleList
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
return ruleCacheLifeCycle.getCache();
}
}
return null;
return RuleCacheLifeCycle.getLifeCycle().getCache();
}
// 获得淘汰的chain传入淘汰前的chain集合