Skip to content

Commit a3ac265

Browse files
optimize: 缓存path的解析结果,避免性能损耗。
1 parent 63f1fcd commit a3ac265

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

apps/application/flow/step_node/variable_splitting_node/impl/base_variable_splitting_node.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@
88
"""
99
import json
1010
from jsonpath_ng.ext import parse
11+
from common.cache.mem_cache import MemCache
1112

1213
from application.flow.i_step_node import NodeResult
1314
from application.flow.step_node.variable_splitting_node.i_variable_splitting_node import IVariableSplittingNode
1415

16+
jsonpath_expr_cache = MemCache('parse_path', {
17+
'TIMEOUT': 3600, # 缓存有效期为 1 小时
18+
'OPTIONS': {
19+
'MAX_ENTRIES': 1000, # 最多缓存 500 个条目
20+
'CULL_FREQUENCY': 10, # 达到上限时,删除约 1/10 的缓存
21+
},
22+
})
23+
24+
def parse_and_cache(path):
25+
jsonpath_expr = jsonpath_expr_cache.get(path)
26+
if not jsonpath_expr:
27+
jsonpath_expr = parse(path)
28+
jsonpath_expr_cache.set(path, jsonpath_expr)
29+
return jsonpath_expr
1530

1631
def smart_jsonpath_search(data: dict, path: str):
1732
"""
@@ -21,7 +36,7 @@ def smart_jsonpath_search(data: dict, path: str):
2136
- 多个匹配: 返回值的列表
2237
- 无匹配: 返回None
2338
"""
24-
jsonpath_expr = parse(path)
39+
jsonpath_expr = parse_and_cache(path)
2540
matches = jsonpath_expr.find(data)
2641

2742
if not matches:

0 commit comments

Comments
 (0)