Skip to content

Commit c42e403

Browse files
committed
Add support to Caffeine Cache #833Add support to Caffeine Cache fix #833
1 parent 01c4035 commit c42e403

7 files changed

Lines changed: 445 additions & 3 deletions

File tree

handlebars-caffeine/pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<parent>
6+
<groupId>com.github.jknack</groupId>
7+
<artifactId>handlebars.java</artifactId>
8+
<version>4.3.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<groupId>com.github.jknack</groupId>
13+
<artifactId>handlebars-caffeine</artifactId>
14+
15+
<name>Handlebars Caffeine Cache</name>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.github.jknack</groupId>
20+
<artifactId>handlebars</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>com.github.ben-manes.caffeine</groupId>
26+
<artifactId>caffeine</artifactId>
27+
<version>2.9.2</version>
28+
</dependency>
29+
30+
<!-- Servlet API -->
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>servlet-api</artifactId>
34+
<scope>provided</scope>
35+
</dependency>
36+
37+
<!-- Test dependencies -->
38+
<dependency>
39+
<groupId>com.github.jknack</groupId>
40+
<artifactId>handlebars</artifactId>
41+
<version>${project.version}</version>
42+
<scope>test</scope>
43+
<classifier>tests</classifier>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>ch.qos.logback</groupId>
48+
<artifactId>logback-classic</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>org.mockito</groupId>
60+
<artifactId>mockito-core</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.apache.commons</groupId>
66+
<artifactId>commons-lang3</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.antlr</groupId>
72+
<artifactId>antlr4-runtime</artifactId>
73+
<version>4.9.2</version>
74+
<exclusions>
75+
<exclusion>
76+
<groupId>org.abego.treelayout</groupId>
77+
<artifactId>org.abego.treelayout.core</artifactId>
78+
</exclusion>
79+
</exclusions>
80+
<scope>test</scope>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.apache.commons</groupId>
85+
<artifactId>commons-text</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
</dependencies>
89+
90+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.cache;
19+
20+
import static java.util.Objects.requireNonNull;
21+
22+
import java.io.IOException;
23+
import java.util.function.Function;
24+
25+
import com.github.benmanes.caffeine.cache.Cache;
26+
import com.github.jknack.handlebars.Handlebars;
27+
import com.github.jknack.handlebars.Parser;
28+
import com.github.jknack.handlebars.Template;
29+
import com.github.jknack.handlebars.io.TemplateSource;
30+
31+
/**
32+
* Caffeine template cache for handlebars.
33+
*
34+
* @author edgar
35+
* @since 4.3.0
36+
*/
37+
public class CaffeineTemplateCache implements TemplateCache {
38+
39+
/**
40+
* Internal cache.
41+
*/
42+
private final Cache<TemplateSource, Template> cache;
43+
44+
/**
45+
* Creates a new template cache.
46+
*
47+
* @param cache Cache.
48+
*/
49+
public CaffeineTemplateCache(final Cache<TemplateSource, Template> cache) {
50+
this.cache = requireNonNull(cache, "The cache is required.");
51+
}
52+
53+
@Override public void clear() {
54+
cache.invalidateAll();
55+
}
56+
57+
@Override public void evict(final TemplateSource source) {
58+
cache.invalidate(source);
59+
}
60+
61+
@Override public Template get(final TemplateSource source, final Parser parser) {
62+
return cache.get(source, parseTemplate(parser));
63+
}
64+
65+
/**
66+
* This method does nothing on Caffeine. Better option is to use a loading cache with a
67+
* eviction policy of your choice.
68+
*
69+
* Don't use this method.
70+
*
71+
* @param reload Ignored.
72+
* @return This template cache.
73+
*/
74+
@Override public TemplateCache setReload(final boolean reload) {
75+
// NOOP
76+
return this;
77+
}
78+
79+
private Function<? super TemplateSource, ? extends Template> parseTemplate(final Parser parser) {
80+
return source -> {
81+
try {
82+
return parser.parse(source);
83+
} catch (IOException ex) {
84+
throw Handlebars.Utils.propagate(ex);
85+
}
86+
};
87+
}
88+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)