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 | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 49 insertions(+), 0 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 61cb7a8..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 @@ -1,6 +1,8 @@ package com.iailab.module.model.mpk.common.utils; import cn.hutool.core.io.FileUtil; +import com.iail.model.IAILModel; +import com.iail.utils.RSAUtils; import com.iailab.module.model.mpk.common.MdkConstant; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -10,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下 @@ -145,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); + } } } @@ -207,4 +230,30 @@ } } + + public static HashMap<String, Object> run(IAILModel model, Object[] paramsValueArray, String projectId) throws Exception { + if (RSAUtils.checkLisenceBean().getCode() != 1) { + throw new SecurityException("Lisence 不可用!"); + } else if (model == null) { + throw new RuntimeException("模型文件不能为空!"); + } else { + 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); + } + } + } } -- Gitblit v1.9.3