Skip to content
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

Spring caching: combining multiple @Cacheable within @Caching annotation doesn't work [SPR-11124] #15750

spring-projects-issues opened this issue Nov 26, 2013 · 0 comments
in: core type: bug


Copy link

@spring-projects-issues spring-projects-issues commented Nov 26, 2013

Pieter Van der Meeren opened SPR-11124 and commented

When combining multiple @Cacheable annotations within a @Caching annotation, the caching does not work due to bug in CacheAspectSupport class.

Consider the following service method, annotated with the Spring caching annotationa:

public class DummyServiceImpl implements DummyService {
	private static final List<String> BIG_LIST = asList("four", "five", "six", "seven", "eight", "nine");
	private static final List<String> SMALL_LIST = asList("one", "two", "three");

	private static final Logger LOGGER = Logger.getLogger(DummyServiceImpl.class);

	public static final Long SMALL_CACHE_KEY = 1L;
	public static final Long BIG_CACHE_KEY = 2L;

	@Caching(cacheable = { @Cacheable(value = "bigCache", unless = "#result.size() < 4"), 
							@Cacheable(value = "smallCache", unless = "#result.size() > 3") })
	public List<String> getTheList(Long id) {"Cache NOT hit, executing service logic...");
		if (SMALL_CACHE_KEY.equals(id)) {
			return SMALL_LIST;
		} else if (BIG_CACHE_KEY.equals(id)) {
			return BIG_LIST;
		} else {
			return emptyList();

You can see we are combining two @Cacheable annotations, we define a cache "smallCache" which will hold the result if the size of the result is < 4 elements (we do this using the "unless" parameter).Then we define another cache "bigCache" which will hold the result if the size of the result is > 3.

If we call the cached service method with a key which should returen the small list, we see that the smallCache size is incremented (cache size is now = 1).But if we call the cached service method again with the same key we still see the service logic is executed instead of returning the cached value.

After some investigation, I think the problem resides in the CacheAspectSupport class, in the method inspectCacheables, the local variable updateRequired is overwritten if there is more then 1 cache defined, which makes that the cached value is never reused if you have more then 1 @Cacheable annotation within a @Caching enclosing annotation.

Small maven project attached to reproduce the issue (testclass: DummyServiceImplTest).

Affects: 3.2.4, 3.2.5


Issue Links:

  • #16216 Target Cache gets checked twice for every cache hit

Referenced from: commits 73a8a1b, b0b40da

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
in: core type: bug
None yet

No branches or pull requests

2 participants