Quarkus - Quarkus Extension for Spring Cache API

While users are encouraged to use Quarkus annotations for caching, Quarkus nevertheless provides a compatibility layer for Spring Cache annotations in the form of the spring-cache extension.

This guide explains how a Quarkus application can leverage the well known Spring Cache annotations to enable application data caching for their Spring beans.

This technology is considered preview.

In preview, backward compatibility and presence in the ecosystem is not guaranteed. Specific improvements might require to change configuration or APIs and plans to become stable are under way. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible extension statuses, check our FAQ entry.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.6.2+

  • Some familiarity with the Spring DI extension

Creating the Maven project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.7.6.Final:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=spring-cache-quickstart \
  4. -DclassName="org.acme.spring.cache.GreeterResource" \
  5. -Dpath="/greeting" \
  6. -Dextensions="spring-di,spring-cache"
  7. cd spring-cache-quickstart

This command generates a Maven project with a REST endpoint and imports the spring-cache and spring-di extensions.

If you already have your Quarkus project configured, you can add the spring-cache extension to your project by running the following command in your project base directory:

  1. ./mvnw quarkus:add-extension -Dextensions="spring-cache"

This will add the following to your pom.xml:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-spring-cache</artifactId>
  4. </dependency>

Creating the REST API

Let’s start by creating a service which will simulate an extremely slow call to an external meteorological service. Create src/main/java/org/acme/spring/cache/WeatherForecastService.java with the following content:

  1. package org.acme.spring.cache;
  2. import java.time.LocalDate;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class WeatherForecastService {
  6. public String getDailyForecast(LocalDate date, String city) {
  7. try {
  8. Thread.sleep(2000L); (1)
  9. } catch (InterruptedException e) {
  10. Thread.currentThread().interrupt();
  11. }
  12. return date.getDayOfWeek() + " will be " + getDailyResult(date.getDayOfMonth() % 4) + " in " + city;
  13. }
  14. private String getDailyResult(int dayOfMonthModuloFour) {
  15. switch (dayOfMonthModuloFour) {
  16. case 0:
  17. return "sunny";
  18. case 1:
  19. return "cloudy";
  20. case 2:
  21. return "chilly";
  22. case 3:
  23. return "rainy";
  24. default:
  25. throw new IllegalArgumentException();
  26. }
  27. }
  28. }
1This is where the slowness comes from.

We also need a class which contains the response sent to the users when they ask for the next three days weather forecast. Create src/main/java/org/acme/spring/cache/WeatherForecast.java this way:

  1. package org.acme.spring.cache;
  2. import java.util.List;
  3. public class WeatherForecast {
  4. private List<String> dailyForecasts;
  5. private long executionTimeInMs;
  6. public WeatherForecast(List<String> dailyForecasts, long executionTimeInMs) {
  7. this.dailyForecasts = dailyForecasts;
  8. this.executionTimeInMs = executionTimeInMs;
  9. }
  10. public List<String> getDailyForecasts() {
  11. return dailyForecasts;
  12. }
  13. public long getExecutionTimeInMs() {
  14. return executionTimeInMs;
  15. }
  16. }

Now, we just need to update the generated WeatherForecastResource class to use the service and response:

  1. package org.acme.spring.cache;
  2. import java.time.LocalDate;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import javax.inject.Inject;
  6. import javax.ws.rs.GET;
  7. import javax.ws.rs.Path;
  8. import javax.ws.rs.Produces;
  9. import javax.ws.rs.core.MediaType;
  10. import org.jboss.resteasy.annotations.jaxrs.QueryParam;
  11. @Path("/weather")
  12. public class WeatherForecastResource {
  13. @Inject
  14. WeatherForecastService service;
  15. @GET
  16. @Produces(MediaType.APPLICATION_JSON)
  17. public WeatherForecast getForecast(@QueryParam String city, @QueryParam long daysInFuture) { (1)
  18. long executionStart = System.currentTimeMillis();
  19. List<String> dailyForecasts = Arrays.asList(
  20. service.getDailyForecast(LocalDate.now().plusDays(daysInFuture), city),
  21. service.getDailyForecast(LocalDate.now().plusDays(daysInFuture + 1L), city),
  22. service.getDailyForecast(LocalDate.now().plusDays(daysInFuture + 2L), city)
  23. );
  24. long executionEnd = System.currentTimeMillis();
  25. return new WeatherForecast(dailyForecasts, executionEnd - executionStart);
  26. }
  27. }
1If the daysInFuture query parameter is omitted, the three days weather forecast will start from the current day. Otherwise, it will start from the current day plus the daysInFuture value.

We’re all done! Let’s check if everything’s working.

First, run the application using ./mvnw compile quarkus:dev from the project directory.

Then, call [http://localhost:8080/weather?city=Raleigh](http://localhost:8080/weather?city=Raleigh) from a browser. After six long seconds, the application will answer something like this:

  1. {"dailyForecasts":["MONDAY will be cloudy in Raleigh","TUESDAY will be chilly in Raleigh","WEDNESDAY will be rainy in Raleigh"],"executionTimeInMs":6001}

The response content may vary depending on the day you run the code.

You can try calling the same URL again and again, it will always take six seconds to answer.

Enabling the cache

Now that your Quarkus application is up and running, let’s tremendously improve its response time by caching the external meteorological service responses. Update the WeatherForecastService class as follows:

  1. package org.acme.cache;
  2. import java.time.LocalDate;
  3. import org.springframework.cache.annotation.Cacheable;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class WeatherForecastService {
  7. @Cacheable("weather-cache") (1)
  8. public String getDailyForecast(LocalDate date, String city) {
  9. try {
  10. Thread.sleep(2000L);
  11. } catch (InterruptedException e) {
  12. Thread.currentThread().interrupt();
  13. }
  14. return date.getDayOfWeek() + " will be " + getDailyResult(date.getDayOfMonth() % 4) + " in " + city;
  15. }
  16. private String getDailyResult(int dayOfMonthModuloFour) {
  17. switch (dayOfMonthModuloFour) {
  18. case 0:
  19. return "sunny";
  20. case 1:
  21. return "cloudy";
  22. case 2:
  23. return "chilly";
  24. case 3:
  25. return "rainy";
  26. default:
  27. throw new IllegalArgumentException();
  28. }
  29. }
  30. }
1We only added this annotation (and the associated import of course).

Let’s try to call [http://localhost:8080/weather?city=Raleigh](http://localhost:8080/weather?city=Raleigh) again. You’re still waiting a long time before receiving an answer. This is normal since the server just restarted and the cache was empty.

Wait a second! The server restarted by itself after the WeatherForecastService update? Yes, this is one of Quarkus amazing features for developers called live coding.

Now that the cache was loaded during the previous call, try calling the same URL. This time, you should get a super fast answer with an executionTimeInMs value close to 0.

Let’s see what happens if we start from one day in the future using the [http://localhost:8080/weather?city=Raleigh&daysInFuture=1](http://localhost:8080/weather?city=Raleigh&daysInFuture=1) URL. You should get an answer two seconds later since two of the requested days were already loaded in the cache.

You can also try calling the same URL with a different city and see the cache in action again. The first call will take six seconds and the following ones will be answered immediately.

Congratulations! You just added application data caching to your Quarkus application with a single line of code!

Supported features

Quarkus provides compatibility with the following Spring Cache annotations:

  • @Cacheable

  • @CachePut

  • @CacheEvict

Note that in this first version of the Spring Cache annotations extension, not all features of these annotations are supported (with proper errors being logged when trying to use an unsupported feature). However, additional features are planned for future releases.

More Spring guides

Quarkus has more Spring compatibility features. See the following guides for more details: