You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/24 06:23:37 UTC
[incubator-shenyu] branch master updated: add: add a plugin enum cache, we can quickly get plugin by name in this cache. (#3608)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 2780c1f7b add: add a plugin enum cache, we can quickly get plugin by name in this cache. (#3608)
2780c1f7b is described below
commit 2780c1f7bfa65fcba96b2b4ca7e56c0911a79cfb
Author: wjlonger <73...@qq.com>
AuthorDate: Fri Jun 24 14:23:30 2022 +0800
add: add a plugin enum cache, we can quickly get plugin by name in this cache. (#3608)
Co-authored-by: 吴俊龙 <wu...@zhichubao.com>
---
.../main/java/org/apache/shenyu/common/enums/PluginEnum.java | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java b/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
index 3ec0276c7..ba618caee 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
@@ -19,6 +19,8 @@ package org.apache.shenyu.common.enums;
import java.util.Arrays;
import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
/**
* PluginEnum.
@@ -215,7 +217,11 @@ public enum PluginEnum {
*/
RESPONSE(420, 0, "response");
-
+ /**
+ * When the application starts, the plugin is cached and we can obtained by name.
+ * When there are duplicate plugin names, it can be detected and resolved at compile time.
+ */
+ private static final Map<String, PluginEnum> PLUGIN_ENUM_MAP = Arrays.stream(PluginEnum.values()).collect(Collectors.toMap(plugin -> plugin.name, plugin -> plugin));
private final int code;
@@ -270,9 +276,7 @@ public enum PluginEnum {
* @return plugin enum.
*/
public static PluginEnum getPluginEnumByName(final String name) {
- return Arrays.stream(PluginEnum.values())
- .filter(pluginEnum -> pluginEnum.getName().equals(name))
- .findFirst().orElse(PluginEnum.GLOBAL);
+ return PLUGIN_ENUM_MAP.getOrDefault(name, PluginEnum.GLOBAL);
}
/**