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. The REST endpoint is:
service: class: net.officefloor.tutorial.constantcachehttpserver.ConstantCacheLogic method: service
The following is the servicing logic to retrieve the data from the caches:
/** Services the request by looking up the cache. */
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.
The @Hello Cache is configured as follows:
managed-object:
source: net.officefloor.cache.constant.ConstantCacheManagedObjectSource
qualifier: net.officefloor.tutorial.constantcachehttpserver.Hello
properties:
qualifier: 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> {
@Override
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;
}
}
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
qualifier: net.officefloor.tutorial.constantcachehttpserver.World
properties:
qualifier: net.officefloor.tutorial.constantcachehttpserver.World
poll.interval: "300000"
managed-object: class: net.officefloor.tutorial.constantcachehttpserver.DatabaseConstantCacheDataRetriever qualifier: net.officefloor.tutorial.constantcachehttpserver.World
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;
@Override
public Map<String, Message> getData() throws Exception {
try (Connection connection = this.dataSource.getConnection()) {
ResultSet result = connection.prepareStatement("SELECT ID, MESSAGE FROM REFERENCE_DATA").executeQuery();
Map<String, Message> data = new HashMap<>();
while (result.next()) {
data.put(result.getString("ID"), new Message(result.getString("MESSAGE")));
}
return data;
}
}
}
To have the ConstantCacheManagedObjectSource available, add the following:
<dependency> <groupId>net.officefloor.cache</groupId> <artifactId>officecache_constant</artifactId> </dependency>
The following demonstrates retrieving data from the Cache instances:
@RegisterExtension
public final MockWoofServerExtension server = new MockWoofServerExtension();
@Test
public void helloWorld() {
MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/cached/1"));
response.assertJson(200, new Message("Hello World"));
}
@Test
public void hiThere() {
MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/cached/2"));
response.assertJson(200, new Message("Hi there"));
}
Return to the tutorials index.