spring-boot ehcache example added (#6012)
This commit is contained in:
parent
e3e1c84b24
commit
cceb6a022e
3
pom.xml
3
pom.xml
@ -663,6 +663,7 @@
|
||||
<module>spring-dispatcher-servlet</module>
|
||||
<module>spring-drools</module>
|
||||
|
||||
<module>spring-ehcache</module>
|
||||
<module>spring-ejb</module>
|
||||
<module>spring-exceptions</module>
|
||||
|
||||
@ -882,6 +883,7 @@
|
||||
<module>spring-data-rest</module>
|
||||
<module>spring-dispatcher-servlet</module>
|
||||
<module>spring-drools</module>
|
||||
<module>spring-ehcache</module>
|
||||
<module>spring-freemarker</module>
|
||||
<module>persistence-modules/spring-hibernate-3</module>
|
||||
<module>persistence-modules/spring-hibernate4</module>
|
||||
@ -1370,6 +1372,7 @@
|
||||
<module>spring-dispatcher-servlet</module>
|
||||
<module>spring-drools</module>
|
||||
|
||||
<module>spring-ehcache</module>
|
||||
<module>spring-ejb</module>
|
||||
<module>spring-exceptions</module>
|
||||
|
||||
|
13
spring-ehcache/.gitignore
vendored
Normal file
13
spring-ehcache/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
7
spring-ehcache/README.md
Normal file
7
spring-ehcache/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
## Spring Ehcache Example Project
|
||||
|
||||
A simple example of using ehcache with spring-boot.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring Boot Ehcache Example](http://www.baeldung.com/spring-ehcache)
|
||||
|
11
spring-ehcache/checkstyle.xml
Normal file
11
spring-ehcache/checkstyle.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE module PUBLIC
|
||||
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
|
||||
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
||||
<module name="Checker">
|
||||
<module name="TreeWalker">
|
||||
<module name="AvoidStarImport">
|
||||
<property name="severity" value="warning" />
|
||||
</module>
|
||||
</module>
|
||||
</module>
|
87
spring-ehcache/pom.xml
Normal file
87
spring-ehcache/pom.xml
Normal file
@ -0,0 +1,87 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-ehcache</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-ehcache</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-ehcache</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>${checkstyle-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<configLocation>checkstyle.xml</configLocation>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>${checkstyle-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<configLocation>checkstyle.xml</configLocation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
<properties>
|
||||
<!-- Maven plugins -->
|
||||
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
|
||||
<dependency.locations.enabled>false</dependency.locations.enabled>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.cachetest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.baeldung.cachetest.config;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
|
||||
import javax.cache.CacheManager;
|
||||
|
||||
import org.ehcache.config.CacheConfiguration;
|
||||
import org.ehcache.config.ResourcePools;
|
||||
import org.ehcache.config.builders.CacheConfigurationBuilder;
|
||||
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
|
||||
import org.ehcache.config.builders.ExpiryPolicyBuilder;
|
||||
import org.ehcache.config.builders.ResourcePoolsBuilder;
|
||||
import org.ehcache.config.units.EntryUnit;
|
||||
import org.ehcache.config.units.MemoryUnit;
|
||||
import org.ehcache.event.EventType;
|
||||
import org.ehcache.jsr107.Eh107Configuration;
|
||||
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CacheConfig {
|
||||
|
||||
private static final int ON_HEAP_CACHE_SIZE_ENTRIES = 2;
|
||||
private static final int OFF_HEAP_CACHE_SIZE_MB = 10;
|
||||
private static final int CACHE_EXPIRY_SECONDS = 30;
|
||||
|
||||
@Bean
|
||||
public JCacheManagerCustomizer jcacheManagerCustomizer() {
|
||||
return new JCacheManagerCustomizer() {
|
||||
|
||||
@Override
|
||||
public void customize(CacheManager cacheManager) {
|
||||
ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
|
||||
.heap(ON_HEAP_CACHE_SIZE_ENTRIES, EntryUnit.ENTRIES)
|
||||
.offheap(OFF_HEAP_CACHE_SIZE_MB, MemoryUnit.MB).build();
|
||||
|
||||
CacheEventListenerConfigurationBuilder eventLoggerConfig = CacheEventListenerConfigurationBuilder
|
||||
.newEventListenerConfiguration(new CacheEventLogger(), EventType.CREATED, EventType.EXPIRED)
|
||||
.unordered().asynchronous();
|
||||
|
||||
CacheConfiguration<?, ?> cacheConfiguration = CacheConfigurationBuilder
|
||||
.newCacheConfigurationBuilder(Long.class, BigDecimal.class, resourcePools)
|
||||
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(CACHE_EXPIRY_SECONDS)))
|
||||
.add(eventLoggerConfig).build();
|
||||
|
||||
cacheManager.createCache("squareCache",
|
||||
Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.cachetest.config;
|
||||
|
||||
import org.ehcache.event.CacheEvent;
|
||||
import org.ehcache.event.CacheEventListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CacheEventLogger implements CacheEventListener<Object, Object> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CacheEventLogger.class);
|
||||
|
||||
@Override
|
||||
public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) {
|
||||
log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(),
|
||||
cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.cachetest.rest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.cachetest.service.NumberService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/number", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public class NumberController {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(NumberController.class);
|
||||
|
||||
@Autowired
|
||||
private NumberService numberService;
|
||||
|
||||
@GetMapping(path = "/square/{number}")
|
||||
public String getThing(@PathVariable Long number) {
|
||||
log.info("call numberService to square {}", number);
|
||||
return String.format("{\"square\": %s}", numberService.square(number));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.cachetest.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class NumberService {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(NumberService.class);
|
||||
|
||||
@Cacheable(value = "squareCache", key = "#number", condition = "#number>10")
|
||||
public BigDecimal square(Long number) {
|
||||
BigDecimal square = BigDecimal.valueOf(number).multiply(BigDecimal.valueOf(number));
|
||||
log.info("square of {} is {}", number, square);
|
||||
return square;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user