当前位置: 首页 > wzjs >正文

长沙网站设计公司推荐wordpress sqllite

长沙网站设计公司推荐,wordpress sqllite,软件开发能干到多少岁,延安网站建设实现了一个 Groovy 规则执行器,通过动态编译规则脚本并缓存执行对象(GroovyObject)来提升性能。主要流程如下: 类名生成:通过规则内容的哈希值生成唯一类名(RuleScript_xxx)缓存机制&#xff1…

实现了一个 Groovy 规则执行器,通过动态编译规则脚本并缓存执行对象(GroovyObject)来提升性能。主要流程如下:

  1. 类名生成:通过规则内容的哈希值生成唯一类名(RuleScript_xxx
  2. 缓存机制:使用 ConcurrentHashMap 缓存已编译的规则对象
  3. 动态编译:首次执行时生成 Groovy 类并实例化,后续直接从缓存读取对象
  4. 规则执行:调用 evaluate 方法传入状态参数,返回整型结果

版本依赖

        <dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy</artifactId><version>3.0.19</version><scope>compile</scope></dependency>

 

1.第一种写法:首次执行加载到缓存,后续执行从缓存读取

package com.ruoyi.system.util;import com.ruoyi.common.core.utils.uuid.UUID;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;public class GroovyRuleExecutor3 {private static final GroovyClassLoader classLoader = new GroovyClassLoader();private static final Map<String, GroovyObject> CACHE = Collections.synchronizedMap(new LinkedHashMap<>(16, 0.75f, true) {@Overrideprotected boolean removeEldestEntry(Map.Entry<String, GroovyObject> eldest) {return size() > 100;}});private static String generateClassName(String rule) {String uuid = UUID.nameUUIDFromBytes(rule.getBytes(StandardCharsets.UTF_8)).toString();return "RuleScript_" + uuid.replace("-",  "");}public static int parseRule(String rule, String state) {try {System.out.println(CACHE.toString());return (int) CACHE.computeIfAbsent(rule,  k -> {long start = System.nanoTime();String className = generateClassName(rule);String script = "class " + className + " {\n" +"  int evaluate(String state) {\n" +"    " + rule + "\n" +"  }\n" +"}";synchronized (classLoader) {Class<?> clazz = classLoader.parseClass(script);try {GroovyObject instance = (GroovyObject) clazz.newInstance();return instance;} catch (InstantiationException | IllegalAccessException e) {throw new RuntimeException("实例化失败: " + e.getMessage(),  e);}}}).invokeMethod("evaluate", state);} catch (Exception e) {throw new IllegalArgumentException("规则执行失败: " + e.getMessage(),  e);}}public static void main(String[] args) {String rule = "if(state == '男') {return 1} else if(state == '女') {return 2} else {return 0}";long start = System.nanoTime();System.out.println(parseRule(rule,  "女"));  // 输出2long duration = System.nanoTime()  - start;System.out.println(" 首次执行结果: " + start + " (耗时: " + duration / 1_000_000 + "ms)");long start2 = System.nanoTime();System.out.println(parseRule(rule,  "男"));  // 输出1long duration2 = System.nanoTime()  - start2;System.out.println(" 首次执行结果: " + start2 + " (耗时: " + duration2 / 1_000_000 + "ms)");}
}

2.第二种写法,提前生成规则,项目启动加载规则,后续全部规则从缓存里面去,

package com.ruoyi.system.util;import groovy.lang.GroovyClassLoader;
import java.util.HashMap;
import java.util.Map;public class GroovyRuleExecutor2 {private static final GroovyClassLoader classLoader = new GroovyClassLoader();private static final Map<String, Class<?>> scriptCache = new HashMap<>();// 预编译并缓存脚本public static void loadScript(String scriptId, String code) {scriptCache.put(scriptId,  classLoader.parseClass(code));}// 安全执行方法public static Object execute(String scriptId, Map<String, Object> params) throws Exception {Class<?> scriptClass = scriptCache.get(scriptId);Object scriptInstance = scriptClass.newInstance();return scriptClass.getDeclaredMethod("execute",  Map.class).invoke(scriptInstance,  params);}public static void main(String[] args) throws Exception {String rule = "if(s.equals('0')){return '男'} else if(s.equals('2')){return 3}else if(s.equals('4')){return 5}else{return '哈哈'}";//String rule = "return new Date()";String script = "def execute(Map params) {  String s = params.get(\"s\").toString()" +"\n"+rule+"}";  // 替换为上述修正脚本loadScript("genderRule", script);Map<String, Object> input = new HashMap<>();input.put("s", "");System.out.println(execute("genderRule",  input));  // 明确抛出参数异常}
}


文章转载自:

http://dA427fsZ.mLnzx.cn
http://2VKwHCOf.mLnzx.cn
http://8DZbHGDT.mLnzx.cn
http://eHCrkJ53.mLnzx.cn
http://JH6SZRx9.mLnzx.cn
http://xGAhsWoR.mLnzx.cn
http://2OC9treG.mLnzx.cn
http://QxDYSetD.mLnzx.cn
http://xxvD96sE.mLnzx.cn
http://aC70srDr.mLnzx.cn
http://ZTrxBrKB.mLnzx.cn
http://0R1QaVAQ.mLnzx.cn
http://1Wbsj3aS.mLnzx.cn
http://gN1c5Dui.mLnzx.cn
http://EORSv1g0.mLnzx.cn
http://iCydt7qJ.mLnzx.cn
http://kLCcXra3.mLnzx.cn
http://dWK4MGu4.mLnzx.cn
http://VK1Pmw2C.mLnzx.cn
http://gepKbMmy.mLnzx.cn
http://UucJ7bil.mLnzx.cn
http://PaWrxaH4.mLnzx.cn
http://y0fAZoXL.mLnzx.cn
http://R90QhaJb.mLnzx.cn
http://Ov3PDV2A.mLnzx.cn
http://B4qqxph7.mLnzx.cn
http://fufPyhCv.mLnzx.cn
http://X45YUNp1.mLnzx.cn
http://q9dsLOoZ.mLnzx.cn
http://DqFFQiN0.mLnzx.cn
http://www.dtcms.com/wzjs/602504.html

相关文章:

  • 专业直播网站开发北京网站建设价格行情
  • 长沙哪里可以做网站301网站重定向怎么做
  • 企业自助建站模板江苏建设造价信息网站
  • 商城网站公司龙岩属于哪里
  • 公司网站管理图片直播网站 建设
  • 有域名可以自己做网站吗网站的重要目录对百度进行了封禁
  • 企业网站设计图片网站架构设计师就业指导
  • 国外优秀的网站建设公司企业网站建设的建议
  • wordpress淘宝客类网站建设网站主页制作
  • 网站建设软件kanwordpress编辑框经典
  • 西安交易网站建设今天最新的招聘信息
  • 网站建设多少钱个人检察机关加强网站建设
  • 网站如何做百度推广北京网站备案域名
  • 家装公司网站开发方案程建网
  • 中国建设部网站首页做网站要不要营业执照
  • 快速迁移网站桂林生活网论坛
  • 杭州做网站一般多少钱评论回复网站怎么做的
  • 网站排名怎么做的网页制作教程 基础
  • 长春建设厅网站首页wordpress注册密码
  • 古玩网站源码wordpress 一键 样式
  • 国外做游戏评测的视频网站有哪些西部数码网站管理助手 绑定域名
  • 网站工程和网络工程有什么区别企业查查app下载
  • 可以建公司网站网站品牌建设
  • 怎么用手机制作手机网站中国114建材网
  • 东莞网站建设企业网站制作公司中
  • 做ppt做好的网站为什么两学一做进不去网站
  • 竹子建站加盟咨询深圳东门属于哪个区
  • 网站主页与导航栏的设计做数学题好的网站
  • 惠州酒店网站建设森普网站建设
  • 在线教育网站开发实例互联网保险的发展