|
| 1 | +/** |
| 2 | + * Copyright (c) 2012-2015 Edgar Espina |
| 3 | + * |
| 4 | + * This file is part of Handlebars.java. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.github.jknack.handlebars.io; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.Charset; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +import com.github.benmanes.caffeine.cache.Cache; |
| 25 | +import com.github.jknack.handlebars.Handlebars; |
| 26 | + |
| 27 | +/** |
| 28 | + * Decorates an existing TemplateLoader with a GuavaCache. |
| 29 | + * This is useful to avoid constantly creating TemplateSources. |
| 30 | + * @author agent |
| 31 | + */ |
| 32 | +public class CaffeineTemplateLoader implements TemplateLoader { |
| 33 | + |
| 34 | + /** |
| 35 | + * never null. |
| 36 | + */ |
| 37 | + private final TemplateLoader delegate; |
| 38 | + /** |
| 39 | + * never null. |
| 40 | + */ |
| 41 | + private final Cache<String, TemplateSource> cache; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param delegate |
| 45 | + * wrappped template loader. |
| 46 | + * @param cache |
| 47 | + * Guava Cache. |
| 48 | + */ |
| 49 | + public CaffeineTemplateLoader(final TemplateLoader delegate, |
| 50 | + final Cache<String, TemplateSource> cache) { |
| 51 | + this.delegate = delegate; |
| 52 | + this.cache = cache; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritDoc} |
| 57 | + */ |
| 58 | + public TemplateSource sourceAt(final String location) { |
| 59 | + return cache.get(location, loadTemplate()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritDoc} |
| 64 | + */ |
| 65 | + public String resolve(final String location) { |
| 66 | + return delegate.resolve(location); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritDoc} |
| 71 | + */ |
| 72 | + public String getPrefix() { |
| 73 | + return delegate.getPrefix(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritDoc} |
| 78 | + */ |
| 79 | + public String getSuffix() { |
| 80 | + return delegate.getSuffix(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritDoc} |
| 85 | + */ |
| 86 | + public void setPrefix(final String prefix) { |
| 87 | + delegate.setPrefix(prefix); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritDoc} |
| 92 | + */ |
| 93 | + public void setSuffix(final String suffix) { |
| 94 | + delegate.setSuffix(suffix); |
| 95 | + } |
| 96 | + |
| 97 | + @Override public void setCharset(final Charset charset) { |
| 98 | + delegate.setCharset(charset); |
| 99 | + } |
| 100 | + |
| 101 | + @Override public Charset getCharset() { |
| 102 | + return delegate.getCharset(); |
| 103 | + } |
| 104 | + |
| 105 | + private Function<String, TemplateSource> loadTemplate() { |
| 106 | + return path -> { |
| 107 | + try { |
| 108 | + return delegate.sourceAt(path); |
| 109 | + } catch (IOException ex) { |
| 110 | + throw Handlebars.Utils.propagate(ex); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | +} |
0 commit comments