1. Overview Let’s look at an example of using Ehcache with Spring Boot. We’ll use Ehcache version 3 as this provides an implementation of a JSR-107 cache manager. The example is a simple REST service that produces the square of a number.

How to enable caching in Spring Boot using Maven?

We can simply enable the caching by annotating the class with @EnableCaching. We can run the spring boot application using the following maven command. The previous application prints the following output to the console.

What is ehehcache version 3?

Ehcache version 3 is an implementation of a JSR-107 cache manager. We need following dependencies to add caching capability.

How do I access the cache of a spring component?

In those cases, we can use org.springframework.cache.CacheManager and org.springframework.cache.Cache abstraction to access and utilize ehcache for adding and accessing cache entries. To use CacheManager, we must first autowire into a spring component and the use it’s getCache (name) method to get the cache instance by it’s name.

What is Ehcache and how to use it?

The current version of Ehcache is 3. It provides the implementation of the JSR-107 cache manager. We can use it directly. It is fast, lightweight, Scalable, and Flexible. It stores the cache in memory and disk (SSD). It depends on SLF4J for logging. It supports distributed caching via JGroups or JMS and RMI.

What is writewrite-through caching?

Write-through caching is a caching pattern where writes to the cache cause writes to an underlying resource. The cache acts as a facade to the underlying resource. With this pattern, it often makes sense to read through the cache too.

How do I enable annotation-driven cache management in spring?

Cache Configuration We need to add Spring’s @EnableCaching annotation to a Spring bean so that Spring’s annotation-driven cache management is enabled. Let’s create a CacheConfig class: Spring’s auto-configuration finds Ehcache’s implementation of JSR-107. However, no caches are created by default.