My linux world » Java Spring – Cache

Java Spring - Cache


Contents

Prerequistes

If you use maven, please add this to your pom.xml:

   <!-- spring boot -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-cache</artifactId>
	</dependency>
 
	<!-- use JSR-107 annotations -->
	<dependency>
		<groupId>javax.cache</groupId>
		<artifactId>cache-api</artifactId>
	</dependency>
 
	<!-- use ehcache implementation -->
	<dependency>
		<groupId>org.ehcache</groupId>
		<artifactId>ehcache</artifactId>
	</dependency>

Configuration

SpringApplication.java

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
 
@EnableCaching
@SpringBootApplication
public class SpringApplication {
 
	public static void main(String[] args) {
		new SpringApplicationBuilder()
		.sources(SpringApplication.class)
		.run(args);
	}
}

ehcache3.xml

<config xmlns='http://www.ehcache.org/v3' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
	xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
							http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
	<cache alias="users" uses-template="event-cache">
		<expiry>
			<ttl unit="seconds">600</ttl>
		</expiry>
		<heap unit="entries">200</heap>
		<jsr107:mbeans enable-statistics="true" />
	</cache>
 
	<cache-template name="event-cache">
		<listeners>
			<listener>
				<class>net.rabahi.java.spring.cache.listener.EventLogger</class>
				<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
				<event-ordering-mode>UNORDERED</event-ordering-mode>
				<events-to-fire-on>CREATED</events-to-fire-on>
				<events-to-fire-on>UPDATED</events-to-fire-on>
				<events-to-fire-on>EXPIRED</events-to-fire-on>
				<events-to-fire-on>REMOVED</events-to-fire-on>
				<events-to-fire-on>EVICTED</events-to-fire-on>
			</listener>
		</listeners>
		<resources>
			<heap unit="entries">2000</heap>
			<offheap unit="MB">100</offheap>
		</resources>
	</cache-template>
 
</config>

application.properties

# provide a specific caching configuration
spring.cache.jcache.config=classpath:ehcache3.xml

Use it

Model

Create the following model :

import javax.cache.annotation.CacheResult;
 
import org.springframework.stereotype.Service;
 
import lombok.extern.slf4j.Slf4j;
 
@Service
@Slf4j
public class UserServiceImpl {
 
  @Override
  @CacheResult(cacheName="users")
  public String findById(int id){
    LOGGER.info("Find user by id : {}",id);
    return "User-"+id;
  }
}

Note: the annotation @CacheResult(“users”) refers to the cache defined in ehcache3.xml.

Request example

// Fetch user with id 1
user.findById(1);
 
// Fetch again user with id 1, result will be fetched from cache
user.findById(1);
 
// Fetch user with id 2
user.findById(2);
 
// Fetch again user with id 2, result will be fetched from cache
user.findById(2);

Copyright © 2023 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.

Fork me on GitHub