groovy

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


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?


Solution

  • 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);