Spring boot 2 with ehcache 3 (jcache) using h2 Database

codepilot
4 min readApr 19, 2020

--

When you are creating APIs that fetch information from database, it is imperative to devise a caching mechanism to optimize the performance by reducing unnecessary calls to the database. One such standard caching mechanism for java is ehcache. In this blog, we are going to learn how to enable a local cache using ehcache 3 — the implementation of JCache (aka JSR-107) specification. For the purpose of this blog, we will be using an in-memory h2 database.

You can dive right into the code by cloning the repo at the below location on GitHub:

Step 1: First, lets use the cool spring Initializr to kick start a spring boot project :

2. Step 2: Import the project into an IDE of your preference and add the below changes to application.yml (I usually rename my application.properties to application.yml as its readable and looks pretty).

Step 3: Now let’s create an entity. Since we are using an in-memory h2 database, Spring boot automatically creates the tables annotated with @Entity on app start up. Please note the usage of lombok annotations. Create the corresponding repository interface for the entity that enables the use of CRUD operations.

Step 4: Create a bean that runs on start up and loads the data into the User table.

Step 5: Create a controller that invokes a service to fetch the User information.

Step 6: Now create the ehcache 3 configuration xml(ehcache.xml) with the required cache settings. Place this file under src/main/resources. Try to understand the config values like tti(time to idle) and heap. Look into the ehcache documentation for more configurations.

Step 7 : Create a service bean that calls the repository to fetch the user info and caches the information. The @CacheConfig refers to the cache alias specified in the ehcache.xml. The service method for which the response should be cached, must be annotated with Cacheable, preferably with a key. The key specified should match the method parameter name.

Step 8: Tell springboot to use the ehcache configuration by adding it in the application.yml.

cache:
jcache:
config: classpath:ehcache.xml

Step 9: That’s it. Now you should be able to run and validate the application. Build the app using mvn clean install and then run the jar under target using java -jar command or use mvn spring-boot:run to run the app.

Step 10: Two users should already be inserted into the in-memory database on start up. validate this through the h2 console, accessible at http://localhost:8080/h2-console

Step 11: Now, invoke the API using curl.

curl --location --request GET 'http://localhost:8080/api/user/test2'

On the console, this should print the logs present in UserServiceImpl class as below. Invoke the API a second time with the same params and this time you should not see the logs which will confirm that the response is being returned from the cache.

Step 12: Alternatively, you can use JMX to validate the cache operations. Launch jconsole from terminal and navigate to the cache MBeans. It should show you the cache hits. You can repeat the same test for the other user (test2). As you hit the cache the CacheHits value increase in the jconsole.

That’s it. Thanks for reading. If you have any questions, please post in the comments section.

You can get the working version of the code on github :

--

--

codepilot
codepilot

Written by codepilot

Backend Developer, UI Designer, Java Programmer and Data Developer. Love coding and learning.

No responses yet