From 997a2c993aa5bbef98bc0ca607fe2f71c947bb24 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Wed, 12 Mar 2014 14:23:19 +0100 Subject: [PATCH 1/2] DATAREDIS-283 - RedisCacheManager.loadCaches overrides already defined cache names in afterPropertiesSet(). Prepare issue branch. --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index f09fb8eb53..e5f74a9ab3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ jredisVersion=06052013 jedisVersion=2.4.1 springVersion=3.2.8.RELEASE log4jVersion=1.2.17 -version=1.3.0.BUILD-SNAPSHOT +version=1.3.0.DATAREDIS-283-SNAPSHOT srpVersion=0.7 jacksonVersion=1.8.8 fasterXmlJacksonVersion=2.2.0 From 3b4c08d32a3e1774accbbcfb513c2537bbcfa0c7 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Wed, 12 Mar 2014 14:25:08 +0100 Subject: [PATCH 2/2] DATAREDIS-283 - RedisCacheManager.loadCaches overrides already defined cache names in afterPropertiesSet(). We now retain the potentially already configured caches during the initialisation of the RedisCacheManager bean. Previously we discarded the already configured caches which was an undesired side-effect. Original pull request: #45. --- .../data/redis/cache/RedisCacheManager.java | 34 ++++++++++++++++++- .../cache/RedisCacheManagerUnitTests.java | 31 +++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java index 93831c3b54..69eacd784f 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java @@ -159,7 +159,39 @@ public void setLoadRemoteCachesOnStartup(boolean loadRemoteCachesOnStartup) { protected Collection loadCaches() { Assert.notNull(this.template, "A redis template is required in order to interact with data store"); - return loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections. emptyList(); + return addConfiguredCachesIfNecessary(loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections. emptyList()); + } + + /** + * Returns a new {@link Collection} of {@link Cache} from the given caches collection and adds + * the configured {@link Cache}s of they are not already present. + * + * @param caches must not be {@literal null} + * @return + */ + private Collection addConfiguredCachesIfNecessary(Collection caches) { + + Assert.notNull(caches, "Caches must not be null!"); + + Collection result = new ArrayList(caches); + + for(String cacheName : getCacheNames()){ + + boolean configuredCacheAlreadyPresent = false; + + for(Cache cache : caches){ + if(cache.getName().equals(cacheName)){ + configuredCacheAlreadyPresent = true; + break; + } + } + + if(!configuredCacheAlreadyPresent){ + result.add(getCache(cacheName)); + } + } + + return result; } private Cache createAndAddCache(String cacheName) { diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java index 38edef84c9..3730ad81e5 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java @@ -42,6 +42,7 @@ /** * @author Christoph Strobl + * @author Thomas Darimont */ @RunWith(MockitoJUnitRunner.class) public class RedisCacheManagerUnitTests { @@ -171,4 +172,34 @@ public void testPuttingCacheManagerIntoStaticModeShouldNotRemoveAlreadyRegistere assertThat(cacheManager.getCache("redis"), notNullValue()); } + /** + * @see DATAREDIS-283 + */ + @Test + public void testRetainConfiguredCachesAfterBeanInitialization(){ + + cacheManager.setCacheNames(Arrays.asList("spring", "data")); + cacheManager.afterPropertiesSet(); + + assertThat(cacheManager.getCache("spring"), notNullValue()); + assertThat(cacheManager.getCache("data"), notNullValue()); + } + + /** + * @see DATAREDIS-283 + */ + @Test + public void testRetainConfiguredCachesAfterBeanInitializationWithLoadingOfRemoteKeys(){ + + cacheManager.setCacheNames(Arrays.asList("spring", "data")); + Set keys = new HashSet(Arrays.asList(redisTemplate.getKeySerializer() + .serialize("remote-cache~keys"))); + when(redisConnectionMock.keys(any(byte[].class))).thenReturn(keys); + cacheManager.setLoadRemoteCachesOnStartup(true); + cacheManager.afterPropertiesSet(); + + assertThat(cacheManager.getCache("spring"), notNullValue()); + assertThat(cacheManager.getCache("data"), notNullValue()); + assertThat(cacheManager.getCacheNames(), IsCollectionContaining.hasItem("remote-cache")); + } }