File tree Expand file tree Collapse file tree 11 files changed +163
-1
lines changed
appTest/java/com/example/cache/ehcache
java/com/example/cache/ehcache Expand file tree Collapse file tree 11 files changed +163
-1
lines changed Original file line number Diff line number Diff line change 1+ Tests if caching with EhCache works
Original file line number Diff line number Diff line change 1+ plugins {
2+ id ' java'
3+ id ' org.springframework.boot'
4+ id ' org.springframework.aot.smoke-test'
5+ id ' org.graalvm.buildtools.native'
6+ }
7+
8+ dependencies {
9+ implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin . BOM_COORDINATES ))
10+ implementation(" org.springframework.boot:spring-boot-starter-cache" )
11+ implementation(" javax.cache:cache-api" )
12+ implementation(" org.ehcache:ehcache" )
13+
14+ testImplementation(" org.springframework.boot:spring-boot-starter-test" )
15+
16+ appTestImplementation(project(" :aot-smoke-test-support" ))
17+ appTestImplementation(" org.awaitility:awaitility:4.2.0" )
18+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache ;
2+
3+ import java .time .Duration ;
4+
5+ import org .awaitility .Awaitility ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ import org .springframework .aot .smoketest .support .assertj .AssertableOutput ;
9+ import org .springframework .aot .smoketest .support .junit .ApplicationTest ;
10+
11+ import static org .assertj .core .api .Assertions .assertThat ;
12+
13+ @ ApplicationTest
14+ class CacheEhcacheApplicationAotTests {
15+
16+ @ Test
17+ void methodIsCachedOnClasses (AssertableOutput output ) {
18+ Awaitility .await ().atMost (Duration .ofSeconds (10 )).untilAsserted (() -> {
19+ assertThat (output ).hasSingleLineContaining ("class.invoke: 1" ).hasNoLinesContaining ("class.invoke: 2" );
20+ });
21+ }
22+
23+ @ Test
24+ void methodIsCachedOnInterfaces (AssertableOutput output ) {
25+ Awaitility .await ().atMost (Duration .ofSeconds (10 )).untilAsserted (() -> {
26+ assertThat (output ).hasSingleLineContaining ("interface.invoke: 1" )
27+ .hasNoLinesContaining ("interface.invoke: 2" );
28+ });
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache ;
2+
3+ import com .example .cache .ehcache .clazz .TestServiceClass ;
4+ import com .example .cache .ehcache .iface .TestServiceInterface ;
5+
6+ import org .springframework .boot .CommandLineRunner ;
7+ import org .springframework .stereotype .Component ;
8+
9+ @ Component
10+ class CLR implements CommandLineRunner {
11+
12+ private final TestServiceClass testServiceClass ;
13+
14+ private final TestServiceInterface testServiceInterface ;
15+
16+ public CLR (TestServiceClass testServiceClass , TestServiceInterface testServiceInterface ) {
17+ this .testServiceClass = testServiceClass ;
18+ this .testServiceInterface = testServiceInterface ;
19+ }
20+
21+ @ Override
22+ public void run (String ... args ) {
23+ this .testServiceClass .invoke ();
24+ this .testServiceClass .invoke ();
25+
26+ this .testServiceInterface .invoke ();
27+ this .testServiceInterface .invoke ();
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache ;
2+
3+ import javax .cache .configuration .MutableConfiguration ;
4+
5+ import org .springframework .boot .autoconfigure .cache .JCacheManagerCustomizer ;
6+ import org .springframework .cache .annotation .EnableCaching ;
7+ import org .springframework .context .annotation .Bean ;
8+ import org .springframework .context .annotation .Configuration ;
9+
10+ @ Configuration (proxyBeanMethods = false )
11+ @ EnableCaching
12+ class CacheConfiguration {
13+
14+ @ Bean
15+ public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer () {
16+ return cacheManager -> {
17+ cacheManager .createCache ("interface.invoke" , cacheConfiguration ());
18+ cacheManager .createCache ("class.invoke" , cacheConfiguration ());
19+ };
20+ }
21+
22+ private javax .cache .configuration .Configuration <Object , Object > cacheConfiguration () {
23+ return new MutableConfiguration <>().setStatisticsEnabled (true );
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+
6+ @ SpringBootApplication
7+ public class CacheEhcacheApplication {
8+
9+ public static void main (String [] args ) throws InterruptedException {
10+ SpringApplication .run (CacheEhcacheApplication .class , args );
11+ Thread .currentThread ().join (); // To be able to measure memory consumption
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache .clazz ;
2+
3+ import org .springframework .cache .annotation .Cacheable ;
4+ import org .springframework .stereotype .Service ;
5+
6+ @ Service
7+ public class TestServiceClass {
8+
9+ private int counter = 1 ;
10+
11+ @ Cacheable (cacheNames = "class.invoke" )
12+ public void invoke () {
13+ System .out .printf ("class.invoke: %d%n" , this .counter );
14+ this .counter ++;
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache .iface ;
2+
3+ public interface TestServiceInterface {
4+
5+ void invoke ();
6+
7+ }
Original file line number Diff line number Diff line change 1+ package com .example .cache .ehcache .iface ;
2+
3+ import org .springframework .cache .annotation .Cacheable ;
4+ import org .springframework .stereotype .Service ;
5+
6+ @ Service
7+ class TestServiceWithInterface implements TestServiceInterface {
8+
9+ private int counter = 1 ;
10+
11+ @ Override
12+ @ Cacheable (cacheNames = "interface.invoke" )
13+ public void invoke () {
14+ System .out .printf ("interface.invoke: %d%n" , this .counter );
15+ this .counter ++;
16+ }
17+
18+ }
Original file line number Diff line number Diff line change 1+ spring.cache.type =jcache
You can’t perform that action at this time.
0 commit comments