No description, website, or topics provided.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
pom.xml [maven-release-plugin] prepare for next development iteration Feb 25, 2017

Thymeleaf Cache Dialect

A dialect for Thymeleaf that allows you to do partial page caching.

Some parts of our webpage will not change often during the lifetime of the application or is dependent only on a usersession. This dialect will cache the resulting output of the element it is declared on and will replace the element on a cache hit.

Build Status


  • Java 5
  • Thymeleaf 3.0.0+ (3.0.0.RELEASE and its dependencies included)


For Maven and Maven-compatible dependency managers

Add a dependency to your project with the following co-ordinates:

  • GroupId: ch.mfrey.thymeleaf.extras.cache
  • ArtifactId: thymeleaf-cache-dialect
  • Version: ${thymeleaf-cache-dialect.version}


The Cache dialect supports three attributes:

  • cache:name="name" -> Cache by name
  • cache:ttl="60" -> Make cache live 60 sec
  • cache:evict="name" -> Evict cache by name

All attribute values can be an expression.

Add the Cache dialect to your existing Thymeleaf template engine, eg:

ServletContextTemplateResolver templateresolver = new ServletContextTemplateResolver();

templateengine = new TemplateEngine();
templateengine.addDialect(new CacheDialect());		// This line adds the dialect to Thymeleaf

Or, for those using Spring configuration files:

<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
  <property name="templateMode" value="HTML5"/>

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  <property name="templateResolver" ref="templateResolver"/>

  <!-- These lines add the dialect to Thymeleaf -->
  <property name="additionalDialects">
      <beans:bean class="ch.mfrey.thymeleaf.extras.cache.CacheDialect" />


Use it

<!DOCTYPE html>
<html xmlns:th=""
	<!-- Simplest use by name. Cached indefinitely -->
	<ul cache:name="menu+${user_role}">
		<li th:text="${a.title}"></li>
		<li th:each="b : ${}" th:object="${b}">
			<span th:text="*{text}"></span>
				<li th:each="c : *{cs}" th:object="${c}">
					<span th:text="*{text}"></span>
	<!-- Cached for 60 sec -->
	<div cache:name="longCalcValue" cache:ttl="60">
		<span th:text="${a.title}"></span>
	<!-- Evict a cache by name if not resolved to '' -->
	<div cache:evict="${mayResolveIntoACachedName}" cache:name="longCalcValue">
		<span th:text="${a.title}"></span>