Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you cache a compiled class made by Groovy Script Engine?

Tags:

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.

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.

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?

like image 591
Elonoa Avatar asked Aug 06 '26 17:08

Elonoa


1 Answers

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);
like image 111
Sargis Avatar answered Aug 12 '26 14:08

Sargis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!