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 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")); + } }