diff --git a/spring/pom.xml b/spring/pom.xml index 96f2b5d66..79c614ecb 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -37,6 +37,7 @@ + spring-boot-caching-hazelcast-cache-manager spring-configuration spring-data-hazelcast-chemistry-sample spring-data-jpa-hazelcast-migration diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/README.adoc b/spring/spring-boot-caching-hazelcast-cache-manager/README.adoc new file mode 100644 index 000000000..10322d79a --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/README.adoc @@ -0,0 +1 @@ +See the link:https://docs.hazelcast.com/tutorials/caching-springboot[tutorial]. \ No newline at end of file diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/pom.xml b/spring/spring-boot-caching-hazelcast-cache-manager/pom.xml new file mode 100644 index 000000000..ce507279e --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.4.0 + + + + com.hazelcast.samples + spring-boot-caching-hazelcast-cache-manager + 0.1-SNAPSHOT + jar + Spring Boot Caching with Hazelcast Cache Manager + + + 5.5.0 + + + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-web + + + + com.hazelcast + hazelcast + ${hazelcast.version} + + + + com.hazelcast + hazelcast-spring + ${hazelcast.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/Application.java b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/Application.java new file mode 100644 index 000000000..9dafb11c6 --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/Application.java @@ -0,0 +1,31 @@ +/* + * + * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.hazelcast.springboot.caching; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/BookController.java b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/BookController.java new file mode 100644 index 000000000..42e2ad90e --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/BookController.java @@ -0,0 +1,23 @@ +package com.hazelcast.springboot.caching; + +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; + +@RestController +@RequestMapping("/books") +public class BookController { + + private final BookService bookService; + + BookController(BookService bookService) { + this.bookService = bookService; + } + + @GetMapping("/{isbn}") + public String getBookNameByIsbn(@PathVariable("isbn") String isbn) { + return bookService.getBookNameByIsbn(isbn); + } +} + diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/BookService.java b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/BookService.java new file mode 100644 index 000000000..df6521c1a --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/java/com/hazelcast/springboot/caching/BookService.java @@ -0,0 +1,23 @@ +package com.hazelcast.springboot.caching; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +@Service +public class BookService { + @Cacheable("books") + public String getBookNameByIsbn(String isbn) { + return findBookInSlowSource(isbn); + } + + private String findBookInSlowSource(String isbn) { + // some long processing + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + return "Sample Book Name, isbn: " + isbn; + } +} diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/src/main/resources/hazelcast.yaml b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/resources/hazelcast.yaml new file mode 100644 index 000000000..d8ca9f792 --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/src/main/resources/hazelcast.yaml @@ -0,0 +1,5 @@ +hazelcast: + network: + join: + multicast: + enabled: true \ No newline at end of file diff --git a/spring/spring-boot-caching-hazelcast-cache-manager/src/test/java/com/hazelcast/springboot/caching/ApplicationTest.java b/spring/spring-boot-caching-hazelcast-cache-manager/src/test/java/com/hazelcast/springboot/caching/ApplicationTest.java new file mode 100644 index 000000000..ee1de2193 --- /dev/null +++ b/spring/spring-boot-caching-hazelcast-cache-manager/src/test/java/com/hazelcast/springboot/caching/ApplicationTest.java @@ -0,0 +1,38 @@ +package com.hazelcast.springboot.caching; + +import com.hazelcast.core.HazelcastInstance; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class ApplicationTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private HazelcastInstance hazelcastInstance; + + @Test + public void useCachedValue() { + // given + String isbn = "12345"; + String cachedValue = "cached-value"; + hazelcastInstance.getMap("books").put(isbn, cachedValue); + + // when + String response = restTemplate.getForObject(String.format("http://localhost:%s/books/%s", port, isbn), String.class); + + // then + assertThat(response).isEqualTo(cachedValue); + } +}