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/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 8f3a1d07e2 [type:optimize] Optimize BodyParamUtils with Caffeine cache
(#5905)
8f3a1d07e2 is described below
commit 8f3a1d07e2e29d68c7db26a44d4085bf085598cf
Author: Zapan Gao <[email protected]>
AuthorDate: Tue Jan 21 16:38:07 2025 +0800
[type:optimize] Optimize BodyParamUtils with Caffeine cache (#5905)
* [type:optimize] Optimize BodyParamUtils with Caffeine cache
- Added Caffeine cache for base type checking
- Improved isBaseType() performance with 5000-entry cache
- Reduced class loading overhead
* [type:fix] Fix for style check.
---------
Co-authored-by: aias00 <[email protected]>
---
.gitignore | 2 ++
.../shenyu/plugin/api/utils/BodyParamUtils.java | 37 ++++++++++++++--------
2 files changed, 26 insertions(+), 13 deletions(-)
diff --git a/.gitignore b/.gitignore
index b9784dc9b2..648f8ec059 100644
--- a/.gitignore
+++ b/.gitignore
@@ -45,3 +45,5 @@ Thumbs.db
# rust ignore
*.lock
+
+.factorypath
diff --git
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java
index 4368e976ea..cda68dc5c5 100644
---
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java
+++
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/BodyParamUtils.java
@@ -17,8 +17,13 @@
package org.apache.shenyu.plugin.api.utils;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -27,12 +32,10 @@ import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.common.utils.ReflectUtils;
import org.springframework.util.LinkedMultiValueMap;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
/**
* Common rpc parameter builder utils.
@@ -41,6 +44,11 @@ public final class BodyParamUtils {
private static final Pattern QUERY_PARAM_PATTERN =
Pattern.compile("([^&=]+)(=?)([^&]+)?");
+ // Caffeine cache with maximum size of 5000
+ private static final Cache<String, Boolean> BASE_TYPE_CACHE =
Caffeine.newBuilder()
+ .maximumSize(5000)
+ .build();
+
private BodyParamUtils() {
}
@@ -127,6 +135,7 @@ public final class BodyParamUtils {
return parameterTypes.startsWith("{") && parameterTypes.endsWith("}");
}
+
/**
* isBaseType.
*
@@ -134,10 +143,12 @@ public final class BodyParamUtils {
* @return whether the base type is.
*/
private static boolean isBaseType(final String paramType) {
- try {
- return ReflectUtils.isPrimitives(ClassUtils.getClass(paramType));
- } catch (ClassNotFoundException e) {
- return false;
- }
+ return BASE_TYPE_CACHE.get(paramType, key -> {
+ try {
+ return ReflectUtils.isPrimitives(ClassUtils.getClass(key));
+ } catch (ClassNotFoundException e) {
+ return false;
+ }
+ });
}
}