reformatting from default and update test method names
This commit is contained in:
parent
b182b42c07
commit
b2a0a75389
@ -26,40 +26,40 @@ public class CacheConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public CacheManager caffeineCacheManager() {
|
||||
public CacheManager caffeineCacheManager(CaffeineCache caffeineCache) {
|
||||
SimpleCacheManager manager = new SimpleCacheManager();
|
||||
manager.setCaches(Arrays.asList(caffeineCacheConfig()));
|
||||
manager.setCaches(Arrays.asList(caffeineCache));
|
||||
return manager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CaffeineCache caffeineCacheConfig() {
|
||||
return new CaffeineCache("customerCache", Caffeine.newBuilder()
|
||||
.expireAfterWrite(Duration.ofSeconds(3))
|
||||
.initialCapacity(1)
|
||||
.maximumSize(2000)
|
||||
.build());
|
||||
.expireAfterWrite(Duration.ofSeconds(3))
|
||||
.initialCapacity(1)
|
||||
.maximumSize(2000)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
|
||||
public CacheManager redisCacheManager(RedisConnectionFactory connectionFactory, RedisCacheConfiguration redisCacheConfiguration) {
|
||||
return RedisCacheManager.RedisCacheManagerBuilder
|
||||
.fromConnectionFactory(connectionFactory)
|
||||
.withCacheConfiguration("customerCache", cacheConfiguration())
|
||||
.build();
|
||||
.fromConnectionFactory(connectionFactory)
|
||||
.withCacheConfiguration("customerCache", redisCacheConfiguration)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisCacheConfiguration cacheConfiguration() {
|
||||
return RedisCacheConfiguration.defaultCacheConfig()
|
||||
.entryTtl(Duration.ofMinutes(5))
|
||||
.disableCachingNullValues()
|
||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
|
||||
.entryTtl(Duration.ofMinutes(5))
|
||||
.disableCachingNullValues()
|
||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheInterceptor cacheInterceptor() {
|
||||
CacheInterceptor interceptor = new CustomerCacheInterceptor(caffeineCacheManager());
|
||||
public CacheInterceptor cacheInterceptor(CacheManager caffeineCacheManager) {
|
||||
CacheInterceptor interceptor = new CustomerCacheInterceptor(caffeineCacheManager);
|
||||
interceptor.setCacheOperationSources(cacheOperationSource());
|
||||
return interceptor;
|
||||
}
|
||||
|
@ -16,11 +16,11 @@ public class CustomerService {
|
||||
}
|
||||
|
||||
@Caching(cacheable = {
|
||||
@Cacheable(cacheNames = "customerCache", cacheManager = "caffeineCacheManager"),
|
||||
@Cacheable(cacheNames = "customerCache", cacheManager = "redisCacheManager")
|
||||
@Cacheable(cacheNames = "customerCache", cacheManager = "caffeineCacheManager"),
|
||||
@Cacheable(cacheNames = "customerCache", cacheManager = "redisCacheManager")
|
||||
})
|
||||
public Customer getCustomer(String id) {
|
||||
return customerRepository.findById(id)
|
||||
.orElseThrow(RuntimeException::new);
|
||||
.orElseThrow(RuntimeException::new);
|
||||
}
|
||||
}
|
@ -2,10 +2,8 @@ package com.baeldung.caching.twolevelcache;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCaching
|
||||
public class TwoLevelCacheApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -44,23 +44,22 @@ class CustomerServiceCachingIntegrationTest {
|
||||
private CacheManager caffeineCacheManager;
|
||||
|
||||
@Test
|
||||
void givenCustomerIsPresentInDB_whenGetCustomerById_thenCustomerReturnedFromDBAndCached() {
|
||||
void givenCustomerIsPresent_whenGetCustomerCalled_thenReturnCustomerAndCacheIt() {
|
||||
String CUSTOMER_ID = "100";
|
||||
Customer customer = new Customer(CUSTOMER_ID, "test", "test@mail.com");
|
||||
|
||||
given(customerRepository.findById(CUSTOMER_ID))
|
||||
.willReturn(Optional.of(customer));
|
||||
given(customerRepository.findById(CUSTOMER_ID)).willReturn(Optional.of(customer));
|
||||
|
||||
Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID);
|
||||
|
||||
assertThat(customerCacheMiss).isEqualTo(customer);
|
||||
verify(customerRepository, times(1)).findById(CUSTOMER_ID);
|
||||
assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCustomerIsPresentInDB_whenGetCustomerByIdIsCalledTwice_thenCustomerReturnedFromDBAndCached() {
|
||||
void givenCustomerIsPresent_whenGetCustomerCalledTwice_thenReturnCustomerAndCacheIt() {
|
||||
String CUSTOMER_ID = "101";
|
||||
Customer customer = new Customer(CUSTOMER_ID, "test", "test@mail.com");
|
||||
given(customerRepository.findById(CUSTOMER_ID)).willReturn(Optional.of(customer));
|
||||
@ -71,16 +70,15 @@ class CustomerServiceCachingIntegrationTest {
|
||||
assertThat(customerCacheMiss).isEqualTo(customer);
|
||||
assertThat(customerCacheHit).isEqualTo(customer);
|
||||
verify(customerRepository, times(1)).findById(CUSTOMER_ID);
|
||||
assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCustomerIsPresentInDB_whenGetCustomerByIdIsCalledTwice_AndFirstCacheExpires_thenCustomerReturnedFromDBAndCached() throws InterruptedException {
|
||||
void givenCustomerIsPresent_whenGetCustomerCalledTwiceAndFirstCacheExpired_thenReturnCustomerAndCacheIt() 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));
|
||||
given(customerRepository.findById(CUSTOMER_ID)).willReturn(Optional.of(customer));
|
||||
|
||||
Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID);
|
||||
TimeUnit.SECONDS.sleep(3);
|
||||
@ -90,18 +88,18 @@ class CustomerServiceCachingIntegrationTest {
|
||||
verify(customerRepository, times(1)).findById(CUSTOMER_ID);
|
||||
assertThat(customerCacheMiss).isEqualTo(customer);
|
||||
assertThat(customerCacheHit).isEqualTo(customer);
|
||||
assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
assertThat(customerFromRedisCache(CUSTOMER_ID)).isEqualTo(customer);
|
||||
}
|
||||
|
||||
private Object customerFromRedisCache(String key) {
|
||||
return redisCacheManager.getCache("customerCache").get(key) != null ?
|
||||
redisCacheManager.getCache("customerCache").get(key).get() : null;
|
||||
redisCacheManager.getCache("customerCache").get(key).get() : null;
|
||||
}
|
||||
|
||||
private Object customerFromCaffeineCache(String key) {
|
||||
return caffeineCacheManager.getCache("customerCache").get(key) != null ?
|
||||
caffeineCacheManager.getCache("customerCache").get(key).get() : null;
|
||||
caffeineCacheManager.getCache("customerCache").get(key).get() : null;
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
|
Loading…
x
Reference in New Issue
Block a user