From d22cd0cc9e85e4d3986dbab16820893f8c791c59 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 12 Jul 2017 09:39:17 +0200 Subject: [PATCH 1/2] Align with breaking API changes in RedisCacheManager See gh-9734 --- .../cache/RedisCacheConfiguration.java | 19 +++++++++++-------- .../cache/CacheAutoConfigurationTests.java | 12 +++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java index 1132fac50a..59f9ec9ac9 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.cache; +import java.util.LinkedHashSet; import java.util.List; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -27,17 +28,19 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder; +import org.springframework.data.redis.connection.RedisConnectionFactory; /** * Redis cache configuration. * * @author Stephane Nicoll + * @author Mark Paluch * @since 1.3.0 */ @Configuration @AutoConfigureAfter(RedisAutoConfiguration.class) -@ConditionalOnBean(RedisTemplate.class) +@ConditionalOnBean(RedisConnectionFactory.class) @ConditionalOnMissingBean(CacheManager.class) @Conditional(CacheCondition.class) class RedisCacheConfiguration { @@ -53,14 +56,14 @@ class RedisCacheConfiguration { } @Bean - public RedisCacheManager cacheManager(RedisTemplate redisTemplate) { - RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); - cacheManager.setUsePrefix(true); + public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { + + RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory); List cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { - cacheManager.setCacheNames(cacheNames); + builder.initialCacheNames(new LinkedHashSet<>(cacheNames)); } - return this.customizerInvoker.customize(cacheManager); + return this.customizerInvoker.customize(builder.build()); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 10516f7913..111b931b05 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -79,7 +79,7 @@ import org.springframework.context.annotation.Import; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.data.redis.cache.RedisCacheManager; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.connection.RedisConnectionFactory; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -92,6 +92,7 @@ import static org.mockito.Mockito.verify; * * @author Stephane Nicoll * @author EddĂș MelĂ©ndez + * @author Mark Paluch */ @RunWith(ModifiedClassPathRunner.class) @ClassPathExclusions("hazelcast-client-*.jar") @@ -282,8 +283,9 @@ public class CacheAutoConfigurationTests { RedisCacheManager cacheManager = validateCacheManager(context, RedisCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); - assertThat((Boolean) new DirectFieldAccessor(cacheManager) - .getPropertyValue("usePrefix")).isTrue(); + org.springframework.data.redis.cache.RedisCacheConfiguration configuration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor( + cacheManager).getPropertyValue("defaultCacheConfig"); + assertThat(configuration.usePrefix()).isTrue(); }); } @@ -892,8 +894,8 @@ public class CacheAutoConfigurationTests { static class RedisCacheConfiguration { @Bean - public RedisTemplate redisTemplate() { - return mock(RedisTemplate.class); + public RedisConnectionFactory redisConnectionFactory() { + return mock(RedisConnectionFactory.class); } } From 9df0992bd975194651041d3cf305f65e1789de4a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 13 Jul 2017 10:59:16 +0200 Subject: [PATCH 2/2] Polish "Align with breaking API changes in RedisCacheManager" Closes gh-9734 --- .../boot/autoconfigure/cache/RedisCacheConfiguration.java | 1 - .../autoconfigure/cache/CacheAutoConfigurationTests.java | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java index 59f9ec9ac9..34cc3e7e2a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java @@ -57,7 +57,6 @@ class RedisCacheConfiguration { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { - RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory); List cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 111b931b05..aa18a6fbb3 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -283,9 +283,9 @@ public class CacheAutoConfigurationTests { RedisCacheManager cacheManager = validateCacheManager(context, RedisCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); - org.springframework.data.redis.cache.RedisCacheConfiguration configuration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor( - cacheManager).getPropertyValue("defaultCacheConfig"); - assertThat(configuration.usePrefix()).isTrue(); + assertThat(((org.springframework.data.redis.cache.RedisCacheConfiguration) + new DirectFieldAccessor(cacheManager).getPropertyValue( + "defaultCacheConfig")).usePrefix()).isTrue(); }); }