preface

This article mainly introduces the data caching function of Spring Boot application based on Ehcache 3.0. In Spring Boot applications, you can use Spring Caching to quickly get around data Caching.

Related video link, you can watch online and download version:

Free to watch online:

I.bjpowernode.com/courses/96….

Download link (including source information, etc.) :

SpringBoot actual combat -SpringBoot from entry to mastery

Spring Boot is a new version of Spring Boot.

Create a Spring Boot project

The maven dependency files for the created Spring Boot application look like this:

Dependency description:

Spring-boot-starter-cache provides caching support for Spring Boot applications

Ehcache provides the cache implementation of EhCache

Cache-api provides a caching specification based on JSR-107

2. Configure Ehcache

Now, you need to tell Spring Boot where to find the cache configuration file, which needs to be set in the Spring Boot configuration file:

Then use the @enablecaching annotation to enable Spring Boot application caching. You can do this in the main application class:

Next, you need to create an Ehcache configuration file in the classpath, such as the Resources directory:

Finally, we need to define a CacheEventListener to record system operations on cached data. The fastest way to do this is to implement the CacheEventListener interface:

Use the @cacheable annotation

To enable Spring Boot to cache our data, we also need to annotate the business method with the @cacheable annotation, telling Spring Boot that the data generated in the method needs to be added to the cache:

With the above three steps, we have completed Spring Boot’s caching capability. Next, we’ll test the cache in action.

4. Cache test

To test our application, create a simple Restful endpoint that calls PersonService and returns a Person object:

Person is a simple POJO class:

With all of this done, let’s compile and run the application. After the project is successfully started, use the browser to open:http://localhost:8080/persons/1, you should see the following information in your browser page:

At this time, you are observing the log information output by the console:

Since we are requesting the API for the first time, there is no cached data. Therefore, Ehcache creates a cache of data, which can be seen by creating.

We set the cache expiration time to 1 minute (1) in the ehcache.xml file, so within a minute we refresh the browser and see no new log output. After a minute the cache expires, we refresh the browser again and see the following log output:

The first log indicates that the cache has expired. The second log indicates that Ehcache created a new cache.

In this case, the implementation of Spring Boot application caching based on Ehcache is explained in three simple steps. The article focuses on the basic steps and methods of cache implementation, which simplifies the specific business code and can be extended if you are interested.

Author: CSDN Java Ye Zhiqiu link: blog.csdn.net/qq_37996327…