【问题标题】:How do you cache a compiled class made by Groovy Script Engine?你如何缓存一个由 Groovy Script Engine 生成的编译类?
【发布时间】:2012-04-14 07:14:10
【问题描述】:

我正在从 scala 动态运行 groovy 脚本,但是在调用 Groovy 脚本引擎的 loadScriptByName 方法时编译 groovy 脚本需要一些时间。

自我上次运行应用程序以来,从我的应用程序加载和编译的大多数 groovy 脚本都没有更改,所以我想不必每次都编译它们。

如果它们没有任何更改,是否有任何方法可以存储已编译的 groovy 脚本并从我的应用程序中运行它们?我知道 groovy 脚本引擎缓存编译的脚本如果你在同一个 groovy 脚本上调用 loadScriptBynName 方法两次或更多,但是当我关闭我的应用程序时这些缓存就消失了。也许它是在内存中存储缓存的脚本?

【问题讨论】:

  • GroovyScriptEngine 将已编译的脚本存储在内存中名为 scriptCache 的变量中。

标签: compiler-construction groovy


【解决方案1】:

使用:

String script = ""     // some script
string scriptName = "" // unique script name

GroovyClassLoader groovyClassLoader = GroovyClassLoader() 
GroovyCodeSource gcs = new GroovyCodeSource(script, scriptName, "/app/script");
Class<?> executableClass = groovyClassLoader.parseClass(gcs, true);

【讨论】:

    猜你喜欢
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2015-05-09
    相关资源
    最近更新 更多