moremind commented on code in PR #6336:
URL: https://github.com/apache/shenyu/pull/6336#discussion_r3253009708
##########
shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/AbstractShenyuPlugin.java:
##########
@@ -118,30 +115,52 @@ public Mono<Void> execute(final ServerWebExchange
exchange, final ShenyuPluginCh
printLog(rule, pluginName);
return doExecute(exchange, chain, selectorData, rule);
}
- // lru map as L1 cache,the cache is enabled by default.
- // if the L1 cache fails to hit, using L2 cache based on trie cache.
- // if the L2 cache fails to hit, execute default strategy.
- RuleData ruleData = obtainRuleDataCacheIfEnabled(path);
- if (Objects.nonNull(ruleData) && Objects.isNull(ruleData.getId())) {
- return handleRuleIfNull(pluginName, exchange, chain);
- }
- ruleData = defaultMatchRule(exchange, rules, path);
- if (Objects.isNull(ruleData)) {
+ RuleData ruleData = twoLevelCacheLookupRule(exchange, rules, path);
+ if (Objects.isNull(ruleData) || StringUtils.isBlank(ruleData.getId()))
{
return handleRuleIfNull(pluginName, exchange, chain);
}
printLog(ruleData, pluginName);
return doExecute(exchange, chain, selectorData, ruleData);
}
+
+ private SelectorData twoLevelCacheLookupSelector(final ServerWebExchange
exchange,
+ final List<SelectorData>
selectors,
+ final String path) {
+ // L1 cache hit: return directly
+ SelectorData cached = obtainSelectorDataCacheIfEnabled(path);
+ if (Objects.nonNull(cached)) {
+ return cached;
+ }
+ // L1 miss: fall through to L2 full matching (also populates L1)
+ return defaultMatchSelector(exchange, selectors, path);
+ }
+
+ private RuleData twoLevelCacheLookupRule(final ServerWebExchange exchange,
+ final List<RuleData> rules,
+ final String path) {
+ // L1 cache hit: return directly
+ RuleData cached = obtainRuleDataCacheIfEnabled(path);
+ if (Objects.nonNull(cached)) {
+ return cached;
+ }
+ // L1 miss: fall through to L2 full matching (also populates L1)
+ return defaultMatchRule(exchange, rules, path);
+ }
protected String getRawPath(final ServerWebExchange exchange) {
return exchange.getRequest().getURI().getRawPath();
}
private void initCacheConfig() {
- if (Objects.isNull(selectorMatchConfig) ||
Objects.isNull(ruleMatchConfig)) {
- ShenyuConfig shenyuConfig =
SpringBeanUtils.getInstance().getBean(ShenyuConfig.class);
- selectorMatchConfig = shenyuConfig.getSelectorMatchCache();
- ruleMatchConfig = shenyuConfig.getRuleMatchCache();
+ if (Objects.nonNull(selectorMatchConfig) &&
Objects.nonNull(ruleMatchConfig)) {
+ return;
+ }
+ synchronized (this) {
Review Comment:
not good refactor
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]