Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EhCache support #160

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/cache-ehcache/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests if caching with EhCache works
18 changes: 18 additions & 0 deletions framework/cache-ehcache/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.aot.smoke-test'
id 'org.graalvm.buildtools.native'
}

dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("javax.cache:cache-api")
implementation("org.ehcache:ehcache")

testImplementation("org.springframework.boot:spring-boot-starter-test")

appTestImplementation(project(":aot-smoke-test-support"))
appTestImplementation("org.awaitility:awaitility:4.2.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.cache.ehcache;

import java.time.Duration;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
import org.springframework.aot.smoketest.support.junit.ApplicationTest;

import static org.assertj.core.api.Assertions.assertThat;

@ApplicationTest
class CacheEhcacheApplicationAotTests {

@Test
void methodIsCachedOnClasses(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("class.invoke: 1").hasNoLinesContaining("class.invoke: 2");
});
}

@Test
void methodIsCachedOnInterfaces(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("interface.invoke: 1")
.hasNoLinesContaining("interface.invoke: 2");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.cache.ehcache;

import com.example.cache.ehcache.clazz.TestServiceClass;
import com.example.cache.ehcache.iface.TestServiceInterface;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
class CLR implements CommandLineRunner {

private final TestServiceClass testServiceClass;

private final TestServiceInterface testServiceInterface;

public CLR(TestServiceClass testServiceClass, TestServiceInterface testServiceInterface) {
this.testServiceClass = testServiceClass;
this.testServiceInterface = testServiceInterface;
}

@Override
public void run(String... args) {
this.testServiceClass.invoke();
this.testServiceClass.invoke();

this.testServiceInterface.invoke();
this.testServiceInterface.invoke();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.cache.ehcache;

import javax.cache.configuration.MutableConfiguration;

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(proxyBeanMethods = false)
@EnableCaching
class CacheConfiguration {

@Bean
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
return cacheManager -> {
cacheManager.createCache("interface.invoke", cacheConfiguration());
cacheManager.createCache("class.invoke", cacheConfiguration());
};
}

private javax.cache.configuration.Configuration<Object, Object> cacheConfiguration() {
return new MutableConfiguration<>().setStatisticsEnabled(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.cache.ehcache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CacheEhcacheApplication {

public static void main(String[] args) throws InterruptedException {
SpringApplication.run(CacheEhcacheApplication.class, args);
Thread.currentThread().join(); // To be able to measure memory consumption
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.cache.ehcache.clazz;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class TestServiceClass {

private int counter = 1;

@Cacheable(cacheNames = "class.invoke")
public void invoke() {
System.out.printf("class.invoke: %d%n", this.counter);
this.counter++;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.cache.ehcache.iface;

public interface TestServiceInterface {

void invoke();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.cache.ehcache.iface;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
class TestServiceWithInterface implements TestServiceInterface {

private int counter = 1;

@Override
@Cacheable(cacheNames = "interface.invoke")
public void invoke() {
System.out.printf("interface.invoke: %d%n", this.counter);
this.counter++;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.cache.type=jcache
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#reachabilityMetadataVersion=0.2.5
#reachabilityMetadataUrl=
reachabilityMetadataUrl=file:///home/seb/workspace/graalvm-reachability-metadata/metadata
kotlinVersion=1.7.21
javaFormatVersion=0.0.34
nbtVersion=0.9.18
Expand Down