From f1fe397df175a7ee809ea28530419116af84af28 Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期二, 31 十二月 2024 17:32:45 +0800
Subject: [PATCH] t_mm_item_result 索引优化

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java |   43 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
index ae18b35..74df6bb 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
@@ -12,14 +12,18 @@
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Vector;
+import java.util.stream.Collectors;
 
 @Slf4j
 public class DllUtils {
 
     private static HashMap<String, URLClassLoader> classLoaderCache = new HashMap<>();
+    private static HashMap<String, Object> classCache = new HashMap<>();
+    private static HashMap<String, Method> classMethodCache = new HashMap<>();
 
     /**
      * @description: 加载dll到指定class下
@@ -147,6 +151,23 @@
             unloadDll(urlClassLoader);
             unloadJar(urlClassLoader);
             classLoaderCache.remove(projectId);
+            removeClassCache(projectId);
+            removeClassMethodCache(projectId);
+        }
+    }
+    public static synchronized void removeClassCache(String projectId) {
+        for (String key : classCache.keySet()) {
+            if (key.startsWith(projectId)) {
+                classCache.remove(key);
+            }
+        }
+    }
+
+    public static synchronized void removeClassMethodCache(String projectId) {
+        for (String key : classMethodCache.keySet()) {
+            if (key.startsWith(projectId)) {
+                classMethodCache.remove(key);
+            }
         }
     }
 
@@ -216,13 +237,23 @@
         } else if (model == null) {
             throw new RuntimeException("模型文件不能为空!");
         } else {
-            URLClassLoader classLoader = DllUtils.getClassLoader(projectId);
-            if (null == classLoader) {
-                throw new RuntimeException("dll未发布,classLoader为null");
+            String classCacheKey = projectId + "_" + model.getClassName();
+            String methodParams = Arrays.stream(model.getParamsArray()).map(e -> e.getName()).collect(Collectors.joining(","));
+            String classMethodCacheKey = classCacheKey + "." + model.getMethodName() + "(" + methodParams + ")";
+            if (classCache.containsKey(classCacheKey) && classMethodCache.containsKey(classMethodCacheKey)) {
+                return (HashMap)classMethodCache.get(classMethodCacheKey).invoke( classCache.get(classCacheKey), paramsValueArray);
+            }else {
+                URLClassLoader classLoader = DllUtils.getClassLoader(projectId);
+                if (null == classLoader) {
+                    throw new RuntimeException("dll未发布,classLoader为null");
+                }
+                Class<?> clazz = classLoader.loadClass(model.getClassName());
+                Object o = clazz.newInstance();
+                Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray());
+                classCache.put(classCacheKey,o);
+                classMethodCache.put(classMethodCacheKey,method);
+                return (HashMap)method.invoke(o, paramsValueArray);
             }
-            Class<?> clazz = classLoader.loadClass(model.getClassName());
-            Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray());
-            return (HashMap)method.invoke(clazz.newInstance(), paramsValueArray);
         }
     }
 }

--
Gitblit v1.9.3