You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating a org.springframework.cache.support.SimpleCacheManager and explicitly adding caches to it, the configured object does not maintain the contract of the org.springframework.cache.CacheManager interface.
Specifically, getCacheNames() and getCache(String name) do not behave as one would expect, returning an empty collection and null respectively.
This issue is reproducible with version 5.1.7.RELEASE.
This issue is demonstrated with the following test:
package com.ak;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Collections;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
public class TestSimpleCacheManager {
private static final String CACHE_NAME = "this is a cache";
@Test
public void testGetCacheNames() {
SimpleCacheManager simpleCacheManager = setUpSimpleCacheManager();
Collection<String> cacheNames = simpleCacheManager.getCacheNames();
assertEquals(1, cacheNames.size());
assertTrue(cacheNames.contains(CACHE_NAME));
}
@Test
public void testGetCache() {
SimpleCacheManager simpleCacheManager = setUpSimpleCacheManager();
Cache cache = simpleCacheManager.getCache(CACHE_NAME);
assertNotNull(cache);
}
private SimpleCacheManager setUpSimpleCacheManager() {
Cache cache = new ConcurrentMapCache(CACHE_NAME);
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
simpleCacheManager.setCaches(Collections.singleton(cache));
return simpleCacheManager;
}
}
The contract of SimpleCacheManager (and more particularly AbstractCacheManager) was designed so to be created as a bean and there is an afterProperties() callback that is invoked automatically when you are doing so. If you add that in setUpSimpleCacheManager before returning the cache manager both your tests pass.
I agree it is a bit confusing when using the API directly (especially since there are methods to mutate the cache manager once it is initialized).
If no change is required or desired, which makes sense to me given your explanation, then I would wonder whether or not it's worth making a note of this in the documentation. I don't think I feel too strongly about this one way or another, but something to consider.
snicoll
changed the title
SimpleCacheManager does not maintain CacheManager contract
Document that SimpleCacheManager requires caches to be initialized when not used as a bean
Aug 7, 2020
When creating a
org.springframework.cache.support.SimpleCacheManager
and explicitly adding caches to it, the configured object does not maintain the contract of theorg.springframework.cache.CacheManager
interface.Specifically,
getCacheNames()
andgetCache(String name)
do not behave as one would expect, returning an empty collection andnull
respectively.This issue is reproducible with version 5.1.7.RELEASE.
This issue is demonstrated with the following test:
using the following pom file:
I'd be happy to take a crack at creating a patch for this issue, should this be something that is desired to be fixed.
Thanks
The text was updated successfully, but these errors were encountered: