From 2903d7c9c2f546a51c47f3d6126beff800bfda8e Mon Sep 17 00:00:00 2001 From: DaleLee <1658850308@qq.com> Date: Sat, 9 Nov 2024 18:20:16 +0800 Subject: [PATCH] =?UTF-8?q?feature=20#IAY66T=20=E5=A2=9E=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteflow-core/pom.xml | 4 ++ ...eCachePostProcessFlowExecuteLifeCycle.java | 67 +++++++++++++++++++ pom.xml | 6 ++ 3 files changed, 77 insertions(+) create mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/flow/cache/RuleCachePostProcessFlowExecuteLifeCycle.java diff --git a/liteflow-core/pom.xml b/liteflow-core/pom.xml index b6da77fe2..5d79cd668 100644 --- a/liteflow-core/pom.xml +++ b/liteflow-core/pom.xml @@ -59,5 +59,9 @@ commons-io commons-io + + com.github.ben-manes.caffeine + caffeine + diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/cache/RuleCachePostProcessFlowExecuteLifeCycle.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/cache/RuleCachePostProcessFlowExecuteLifeCycle.java new file mode 100644 index 000000000..f3a607ff8 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/cache/RuleCachePostProcessFlowExecuteLifeCycle.java @@ -0,0 +1,67 @@ +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; +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 cache; + + public RuleCachePostProcessFlowExecuteLifeCycle(int capacity) { + this.cache = Caffeine.newBuilder() + .maximumSize(capacity) + .evictionListener(new ChainRemovalListener()) + .build(); + } + + @Override + public void postProcessBeforeFlowExecute(String chainId, Slot slot) { + Chain chain = FlowBus.getChain(chainId); + if (ObjectUtil.isNull(chain)) { + return; + } + // 记录在缓存中 + cache.put(chainId, chain); + } + + @Override + public void postProcessAfterFlowExecute(String chainId, Slot slot) { + + } + + /** + * 监听在缓存中被移除的chain + */ + private static class ChainRemovalListener implements RemovalListener { + + @Override + public void onRemoval(@Nullable String chanId, @Nullable Chain chain, @NonNull RemovalCause removalCause) { + List conditionList = chain.getConditionList(); + // 清空condition 并将chain设置为未编译 + if (CollUtil.isNotEmpty(conditionList)) { + conditionList.clear(); + } + chain.setCompiled(false); + } + } +} diff --git a/pom.xml b/pom.xml index 9dc40ddfa..88f203932 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,7 @@ 3.1.12 1.9.23 1.3.6 + 2.9.3 @@ -326,6 +327,11 @@ liquor-eval ${liquor.version} + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} +