feature #IAY66T 缓存初始化配置

This commit is contained in:
DaleLee
2024-11-09 19:56:37 +08:00
parent 513a3f3ed1
commit 917c98cc48
2 changed files with 29 additions and 24 deletions

View File

@@ -20,13 +20,13 @@ import com.yomahub.liteflow.enums.ParseModeEnum;
import com.yomahub.liteflow.exception.*;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.flow.cache.RuleCacheLifeCycle;
import com.yomahub.liteflow.flow.element.Chain;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.flow.element.Rollbackable;
import com.yomahub.liteflow.flow.entity.CmpStep;
import com.yomahub.liteflow.flow.id.IdGeneratorHolder;
import com.yomahub.liteflow.lifecycle.LifeCycleHolder;
import com.yomahub.liteflow.lifecycle.PostProcessFlowExecuteLifeCycle;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.monitor.MonitorFile;
@@ -45,10 +45,7 @@ import com.yomahub.liteflow.thread.ExecutorHelper;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
@@ -103,6 +100,15 @@ public class FlowExecutor {
IdGeneratorHolder.init();
}
// 规则缓存
if (isStart && liteflowConfig.isRuleCacheEnabled()) {
Integer capacity = liteflowConfig.getRuleCacheCapacity();
RuleCacheLifeCycle ruleCacheLifeCycle = new RuleCacheLifeCycle(capacity);
LifeCycleHolder.addLifeCycle(ruleCacheLifeCycle);
// 执行时才解析chain
liteflowConfig.setParseMode(ParseModeEnum.PARSE_ONE_ON_FIRST_EXEC);
}
String ruleSource = liteflowConfig.getRuleSource();
if (StrUtil.isBlank(ruleSource)) {
// 查看有没有Parser的SPI实现

View File

@@ -1,6 +1,5 @@
package com.yomahub.liteflow.flow.cache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
@@ -8,26 +7,23 @@ import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.element.Chain;
import com.yomahub.liteflow.flow.element.Condition;
import com.yomahub.liteflow.lifecycle.PostProcessFlowExecuteLifeCycle;
import com.yomahub.liteflow.slot.Slot;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
/**
* Chain执行前的缓存处理
* @author DaleLee
* @since
*/
public class RuleCachePostProcessFlowExecuteLifeCycle implements PostProcessFlowExecuteLifeCycle {
/**
* 缓存
*/
private final Cache<String, Chain> cache;
public class RuleCacheLifeCycle implements PostProcessFlowExecuteLifeCycle {
// 缓存
private final Cache<String, Object> cache;
// 在缓存中与key关联的虚拟值
private static final Object PRESENT = new Object();
public RuleCachePostProcessFlowExecuteLifeCycle(int capacity) {
public RuleCacheLifeCycle(int capacity) {
this.cache = Caffeine.newBuilder()
.maximumSize(capacity)
.evictionListener(new ChainRemovalListener())
@@ -36,12 +32,13 @@ public class RuleCachePostProcessFlowExecuteLifeCycle implements PostProcessFlow
@Override
public void postProcessBeforeFlowExecute(String chainId, Slot slot) {
Chain chain = FlowBus.getChain(chainId);
if (ObjectUtil.isNull(chain)) {
if (!FlowBus.containChain(chainId)) {
return;
}
// 记录在缓存中
cache.put(chainId, chain);
// 记录chainId在缓存中
// 这里不记录实际的chain是因为chain之后有可能在FlowBus中被移除
// 以FlowBus中实际存在的chain为准
cache.put(chainId, PRESENT);
}
@Override
@@ -52,15 +49,17 @@ public class RuleCachePostProcessFlowExecuteLifeCycle implements PostProcessFlow
/**
* 监听在缓存中被移除的chain
*/
private static class ChainRemovalListener implements RemovalListener<String, Chain> {
private static class ChainRemovalListener implements RemovalListener<String, Object> {
@Override
public void onRemoval(@Nullable String chanId, @Nullable Chain chain, @NonNull RemovalCause removalCause) {
List<Condition> conditionList = chain.getConditionList();
// 清空condition 并将chain设置为未编译
if (CollUtil.isNotEmpty(conditionList)) {
conditionList.clear();
public void onRemoval(@Nullable String chanId, @Nullable Object object, @NonNull RemovalCause removalCause) {
Chain chain = FlowBus.getChain(chanId);
// chain可能已经在FlowBus中被移除了
if (ObjectUtil.isNull(chain)) {
return;
}
// 清空condition并将chain设置为未编译
chain.setConditionList(null);
chain.setCompiled(false);
}
}