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 93831c3b5..82ca0ae07 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java @@ -15,7 +15,13 @@ */ package org.springframework.data.redis.cache; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; @@ -159,7 +165,41 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager 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) { @@ -195,8 +235,8 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager } } } catch (Exception e) { - if(logger.isWarnEnabled()){ - logger.warn("Failed to initialize cache with remote cache keys.",e); + if (logger.isWarnEnabled()) { + logger.warn("Failed to initialize cache with remote cache keys.", e); } } 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 38edef84c..17f2cdd25 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 @@ import org.springframework.data.redis.core.RedisTemplate; /** * @author Christoph Strobl + * @author Thomas Darimont */ @RunWith(MockitoJUnitRunner.class) public class RedisCacheManagerUnitTests { @@ -171,4 +172,34 @@ public class RedisCacheManagerUnitTests { 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")); + } }