Let's caching server-side-rending template rendering results.
template rendering is more complicated than you think.
freemarker supports template cache, but it not a view.
If your system confront high TPS and try spring-template-cache
build.gradle
dependencies {
...
implementation 'io.github.owen-q:spring-template-cache:1.1'
...
}
@Import({ TemplateViewCacheConfiguration.class })
@Configuration
public class TemplateViewCacheConfig {
// required
@Bean
public CacheableFreeMarkerViewResolver cacheableFreeMarkerViewResolver() {
CacheableFreeMarkerViewResolver resolver = new CacheableFreeMarkerViewResolver();
resolver.setCache(true);
resolver.setSuffix(".ftl");
return resolver;
}
// required
@Bean
public TemplateViewCacheable templateViewCacheable() {
return new CaffeineTemplateViewCache(TemplateViewCacheOptions.builder()
.expireAfterWriteDuration(10L)
.expireAfterWriteDurationUnit(TimeUnit.SECONDS)
.maximumSize(1000L)
.build());
}
}
Application.java
@Import({ TemplateViewCacheConfig.class })
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- caching freemarker rendering results
- other template engines will be supported
- using caffeine for default caching implementation
- caching implementation can be replaced by any caching libraries