Detect cache hit with multiple @Cachables
Fix CacheAspectSupport to consider a cache hit from any of the multiple
@Cachables that may have been specified using the @Caching annotation.
Prior to this commit the following scenario would never produce a hit:
@Caching(cacheable = {
@Cacheable(value = "c1", unless = "#result.size() < 4"),
@Cacheable(value = "c2", unless = "#result.size() > 3")
})
Issue: SPR-11124
This commit is contained in:
@@ -269,7 +269,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
|
||||
|
||||
private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheables) {
|
||||
Map<CacheOperationContext, Object> cacheUpdates = new LinkedHashMap<CacheOperationContext, Object>(cacheables.size());
|
||||
boolean updateRequired = false;
|
||||
boolean cacheHit = false;
|
||||
Object retVal = null;
|
||||
|
||||
if (!cacheables.isEmpty()) {
|
||||
@@ -288,21 +288,17 @@ public abstract class CacheAspectSupport implements InitializingBean {
|
||||
}
|
||||
// add op/key (in case an update is discovered later on)
|
||||
cacheUpdates.put(context, key);
|
||||
boolean localCacheHit = false;
|
||||
// check whether the cache needs to be inspected or not (the method will be invoked anyway)
|
||||
if (!updateRequired) {
|
||||
if (!cacheHit) {
|
||||
for (Cache cache : context.getCaches()) {
|
||||
Cache.ValueWrapper wrapper = cache.get(key);
|
||||
if (wrapper != null) {
|
||||
retVal = wrapper.get();
|
||||
localCacheHit = true;
|
||||
cacheHit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!localCacheHit) {
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (log) {
|
||||
@@ -313,7 +309,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
|
||||
|
||||
// return a status only if at least one cacheable matched
|
||||
if (atLeastOnePassed) {
|
||||
return new CacheStatus(cacheUpdates, updateRequired, retVal);
|
||||
return new CacheStatus(cacheUpdates, !cacheHit, retVal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user