Skip to content

Commit 5ef23c2

Browse files
Thomas Darimontchristophstrobl
Thomas Darimont
authored andcommitted
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.
1 parent 887dbe9 commit 5ef23c2

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import java.util.*;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.LinkedHashSet;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Set;
1925
import java.util.concurrent.ConcurrentHashMap;
2026

2127
import org.apache.commons.logging.Log;
@@ -159,7 +165,41 @@ public void setLoadRemoteCachesOnStartup(boolean loadRemoteCachesOnStartup) {
159165
protected Collection<? extends Cache> loadCaches() {
160166

161167
Assert.notNull(this.template, "A redis template is required in order to interact with data store");
162-
return loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections.<Cache> emptyList();
168+
return addConfiguredCachesIfNecessary(loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections
169+
.<Cache> emptyList());
170+
}
171+
172+
/**
173+
* Returns a new {@link Collection} of {@link Cache} from the given caches collection and adds the configured
174+
* {@link Cache}s of they are not already present.
175+
*
176+
* @param caches must not be {@literal null}
177+
* @return
178+
*/
179+
private Collection<? extends Cache> addConfiguredCachesIfNecessary(Collection<? extends Cache> caches) {
180+
181+
Assert.notNull(caches, "Caches must not be null!");
182+
183+
Collection<Cache> result = new ArrayList<Cache>(caches);
184+
185+
for (String cacheName : getCacheNames()) {
186+
187+
boolean configuredCacheAlreadyPresent = false;
188+
189+
for (Cache cache : caches) {
190+
191+
if (cache.getName().equals(cacheName)) {
192+
configuredCacheAlreadyPresent = true;
193+
break;
194+
}
195+
}
196+
197+
if (!configuredCacheAlreadyPresent) {
198+
result.add(getCache(cacheName));
199+
}
200+
}
201+
202+
return result;
163203
}
164204

165205
private Cache createAndAddCache(String cacheName) {
@@ -195,8 +235,8 @@ private List<RedisCache> loadAndInitRemoteCaches() {
195235
}
196236
}
197237
} catch (Exception e) {
198-
if(logger.isWarnEnabled()){
199-
logger.warn("Failed to initialize cache with remote cache keys.",e);
238+
if (logger.isWarnEnabled()) {
239+
logger.warn("Failed to initialize cache with remote cache keys.", e);
200240
}
201241
}
202242

src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
/**
4444
* @author Christoph Strobl
45+
* @author Thomas Darimont
4546
*/
4647
@RunWith(MockitoJUnitRunner.class)
4748
public class RedisCacheManagerUnitTests {
@@ -171,4 +172,34 @@ public void testPuttingCacheManagerIntoStaticModeShouldNotRemoveAlreadyRegistere
171172
assertThat(cacheManager.getCache("redis"), notNullValue());
172173
}
173174

175+
/**
176+
* @see DATAREDIS-283
177+
*/
178+
@Test
179+
public void testRetainConfiguredCachesAfterBeanInitialization() {
180+
181+
cacheManager.setCacheNames(Arrays.asList("spring", "data"));
182+
cacheManager.afterPropertiesSet();
183+
184+
assertThat(cacheManager.getCache("spring"), notNullValue());
185+
assertThat(cacheManager.getCache("data"), notNullValue());
186+
}
187+
188+
/**
189+
* @see DATAREDIS-283
190+
*/
191+
@Test
192+
public void testRetainConfiguredCachesAfterBeanInitializationWithLoadingOfRemoteKeys() {
193+
194+
cacheManager.setCacheNames(Arrays.asList("spring", "data"));
195+
Set<byte[]> keys = new HashSet<byte[]>(Arrays.asList(redisTemplate.getKeySerializer()
196+
.serialize("remote-cache~keys")));
197+
when(redisConnectionMock.keys(any(byte[].class))).thenReturn(keys);
198+
cacheManager.setLoadRemoteCachesOnStartup(true);
199+
cacheManager.afterPropertiesSet();
200+
201+
assertThat(cacheManager.getCache("spring"), notNullValue());
202+
assertThat(cacheManager.getCache("data"), notNullValue());
203+
assertThat(cacheManager.getCacheNames(), IsCollectionContaining.hasItem("remote-cache"));
204+
}
174205
}

0 commit comments

Comments
 (0)