
com.mitchellbosecke.pebble.cache.DefaultTemplateLoadingCache Maven / Gradle / Ivy
package com.mitchellbosecke.pebble.cache;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
public class DefaultTemplateLoadingCache implements TemplateLoadingCache {
private final ConcurrentMap> cache = new ConcurrentHashMap<>();
@Override
public PebbleTemplate get(String key, Callable loadingFunction) throws PebbleException {
Future future = cache.get(key);
if (future == null) {
FutureTask futureTask = new FutureTask(loadingFunction);
future = cache.putIfAbsent(key, futureTask);
if (future == null) {
future = futureTask;
futureTask.run();
}
}
try {
return future.get();
} catch (ExecutionException | InterruptedException e) {
if(e.getCause() instanceof PebbleException){
throw (PebbleException)e.getCause();
}
throw new RuntimeException("An error occurred while retrieving from cache");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy