如何缓存由Groovy Script Engine编写的编译类? [英] How do you cache a compiled class made by Groovy Script Engine?
问题描述
我从scala动态运行groovy脚本,但是当调用Groovy Script Engine的loadScriptByName方法时,需要一些时间来编译groovy脚本。
I'm dynamically running groovy scripts from scala but it takes some time to compile groovy script when calling loadScriptByName method of Groovy Script Engine.
从我的应用程序加载和编译的大多数groovy脚本从上次运行应用程序后不会改变,所以我想每次编译它们都不是必需的。
Most groovy scripts loaded and compiled from my application aren't changed since the last time I run the application so I guess it isn't neccessary to compile them every time.
有没有办法存储编译的groovy脚本,如果他们没有任何更改,从我的应用程序运行它们?我知道groovy脚本引擎缓存编译脚本如果你在同一个Groovy脚本调用loadScriptBynName方法两次或更多,但这些缓存在我关闭我的应用程序时消失。也许它是在内存上存储缓存脚本?
Is there any way to store compiled groovy scripts and run them from my application if they don't have any changes? I know groovy script engine cache compiled scripts IF you call loadScriptBynName method on a same groovy script twice or more but those cache are gone when I close my application. Maybe it's storing cached scripts on memory?
我真的很新groovy脚本引擎。有人可以帮助我吗?
I'm really new to groovy script engine. Can someone help me?
推荐答案
use:
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);
这篇关于如何缓存由Groovy Script Engine编写的编译类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!