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.
This commit is contained in:
Thomas Darimont
2014-03-12 14:25:08 +01:00
committed by Christoph Strobl
parent 887dbe9e0c
commit 5ef23c206c
2 changed files with 75 additions and 4 deletions

View File

@@ -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<? extends Cache> loadCaches() {
Assert.notNull(this.template, "A redis template is required in order to interact with data store");
return loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections.<Cache> emptyList();
return addConfiguredCachesIfNecessary(loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections
.<Cache> 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<? extends Cache> addConfiguredCachesIfNecessary(Collection<? extends Cache> caches) {
Assert.notNull(caches, "Caches must not be null!");
Collection<Cache> result = new ArrayList<Cache>(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);
}
}

View File

@@ -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<byte[]> keys = new HashSet<byte[]>(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"));
}
}