diff --git a/src/main/java/org/elasticsearch/script/ScriptService.java b/src/main/java/org/elasticsearch/script/ScriptService.java index 55bd291f08b..a9973ad84d8 100644 --- a/src/main/java/org/elasticsearch/script/ScriptService.java +++ b/src/main/java/org/elasticsearch/script/ScriptService.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.env.Environment; import org.elasticsearch.index.cache.field.data.FieldDataCache; @@ -43,6 +44,7 @@ import java.io.InputStreamReader; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; /** * @@ -55,8 +57,7 @@ public class ScriptService extends AbstractComponent { private final ConcurrentMap staticCache = ConcurrentCollections.newConcurrentMap(); - // TODO expose some cache aspects like expiration and max size - private final Cache cache = CacheBuilder.newBuilder().build(); + private final Cache cache; private final boolean disableDynamic; @@ -71,9 +72,22 @@ public class ScriptService extends AbstractComponent { public ScriptService(Settings settings, Environment env, Set scriptEngines) { super(settings); + int cacheMaxSize = componentSettings.getAsInt("cache.max_size", 500); + TimeValue cacheExpire = componentSettings.getAsTime("cache.expire", null); + logger.debug("using script cache with max_size [{}], expire [{}]", cacheMaxSize, cacheExpire); + this.defaultLang = componentSettings.get("default_lang", "mvel"); this.disableDynamic = componentSettings.getAsBoolean("disable_dynamic", false); + CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); + if (cacheMaxSize >= 0) { + cacheBuilder.maximumSize(cacheMaxSize); + } + if (cacheExpire != null) { + cacheBuilder.expireAfterAccess(cacheExpire.nanos(), TimeUnit.NANOSECONDS); + } + this.cache = cacheBuilder.build(); + ImmutableMap.Builder builder = ImmutableMap.builder(); for (ScriptEngineService scriptEngine : scriptEngines) { for (String type : scriptEngine.types()) {