hbs.with(new GuavaTemplateCache( CacheBuilder .from(config.getString("hbs.cache"))
@Override public void evict(final TemplateSource source) { cache.invalidate(key(source)); }
public Builder withCaching() { log.debug("Using caching handlebars"); handlebars.with(new ConcurrentMapTemplateCache()); return this; }
protected Handlebars createHandlebars(UpdatableInjectionContext injectionContext, boolean hotload) { Logger.info("Handlebars template hotloading is %s", hotload ? "enabled" : "disabled"); TemplateCache templateCache = hotload ? NullTemplateCache.INSTANCE : new HighConcurrencyTemplateCache(); return new Handlebars(new CompositeTemplateLoader(injectionContext)).with(templateCache); }
@Override public Template get(final TemplateSource source, final Parser parser) throws IOException { requireNonNull(source, "The source is required."); requireNonNull(parser, "The parser is required."); try { return cache.get(key(source), () -> parser.parse(source)); } catch (ExecutionException ex) { throw launderThrowable(source, ex.getCause()); } }
private String applyTemplateAsString(Dag dag, Dag.Node node, String templateAsString) throws IOException { // strange cache where you need to read the content ('source') before querying ?! // I guess caching means 'template generation caching' final TemplateSource source = new StringTemplateSource("jobNameTemplate", templateAsString); final Template template = this.cache.get(source, this.parser); final Writer writer = new StringWriter(); template.apply(DagAndNode.get(dag, node), writer); writer.flush(); return writer.toString(); } }
@Bean @ConditionalOnMissingBean public TemplateCache templateCache() { return new GuavaTemplateCache(newBuilder().<TemplateSource, Template>build()); } }
private HB() { this.cache = new ConcurrentMapTemplateCache(); this.handlebars = new Handlebars(); this.parser = handlebars.getParserFactory().create(handlebars, "{{", "}}"); } private String applyTemplateAsString(Dag dag, Dag.Node node, String templateAsString) throws IOException {
static Handlebars create(final List<TemplateLoader> templateLoaders, final I18nResolver i18NResolver, final I18nIdentifierFactory i18nIdentifierFactory) { if (templateLoaders.isEmpty()) { throw new SunriseConfigurationException("Could not initialize Handlebars due to missing template loaders configuration", CONFIG_TEMPLATE_LOADERS); } logger.info("Provide Handlebars: template loaders [{}]]", templateLoaders.stream().map(TemplateLoader::getPrefix).collect(joining(", "))); final TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[templateLoaders.size()]); final Handlebars handlebars = new Handlebars() .with(loaders) .with(new HighConcurrencyTemplateCache()) .infiniteLoops(true); handlebars.registerHelper("i18n", new HandlebarsI18nHelper(i18NResolver, i18nIdentifierFactory)); handlebars.registerHelper("cms", new HandlebarsCmsHelper()); handlebars.registerHelper("json", new HandlebarsJsonHelper<>()); return handlebars; }
/** * Constructs a handlebars template engine * * @param resourceRoot the resource root */ public HandlebarsTemplateEngine(String resourceRoot) { TemplateLoader templateLoader = new ClassPathTemplateLoader(); templateLoader.setPrefix(resourceRoot); templateLoader.setSuffix(null); handlebars = new Handlebars(templateLoader); // Set Guava cache. Cache<TemplateSource, Template> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(1000).build(); handlebars = handlebars.with(new GuavaTemplateCache(cache)); }
private static Handlebars handlebars(Env env, Resources resources, CompilerFacade compilers, MarkdownCompiler markdownCompiler) { return new Handlebars() .startDelimiter("[[") .endDelimiter("]]") .registerHelpers(new EachReverseHelperSource()) .registerHelpers(new EachValueHelperSource()) .registerHelpers(new GoogleAnalyticsHelper()) .registerHelpers(new LiveReloadHelper(env.prodMode())) .registerHelpers(new AssetsHelperSource(env.prodMode(), resources, compilers)) .registerHelpers(new WebjarHelperSource(env.prodMode())) .registerHelpers(StringHelpers.class) .infiniteLoops(true) .with(new ConcurrentMapTemplateCache()) .with(new AbstractTemplateLoader() { @Override public TemplateSource sourceAt(String location) throws IOException { Path include = resources.findExistingPath("_includes/" + location); if (include == null) { throw new IOException("Template not found " + location); } String body = resources.sourceFile(include).getSource(); if (MarkdownCompiler.supports(include)) { body = markdownCompiler.compile(body); } return new StringTemplateSource(location, body); } }); }
public HandlebarsTransformer() { config = ConfigUtil.loadYaml("handlebars_config.yml", HandlebarsConfig.class); setPriority(config.priority); addSupportedTypes(WILDCARD_TYPE, TEXT_HTML_TYPE, APPLICATION_FORM_URLENCODED_TYPE, APPLICATION_XHTML_XML_TYPE); if (DependencyProvider.global().get(HANDLE_BARS) == null) { DependencyProvider.global().put(HANDLE_BARS, new HashMap<String, Object>()); } loader = new HiggsTemplateLoader(config); handlebars = new Handlebars(loader); loadHelpers(); if (config.enable_humanize_helper) { HumanizeHelper.register(handlebars); } if (config.enable_jackson_helper) { handlebars.registerHelper("json", Jackson2Helper.INSTANCE); } if (config.enable_markdown_helper) { handlebars.registerHelper("md", MarkdownHelper.INSTANCE); } if (config.cache_templates) { handlebars.with(new HighConcurrencyTemplateCache()); } }
/** * Constructs a handlebars template engine * * @param resourceRoot the resource root */ public HandlebarsTemplateEngine(String resourceRoot) { TemplateLoader templateLoader = new ClassPathTemplateLoader(); templateLoader.setPrefix(resourceRoot); templateLoader.setSuffix(null); handlebars = new Handlebars(templateLoader); // Set Guava cache. Cache<TemplateSource, Template> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(1000).build(); handlebars = handlebars.with(new GuavaTemplateCache(cache)); }
hbs.with(new GuavaTemplateCache( CacheBuilder .from(config.getString("hbs.cache"))