Make script cache configurable and bounded

Fixes #2539
This commit is contained in:
Igor Motov 2013-01-11 17:53:08 -05:00
parent 6243f8e64d
commit e82f96f1e5
1 changed files with 16 additions and 2 deletions

View File

@ -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<String, CompiledScript> staticCache = ConcurrentCollections.newConcurrentMap();
// TODO expose some cache aspects like expiration and max size
private final Cache<CacheKey, CompiledScript> cache = CacheBuilder.newBuilder().build();
private final Cache<CacheKey, CompiledScript> cache;
private final boolean disableDynamic;
@ -71,9 +72,22 @@ public class ScriptService extends AbstractComponent {
public ScriptService(Settings settings, Environment env, Set<ScriptEngineService> 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<String, ScriptEngineService> builder = ImmutableMap.builder();
for (ScriptEngineService scriptEngine : scriptEngines) {
for (String type : scriptEngine.types()) {