Skip to content

Commit

Permalink
Polish disabling caching for Mustache view renderer
Browse files Browse the repository at this point in the history
A follow-up for #1814.

* Add a couple of unit tests to preserve coverage;
* Fix checkstyle warnings
  • Loading branch information
arteam committed Nov 11, 2016
1 parent 2f50545 commit 934af07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Expand Up @@ -3,6 +3,7 @@
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
Expand All @@ -23,7 +24,7 @@
*/
public class MustacheViewRenderer implements ViewRenderer {
private final LoadingCache<Class<? extends View>, MustacheFactory> factories;
private boolean useCache;
private boolean useCache = true;

public MustacheViewRenderer() {
this.factories = CacheBuilder.newBuilder()
Expand All @@ -43,7 +44,8 @@ public boolean isRenderable(View view) {
@Override
public void render(View view, Locale locale, OutputStream output) throws IOException {
try {
final MustacheFactory mustacheFactory = useCache ? factories.get(view.getClass()) : createNewMustacheFactory(view.getClass());
final MustacheFactory mustacheFactory = useCache ? factories.get(view.getClass()) :
createNewMustacheFactory(view.getClass());
final Mustache template = mustacheFactory.compile(view.getTemplateName());
final Charset charset = view.getCharset().orElse(StandardCharsets.UTF_8);
try (OutputStreamWriter writer = new OutputStreamWriter(output, charset)) {
Expand All @@ -56,7 +58,14 @@ public void render(View view, Locale locale, OutputStream output) throws IOExcep

@Override
public void configure(Map<String, String> options) {
useCache = !("false".equals(options.get("cache")));
useCache = Optional.ofNullable(options.get("cache"))
.map(Boolean::parseBoolean)
.orElse(true);
}

@VisibleForTesting
boolean isUseCache() {
return useCache;
}

@Override
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.dropwizard.logging.BootstrapLogging;
import io.dropwizard.views.ViewMessageBodyWriter;
import io.dropwizard.views.ViewRenderer;
Expand Down Expand Up @@ -102,4 +103,18 @@ public void returnsA500ForViewsThatCantCompile() throws Exception {
.isEqualTo(ViewMessageBodyWriter.TEMPLATE_ERROR_MSG);
}
}

@Test
public void cacheByDefault() {
MustacheViewRenderer mustacheViewRenderer = new MustacheViewRenderer();
mustacheViewRenderer.configure(ImmutableMap.of());
assertThat(mustacheViewRenderer.isUseCache()).isTrue();
}

@Test
public void canDisableCache() {
MustacheViewRenderer mustacheViewRenderer = new MustacheViewRenderer();
mustacheViewRenderer.configure(ImmutableMap.of("cache", "false"));
assertThat(mustacheViewRenderer.isUseCache()).isFalse();
}
}

0 comments on commit 934af07

Please sign in to comment.