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:
@@ -261,7 +261,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
|
||||
for (CacheOperationContext context : contexts) {
|
||||
if (isConditionPassing(context, result)) {
|
||||
Object key = generateKey(context, result);
|
||||
if (!whenNotInCache || findInCaches(context, key) == null) {
|
||||
if (!whenNotInCache || findInAnyCaches(contexts, key) == null) {
|
||||
putRequests.add(new CachePutRequest(context, key));
|
||||
}
|
||||
}
|
||||
@@ -280,6 +280,16 @@ public abstract class CacheAspectSupport implements InitializingBean {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Cache.ValueWrapper findInAnyCaches(Collection<CacheOperationContext> contexts, Object key) {
|
||||
for (CacheOperationContext context : contexts) {
|
||||
ValueWrapper wrapper = findInCaches(context, key);
|
||||
if (wrapper != null) {
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Cache.ValueWrapper findInCaches(CacheOperationContext context, Object key) {
|
||||
for (Cache cache : context.getCaches()) {
|
||||
Cache.ValueWrapper wrapper = cache.get(key);
|
||||
|
||||
Reference in New Issue
Block a user