diff --git a/spring-boot-modules/spring-caching-2/pom.xml b/spring-boot-modules/spring-caching-2/pom.xml index 7a4744802a..3934a8b22e 100644 --- a/spring-boot-modules/spring-caching-2/pom.xml +++ b/spring-boot-modules/spring-caching-2/pom.xml @@ -1,7 +1,7 @@ + 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"> 4.0.0 spring-caching-2 0.1-SNAPSHOT @@ -51,7 +51,7 @@ ${caffeine.version} - it.ozimov + com.github.codemonstur embedded-redis ${embedded.redis.version} @@ -65,7 +65,7 @@ - 0.7.3 + 1.4.0 3.1.8 diff --git a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CacheConfig.java b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CacheConfig.java index 22b223066a..a911bdf161 100644 --- a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CacheConfig.java +++ b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CacheConfig.java @@ -1,7 +1,6 @@ package com.baeldung.caching.multicache; import com.github.benmanes.caffeine.cache.Caffeine; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.AnnotationCacheOperationSource; import org.springframework.cache.annotation.EnableCaching; @@ -37,7 +36,7 @@ public class CacheConfig { @Bean public CaffeineCache caffeineCacheConfig() { return new CaffeineCache("customerCache", Caffeine.newBuilder() - .expireAfterWrite(Duration.ofSeconds(3)) + .expireAfterWrite(Duration.ofSeconds(1)) .initialCapacity(1) .maximumSize(2000) .build()); diff --git a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerController.java b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerController.java deleted file mode 100644 index d053754be8..0000000000 --- a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerController.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.caching.multicache; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CustomerController { - - private final CustomerService customerService; - - @Autowired - public CustomerController(CustomerService customerService) { - this.customerService = customerService; - } - - @GetMapping("/customer/{id}") - public Customer getCustomer(@PathVariable String id) { - return customerService.getCustomer(id); - } -} \ No newline at end of file diff --git a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerRepository.java b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerRepository.java index da08e299e5..3281e35c76 100644 --- a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerRepository.java +++ b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerRepository.java @@ -1,44 +1,7 @@ package com.baeldung.caching.multicache; -import org.springframework.stereotype.Service; +import org.springframework.data.repository.CrudRepository; -import javax.annotation.PostConstruct; -import java.util.HashMap; -import java.util.Map; -@Service -public class CustomerRepository { - - private final Map customerMap = new HashMap<>(); - - public Customer getCustomerById(String id) { - return customerMap.get(id); - } - - @PostConstruct - private void setupCustomerRepo() { - Customer product1 = getCustomer("100001", "name1", "name1@mail.com"); - customerMap.put("100001", product1); - - Customer product2 = getCustomer("100002", "name2", "name2@mail.com"); - customerMap.put("100002", product2); - - Customer product3 = getCustomer("100003", "name3", "name3@mail.com"); - customerMap.put("100003", product3); - - Customer product4 = getCustomer("100004", "name4", "name4@mail.com"); - customerMap.put("100004", product4); - - Customer product5 = getCustomer("100005", "name5", "name5@mail.com"); - customerMap.put("100005", product5); - } - - private static Customer getCustomer(String id, String name, String email) { - Customer customer = new Customer(); - customer.setId(id); - customer.setName(name); - customer.setEmail(email); - - return customer; - } +public interface CustomerRepository extends CrudRepository { } diff --git a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerService.java b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerService.java index db24254d83..14a7f293c8 100644 --- a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerService.java +++ b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/multicache/CustomerService.java @@ -20,6 +20,7 @@ public class CustomerService { @Cacheable(cacheNames = "customerCache", cacheManager = "redisCacheManager") }) public Customer getCustomer(String id) { - return customerRepository.getCustomerById(id); + return customerRepository.findById(id) + .orElseThrow(RuntimeException::new); } } \ No newline at end of file diff --git a/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/multicache/CustomerServiceCachingIntegrationTest.java b/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/multicache/CustomerServiceCachingIntegrationTest.java index 17faef871c..df53eb2833 100644 --- a/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/multicache/CustomerServiceCachingIntegrationTest.java +++ b/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/multicache/CustomerServiceCachingIntegrationTest.java @@ -17,6 +17,8 @@ import redis.embedded.RedisServer; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import java.io.IOException; +import java.util.Optional; import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; @@ -43,56 +45,53 @@ class CustomerServiceCachingIntegrationTest { private CacheManager caffeineCacheManager; @Test - void givenCustomerIsPresentInDb_whenFindCustomerById_thenCustomerReturnedFromDb_And_Cached() { - Customer customer = new Customer("100", "test", "test@mail.com"); - given(customerRepository.getCustomerById("100")) - .willReturn(customer); + void givenCustomerIsPresentInDB_whenGetCustomerById_thenCustomerReturnedFromDBAndCached() { + String CUSTOMER_ID = "100"; + Customer customer = new Customer(CUSTOMER_ID, "test", "test@mail.com"); - Customer customerCacheMiss = customerService.getCustomer("100"); + given(customerRepository.findById(CUSTOMER_ID)) + .willReturn(Optional.of(customer)); + + Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID); assertThat(customerCacheMiss).isEqualTo(customer); - verify(customerRepository, times(1)).getCustomerById("100"); - assertThat(customerFromRedisCache("100")).isEqualTo(customer); - assertThat(customerFromCaffeineCache("100")).isEqualTo(customer); + verify(customerRepository, times(1)).findById(CUSTOMER_ID); + assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer); + assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(customer); } @Test - void givenCustomerIsPresentInDb_whenFindCustomerById_CalledTwice_thenCustomerReturnedFromDb_And_Cached() { - Customer customer = new Customer("101", "test", "test@mail.com"); - given(customerRepository.getCustomerById("101")) - .willReturn(customer); + void givenCustomerIsPresentInDB_whenGetCustomerByIdIsCalledTwice_thenCustomerReturnedFromDBAndCached() { + String CUSTOMER_ID = "101"; + Customer customer = new Customer(CUSTOMER_ID, "test", "test@mail.com"); + given(customerRepository.findById(CUSTOMER_ID)).willReturn(Optional.of(customer)); - Customer customerCacheMiss = customerService.getCustomer("101"); - Customer customerCacheHit = customerService.getCustomer("101"); + Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID); + Customer customerCacheHit = customerService.getCustomer(CUSTOMER_ID); assertThat(customerCacheMiss).isEqualTo(customer); assertThat(customerCacheHit).isEqualTo(customer); - - verify(customerRepository, times(1)).getCustomerById("101"); - assertThat(customerFromRedisCache("101")).isEqualTo(customer); - assertThat(customerFromCaffeineCache("101")).isEqualTo(customer); + verify(customerRepository, times(1)).findById(CUSTOMER_ID); + assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer); + assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(customer); } @Test - void givenCustomerIsPresentInDb_whenFindCustomerById_CalledThrice_thenCustomerReturnedFromDBFirst_ThenFromCache() throws InterruptedException { - Customer customer = new Customer("102", "test", "test@mail.com"); - given(customerRepository.getCustomerById("102")) - .willReturn(customer); + void givenCustomerIsPresentInDB_whenGetCustomerByIdIsCalledThrice_thenCustomerReturnedFromDBAndCached() throws InterruptedException { + String CUSTOMER_ID = "102"; + Customer customer = new Customer(CUSTOMER_ID, "test", "test@mail.com"); + given(customerRepository.findById(CUSTOMER_ID)) + .willReturn(Optional.of(customer)); - Customer customerCacheMiss = customerService.getCustomer("102"); - Customer customerCacheHit = customerService.getCustomer("102"); + Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID); + TimeUnit.SECONDS.sleep(2); + Customer customerCacheHit = customerService.getCustomer(CUSTOMER_ID); - TimeUnit.SECONDS.sleep(4); - - assertThat(customerFromCaffeineCache("102")).isEqualTo(null); - Customer customerCacheHitAgain = customerService.getCustomer("102"); - - verify(customerRepository, times(1)).getCustomerById("102"); + verify(customerRepository, times(1)).findById(CUSTOMER_ID); assertThat(customerCacheMiss).isEqualTo(customer); assertThat(customerCacheHit).isEqualTo(customer); - assertThat(customerCacheHitAgain).isEqualTo(customer); - assertThat(customerFromRedisCache("102")).isEqualTo(customer); - assertThat(customerFromCaffeineCache("102")).isEqualTo(customer); + assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer); + assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(customer); } private Object customerFromRedisCache(String key) { @@ -110,17 +109,17 @@ class CustomerServiceCachingIntegrationTest { private final RedisServer redisServer; - public EmbeddedRedisConfiguration() { + public EmbeddedRedisConfiguration() throws IOException { this.redisServer = new RedisServer(); } @PostConstruct - public void startRedis() { + public void startRedis() throws IOException { redisServer.start(); } @PreDestroy - public void stopRedis() { + public void stopRedis() throws IOException { this.redisServer.stop(); } } diff --git a/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/redis/ItemServiceCachingIntegrationTest.java b/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/redis/ItemServiceCachingIntegrationTest.java index 291e729fb9..b070528bf6 100644 --- a/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/redis/ItemServiceCachingIntegrationTest.java +++ b/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/redis/ItemServiceCachingIntegrationTest.java @@ -16,6 +16,7 @@ import redis.embedded.RedisServer; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import java.io.IOException; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; @@ -66,17 +67,17 @@ class ItemServiceCachingIntegrationTest { private final RedisServer redisServer; - public EmbeddedRedisConfiguration() { + public EmbeddedRedisConfiguration() throws IOException { this.redisServer = new RedisServer(); } @PostConstruct - public void startRedis() { + public void startRedis() throws IOException { redisServer.start(); } @PreDestroy - public void stopRedis() { + public void stopRedis() throws IOException { this.redisServer.stop(); } }