Thymeleaf Cache Dialect

A dialect for Thymeleaf that allows you to do partial page caching. Some parts of our webpage will never change during the lifetime of the application or a usersession, but the part should still be dynamic in the beginning. Working with Thymeleaf 3.0.0+ (3.0.0.RELEASE and its dependencies included)

License

License

Categories

Categories

Leaf Data Databases
GroupId

GroupId

ch.mfrey.thymeleaf.extras.cache
ArtifactId

ArtifactId

thymeleaf-cache-dialect
Last Version

Last Version

3.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Thymeleaf Cache Dialect
A dialect for Thymeleaf that allows you to do partial page caching. Some parts of our webpage will never change during the lifetime of the application or a usersession, but the part should still be dynamic in the beginning. Working with Thymeleaf 3.0.0+ (3.0.0.RELEASE and its dependencies included)
Project URL

Project URL

https://github.com/Antibrumm/thymeleaf-extras-cache-dialect
Source Code Management

Source Code Management

https://github.com/Antibrumm/thymeleaf-extras-cache-dialect.git

Download thymeleaf-cache-dialect

How to add to project

<!-- https://jarcasting.com/artifacts/ch.mfrey.thymeleaf.extras.cache/thymeleaf-cache-dialect/ -->
<dependency>
    <groupId>ch.mfrey.thymeleaf.extras.cache</groupId>
    <artifactId>thymeleaf-cache-dialect</artifactId>
    <version>3.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/ch.mfrey.thymeleaf.extras.cache/thymeleaf-cache-dialect/
implementation 'ch.mfrey.thymeleaf.extras.cache:thymeleaf-cache-dialect:3.0.1'
// https://jarcasting.com/artifacts/ch.mfrey.thymeleaf.extras.cache/thymeleaf-cache-dialect/
implementation ("ch.mfrey.thymeleaf.extras.cache:thymeleaf-cache-dialect:3.0.1")
'ch.mfrey.thymeleaf.extras.cache:thymeleaf-cache-dialect:jar:3.0.1'
<dependency org="ch.mfrey.thymeleaf.extras.cache" name="thymeleaf-cache-dialect" rev="3.0.1">
  <artifact name="thymeleaf-cache-dialect" type="jar" />
</dependency>
@Grapes(
@Grab(group='ch.mfrey.thymeleaf.extras.cache', module='thymeleaf-cache-dialect', version='3.0.1')
)
libraryDependencies += "ch.mfrey.thymeleaf.extras.cache" % "thymeleaf-cache-dialect" % "3.0.1"
[ch.mfrey.thymeleaf.extras.cache/thymeleaf-cache-dialect "3.0.1"]

Dependencies

provided (1)

Group / Artifact Type Version
org.thymeleaf : thymeleaf jar 3.0.3.RELEASE

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
ch.qos.logback : logback-classic jar 1.1.7

Project Modules

There are no modules declared in this project.

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

Requirements

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

Installation

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}
<dependency>
  <groupId>ch.mfrey.thymeleaf.extras.cache</groupId>
  <artifactId>thymeleaf-cache-dialect</artifactId>
  <version>3.0.0</version>
</dependency>

Usage

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();
templateresolver.setTemplateMode("HTML5");

templateengine = new TemplateEngine();
templateengine.setTemplateResolver(templateresolver);
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>

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

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

</bean>

Use it

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

Versions

Version
3.0.1
3.0.0
1.0.1
1.0.0