mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-14 11:31:46 +08:00
feature #IAY66T 增加缓存
This commit is contained in:
@@ -59,5 +59,9 @@
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -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<String, Chain> 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<String, Chain> {
|
||||
|
||||
@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();
|
||||
}
|
||||
chain.setCompiled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
pom.xml
6
pom.xml
@@ -78,6 +78,7 @@
|
||||
<janino.version>3.1.12</janino.version>
|
||||
<kotlin.version>1.9.23</kotlin.version>
|
||||
<liquor.version>1.3.6</liquor.version>
|
||||
<caffeine.version>2.9.3</caffeine.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -326,6 +327,11 @@
|
||||
<artifactId>liquor-eval</artifactId>
|
||||
<version>${liquor.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
<version>${caffeine.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user