BAEL-5696: Adds TTL configuration for caching (#12700)
Co-authored-by: Abdul Wahab <abdul.wahab@monese.com>
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
|
||||
|
||||
@Configuration
|
||||
public class CacheConfig {
|
||||
|
||||
@Bean
|
||||
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
|
||||
return (builder) -> builder
|
||||
.withCacheConfiguration("itemCache",
|
||||
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)))
|
||||
.withCacheConfiguration("customerCache",
|
||||
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5)));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisCacheConfiguration cacheConfiguration() {
|
||||
return RedisCacheConfiguration.defaultCacheConfig()
|
||||
.entryTtl(Duration.ofMinutes(60))
|
||||
.disableCachingNullValues()
|
||||
.serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Item implements Serializable {
|
||||
|
||||
@Id
|
||||
String id;
|
||||
|
||||
String description;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
public class ItemController {
|
||||
|
||||
private final ItemService itemService;
|
||||
|
||||
@GetMapping("/item/{id}")
|
||||
public Item getItemById(@PathVariable String id) {
|
||||
return itemService.getItemForId(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ItemRepository extends CrudRepository<Item, String> {
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class ItemService {
|
||||
|
||||
private final ItemRepository itemRepository;
|
||||
|
||||
@Cacheable(value = "itemCache")
|
||||
public Item getItemForId(String id) {
|
||||
return itemRepository.findById(id)
|
||||
.orElseThrow(RuntimeException::new);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCaching
|
||||
public class RedisCacheApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RedisCacheApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
# Enabling H2 Console
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2
|
||||
|
||||
# Connection details
|
||||
#spring.redis.host=localhost
|
||||
#spring.redis.port=6379
|
||||
@@ -1 +0,0 @@
|
||||
INSERT INTO ITEM VALUES('abc','ITEM1');
|
||||
-84
@@ -1,84 +0,0 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@Import({ CacheConfig.class, ItemService.class })
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ImportAutoConfiguration(classes = { CacheAutoConfiguration.class, RedisAutoConfiguration.class })
|
||||
@EnableCaching
|
||||
class ItemServiceCachingIntegrationTest {
|
||||
|
||||
private static final String AN_ID = "id-1";
|
||||
private static final String A_DESCRIPTION = "an item";
|
||||
|
||||
@MockBean
|
||||
private ItemRepository mockItemRepository;
|
||||
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Test
|
||||
void givenRedisCaching_whenFindItemById_thenItemReturnedFromCache() {
|
||||
Item anItem = new Item(AN_ID, A_DESCRIPTION);
|
||||
given(mockItemRepository.findById(AN_ID))
|
||||
.willReturn(Optional.of(anItem));
|
||||
|
||||
Item itemCacheMiss = itemService.getItemForId(AN_ID);
|
||||
Item itemCacheHit = itemService.getItemForId(AN_ID);
|
||||
|
||||
assertThat(itemCacheMiss).isEqualTo(anItem);
|
||||
assertThat(itemCacheHit).isEqualTo(anItem);
|
||||
|
||||
verify(mockItemRepository, times(1)).findById(AN_ID);
|
||||
assertThat(itemFromCache()).isEqualTo(anItem);
|
||||
}
|
||||
|
||||
private Object itemFromCache() {
|
||||
return cacheManager.getCache("itemCache").get(AN_ID).get();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class EmbeddedRedisConfiguration {
|
||||
|
||||
private final RedisServer redisServer;
|
||||
|
||||
public EmbeddedRedisConfiguration() {
|
||||
this.redisServer = new RedisServer();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void startRedis() {
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stopRedis() {
|
||||
this.redisServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
<logger name="org.springframework" level="OFF"/>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user