Skip to content

Commit cceb6a0

Browse files
pcoates33pivovarit
authored andcommitted
spring-boot ehcache example added (eugenp#6012)
1 parent e3e1c84 commit cceb6a0

File tree

10 files changed

+260
-0
lines changed

10 files changed

+260
-0
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@
663663
<module>spring-dispatcher-servlet</module>
664664
<module>spring-drools</module>
665665

666+
<module>spring-ehcache</module>
666667
<module>spring-ejb</module>
667668
<module>spring-exceptions</module>
668669

@@ -882,6 +883,7 @@
882883
<module>spring-data-rest</module>
883884
<module>spring-dispatcher-servlet</module>
884885
<module>spring-drools</module>
886+
<module>spring-ehcache</module>
885887
<module>spring-freemarker</module>
886888
<module>persistence-modules/spring-hibernate-3</module>
887889
<module>persistence-modules/spring-hibernate4</module>
@@ -1370,6 +1372,7 @@
13701372
<module>spring-dispatcher-servlet</module>
13711373
<module>spring-drools</module>
13721374

1375+
<module>spring-ehcache</module>
13731376
<module>spring-ejb</module>
13741377
<module>spring-exceptions</module>
13751378

spring-ehcache/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

spring-ehcache/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Spring Ehcache Example Project
2+
3+
A simple example of using ehcache with spring-boot.
4+
5+
### Relevant Articles:
6+
- [Spring Boot Ehcache Example](http://www.baeldung.com/spring-ehcache)
7+

spring-ehcache/checkstyle.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
4+
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
5+
<module name="Checker">
6+
<module name="TreeWalker">
7+
<module name="AvoidStarImport">
8+
<property name="severity" value="warning" />
9+
</module>
10+
</module>
11+
</module>

spring-ehcache/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>spring-ehcache</artifactId>
5+
<version>0.1-SNAPSHOT</version>
6+
<name>spring-ehcache</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<artifactId>parent-boot-2</artifactId>
11+
<groupId>com.baeldung</groupId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-boot-2</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-cache</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>javax.cache</groupId>
28+
<artifactId>cache-api</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.ehcache</groupId>
32+
<artifactId>ehcache</artifactId>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<finalName>spring-ehcache</finalName>
38+
<resources>
39+
<resource>
40+
<directory>src/main/resources</directory>
41+
<filtering>true</filtering>
42+
</resource>
43+
</resources>
44+
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-checkstyle-plugin</artifactId>
53+
<version>${checkstyle-maven-plugin.version}</version>
54+
<configuration>
55+
<configLocation>checkstyle.xml</configLocation>
56+
</configuration>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>check</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
<reporting>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-checkstyle-plugin</artifactId>
73+
<version>${checkstyle-maven-plugin.version}</version>
74+
<configuration>
75+
<configLocation>checkstyle.xml</configLocation>
76+
</configuration>
77+
</plugin>
78+
</plugins>
79+
</reporting>
80+
81+
<properties>
82+
<!-- Maven plugins -->
83+
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
84+
<dependency.locations.enabled>false</dependency.locations.enabled>
85+
</properties>
86+
87+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.cachetest;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.cachetest.config;
2+
3+
import java.math.BigDecimal;
4+
import java.time.Duration;
5+
6+
import javax.cache.CacheManager;
7+
8+
import org.ehcache.config.CacheConfiguration;
9+
import org.ehcache.config.ResourcePools;
10+
import org.ehcache.config.builders.CacheConfigurationBuilder;
11+
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
12+
import org.ehcache.config.builders.ExpiryPolicyBuilder;
13+
import org.ehcache.config.builders.ResourcePoolsBuilder;
14+
import org.ehcache.config.units.EntryUnit;
15+
import org.ehcache.config.units.MemoryUnit;
16+
import org.ehcache.event.EventType;
17+
import org.ehcache.jsr107.Eh107Configuration;
18+
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
19+
import org.springframework.cache.annotation.EnableCaching;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
23+
@Configuration
24+
@EnableCaching
25+
public class CacheConfig {
26+
27+
private static final int ON_HEAP_CACHE_SIZE_ENTRIES = 2;
28+
private static final int OFF_HEAP_CACHE_SIZE_MB = 10;
29+
private static final int CACHE_EXPIRY_SECONDS = 30;
30+
31+
@Bean
32+
public JCacheManagerCustomizer jcacheManagerCustomizer() {
33+
return new JCacheManagerCustomizer() {
34+
35+
@Override
36+
public void customize(CacheManager cacheManager) {
37+
ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
38+
.heap(ON_HEAP_CACHE_SIZE_ENTRIES, EntryUnit.ENTRIES)
39+
.offheap(OFF_HEAP_CACHE_SIZE_MB, MemoryUnit.MB).build();
40+
41+
CacheEventListenerConfigurationBuilder eventLoggerConfig = CacheEventListenerConfigurationBuilder
42+
.newEventListenerConfiguration(new CacheEventLogger(), EventType.CREATED, EventType.EXPIRED)
43+
.unordered().asynchronous();
44+
45+
CacheConfiguration<?, ?> cacheConfiguration = CacheConfigurationBuilder
46+
.newCacheConfigurationBuilder(Long.class, BigDecimal.class, resourcePools)
47+
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(CACHE_EXPIRY_SECONDS)))
48+
.add(eventLoggerConfig).build();
49+
50+
cacheManager.createCache("squareCache",
51+
Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));
52+
53+
}
54+
};
55+
}
56+
57+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.cachetest.config;
2+
3+
import org.ehcache.event.CacheEvent;
4+
import org.ehcache.event.CacheEventListener;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class CacheEventLogger implements CacheEventListener<Object, Object> {
9+
10+
private static final Logger log = LoggerFactory.getLogger(CacheEventLogger.class);
11+
12+
@Override
13+
public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) {
14+
log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(),
15+
cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
16+
}
17+
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.cachetest.rest;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import com.baeldung.cachetest.service.NumberService;
13+
14+
@RestController
15+
@RequestMapping(path = "/number", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
16+
public class NumberController {
17+
18+
private final static Logger log = LoggerFactory.getLogger(NumberController.class);
19+
20+
@Autowired
21+
private NumberService numberService;
22+
23+
@GetMapping(path = "/square/{number}")
24+
public String getThing(@PathVariable Long number) {
25+
log.info("call numberService to square {}", number);
26+
return String.format("{\"square\": %s}", numberService.square(number));
27+
}
28+
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.cachetest.service;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.cache.annotation.Cacheable;
8+
import org.springframework.stereotype.Service;
9+
10+
@Service
11+
public class NumberService {
12+
13+
private final static Logger log = LoggerFactory.getLogger(NumberService.class);
14+
15+
@Cacheable(value = "squareCache", key = "#number", condition = "#number>10")
16+
public BigDecimal square(Long number) {
17+
BigDecimal square = BigDecimal.valueOf(number).multiply(BigDecimal.valueOf(number));
18+
log.info("square of {} is {}", number, square);
19+
return square;
20+
}
21+
22+
}

0 commit comments

Comments
 (0)