Skip to content

DATAREDIS-283 - RedisCacheManager.loadCaches overrides already defined cache names in afterPropertiesSet(). #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,39 @@ public void setLoadRemoteCachesOnStartup(boolean loadRemoteCachesOnStartup) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

/**
* @author Christoph Strobl
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.class)
public class RedisCacheManagerUnitTests {
Expand Down Expand Up @@ -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<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"));
}
}