package io.vertx.ext.web.common.template;
import io.vertx.core.Vertx;
import io.vertx.core.shareddata.LocalMap;
import io.vertx.ext.web.common.WebEnvironment;
import io.vertx.ext.web.common.template.impl.TemplateHolder;
import java.util.Objects;
public abstract class CachingTemplateEngine<T> implements TemplateEngine {
private final LocalMap<String, TemplateHolder<T>> cache;
protected String extension;
protected CachingTemplateEngine(Vertx vertx, String ext) {
if (!WebEnvironment.development()) {
cache = vertx.sharedData().getLocalMap("__vertx.web.template.cache");
} else {
cache = null;
}
Objects.requireNonNull(ext);
this.extension = ext.charAt(0) == '.' ? ext : "." + ext;
}
public TemplateHolder<T> getTemplate(String filename) {
if (cache != null) {
return cache.get(filename);
}
return null;
}
public TemplateHolder<T> putTemplate(String filename, TemplateHolder<T> templateHolder) {
if (cache != null) {
return cache.put(filename, templateHolder);
}
return null;
}
protected String adjustLocation(String location) {
if (extension != null) {
if (!location.endsWith(extension)) {
location += extension;
}
}
return location;
}
}