Fork me on GitHub

Constant Cache Tutorial

This tutorial demonstrates using Cache to provide caching of fairly constant data. In other words, cached data that may be refreshed by occasional reloading from source (e.g. reference data).

It is a simple application to retrieve data from two caches. As such, the configuration for this tutorial is the following:

ConstantCacheHttpServer screen shot.

Tutorial Source

Service logic

The following is the servicing logic to retrieve the data from the caches:

	public void service(@HttpPathParameter("key") String key, ObjectResponse<Message> response,
			@Hello Cache<String, Message> helloCache, @World Cache<String, Message> worldCache) {
		Message helloText = helloCache.get(key);
		Message worldText = worldCache.get(key);
		response.send(new Message(helloText.getText() + " " + worldText.getText()));

Notice that there are two Cache instance injected. The instances are differentiated by the qualifier annotation.

Hello Cache

The @Hello Cache is configured as follows:

	<managed-object source="net.officefloor.cache.constant.ConstantCacheManagedObjectSource"
		<property name="qualifier" value="net.officefloor.tutorial.constantcachehttpserver.Hello" />

	<managed-object class="net.officefloor.tutorial.constantcachehttpserver.StaticConstantCacheDataRetriever" 
		qualifier="net.officefloor.tutorial.constantcachehttpserver.Hello" />

The ConstantCacheManagedObjectSource provides the Cache. The configured property identifies the qualifier for the qualified ConstantCacheDataRetriever dependency.

The ConstantCacheDataRetriever retrieves the data for caching. The configured implementation uses hard coding and is as follows:

public class StaticConstantCacheDataRetriever implements ConstantCacheDataRetriever<String, Message> {

	public Map<String, Message> getData() throws Exception {
		Map<String, Message> data = new HashMap<>();
		data.put("1", new Message("Hello"));
		data.put("2", new Message("Hi"));
		return data;

World Cache

The ConstantCacheDataRetriever may inject dependencies to retrieve data from various sources.

The @World Cache retrieves data from a database. It is configured as follows:

	<managed-object source="net.officefloor.cache.constant.ConstantCacheManagedObjectSource"
		<property name="qualifier" value="net.officefloor.tutorial.constantcachehttpserver.World" />
		<property name="poll.interval" value="300000" />

	<managed-object class="net.officefloor.tutorial.constantcachehttpserver.DatabaseConstantCacheDataRetriever" 
		qualifier="net.officefloor.tutorial.constantcachehttpserver.World" />

	<managed-object source="net.officefloor.jdbc.DataSourceManagedObjectSource">
		<property-file path="" />

Note that the poll interval can be configured to indicate frequency in milliseconds to refresh the data.

The ConstantCacheDataRetriever implementation is as follows:

public class DatabaseConstantCacheDataRetriever implements ConstantCacheDataRetriever<String, Message> {

	private @Dependency DataSource dataSource;

	public Map<String, Message> getData() throws Exception {
		try (Connection connection = this.dataSource.getConnection()) {
			ResultSet result = connection.prepareStatement("SELECT KEY, MESSAGE FROM REFERENCE_DATA").executeQuery();
			Map<String, Message> data = new HashMap<>();
			while ( {
				data.put(result.getString("KEY"), new Message(result.getString("MESSAGE")));
			return data;


To have the ConstantCacheManagedObjectSource available, add the following:



The following demonstrates retrieving data from the Cache instances:

	public final MockWoofServerExtension server = new MockWoofServerExtension();

	public void helloWorld() {
		MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/cached/1"));
		response.assertJson(200, new Message("Hello World"));
	public void hiThere() {
		MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/cached/2"));
		response.assertJson(200, new Message("Hi there"));


The next tutorial covers login via Google sign-in.