Skip to content

Commit 2d646ca

Browse files
committed
Polishing
1 parent 1245919 commit 2d646ca

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public void afterPropertiesSet() {
135135
throw new IllegalStateException("'cacheManager' is required");
136136
}
137137
if (this.cacheOperationSource == null) {
138-
throw new IllegalStateException("The 'cacheOperationSources' property is required: "
139-
+ "If there are no cacheable methods, then don't use a cache aspect.");
138+
throw new IllegalStateException("The 'cacheOperationSources' property is required: " +
139+
"If there are no cacheable methods, then don't use a cache aspect.");
140140
}
141141

142142
this.initialized = true;
@@ -163,7 +163,7 @@ protected Collection<Cache> getCaches(CacheOperation operation) {
163163
for (String cacheName : cacheNames) {
164164
Cache cache = this.cacheManager.getCache(cacheName);
165165
if (cache == null) {
166-
throw new IllegalArgumentException("Cannot find cache named [" + cacheName + "] for " + operation);
166+
throw new IllegalArgumentException("Cannot find cache named '" + cacheName + "' for " + operation);
167167
}
168168
caches.add(cache);
169169
}
@@ -188,7 +188,7 @@ protected Object execute(Invoker invoker, Object target, Method method, Object[]
188188
if (targetClass == null && target != null) {
189189
targetClass = target.getClass();
190190
}
191-
final Collection<CacheOperation> cacheOp = getCacheOperationSource().getCacheOperations(method, targetClass);
191+
Collection<CacheOperation> cacheOp = getCacheOperationSource().getCacheOperations(method, targetClass);
192192

193193
// analyze caching information
194194
if (!CollectionUtils.isEmpty(cacheOp)) {
@@ -237,7 +237,6 @@ private void inspectCacheEvicts(Collection<CacheOperationContext> evictions, boo
237237
// for each cache
238238
// lazy key initialization
239239
Object key = null;
240-
241240
for (Cache cache : context.getCaches()) {
242241
// cache-wide flush
243242
if (evictOp.isCacheWide()) {
@@ -284,9 +283,8 @@ private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheabl
284283
logger.trace("Computed cache key " + key + " for operation " + context.operation);
285284
}
286285
if (key == null) {
287-
throw new IllegalArgumentException(
288-
"Null key returned for cache operation (maybe you are using named params on classes without debug info?) "
289-
+ context.operation);
286+
throw new IllegalArgumentException("Null key returned for cache operation (maybe you " +
287+
"are using named params on classes without debug info?) " + context.operation);
290288
}
291289
// add op/key (in case an update is discovered later on)
292290
cacheUpdates.put(context, key);
@@ -313,7 +311,7 @@ private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheabl
313311
}
314312
}
315313

316-
// return a status only if at least on cacheable matched
314+
// return a status only if at least one cacheable matched
317315
if (atLeastOnePassed) {
318316
return new CacheStatus(cacheUpdates, updateRequired, retVal);
319317
}
@@ -333,9 +331,8 @@ private Map<CacheOperationContext, Object> inspectCacheUpdates(Collection<CacheO
333331
logger.trace("Computed cache key " + key + " for operation " + context.operation);
334332
}
335333
if (key == null) {
336-
throw new IllegalArgumentException(
337-
"Null key returned for cache operation (maybe you are using named params on classes without debug info?) "
338-
+ context.operation);
334+
throw new IllegalArgumentException("Null key returned for cache operation (maybe you " +
335+
"are using named params on classes without debug info?) " + context.operation);
339336
}
340337
// add op/key (in case an update is discovered later on)
341338
cacheUpdates.put(context, key);
@@ -353,44 +350,39 @@ private Map<CacheOperationContext, Object> inspectCacheUpdates(Collection<CacheO
353350
private void update(Map<CacheOperationContext, Object> updates, Object retVal) {
354351
for (Map.Entry<CacheOperationContext, Object> entry : updates.entrySet()) {
355352
CacheOperationContext operationContext = entry.getKey();
356-
if(operationContext.canPutToCache(retVal)) {
353+
if (operationContext.canPutToCache(retVal)) {
357354
for (Cache cache : operationContext.getCaches()) {
358355
cache.put(entry.getValue(), retVal);
359356
}
360357
}
361358
}
362359
}
363360

364-
private Map<String, Collection<CacheOperationContext>> createOperationContext(Collection<CacheOperation> cacheOp,
365-
Method method, Object[] args, Object target, Class<?> targetClass) {
366-
367-
Map<String, Collection<CacheOperationContext>> map = new LinkedHashMap<String, Collection<CacheOperationContext>>(3);
361+
private Map<String, Collection<CacheOperationContext>> createOperationContext(
362+
Collection<CacheOperation> cacheOperations, Method method, Object[] args, Object target, Class<?> targetClass) {
368363

364+
Map<String, Collection<CacheOperationContext>> result = new LinkedHashMap<String, Collection<CacheOperationContext>>(3);
369365
Collection<CacheOperationContext> cacheables = new ArrayList<CacheOperationContext>();
370366
Collection<CacheOperationContext> evicts = new ArrayList<CacheOperationContext>();
371367
Collection<CacheOperationContext> updates = new ArrayList<CacheOperationContext>();
372368

373-
for (CacheOperation cacheOperation : cacheOp) {
369+
for (CacheOperation cacheOperation : cacheOperations) {
374370
CacheOperationContext opContext = getOperationContext(cacheOperation, method, args, target, targetClass);
375-
376371
if (cacheOperation instanceof CacheableOperation) {
377372
cacheables.add(opContext);
378373
}
379-
380374
if (cacheOperation instanceof CacheEvictOperation) {
381375
evicts.add(opContext);
382376
}
383-
384377
if (cacheOperation instanceof CachePutOperation) {
385378
updates.add(opContext);
386379
}
387380
}
388381

389-
map.put(CACHEABLE, cacheables);
390-
map.put(EVICT, evicts);
391-
map.put(UPDATE, updates);
392-
393-
return map;
382+
result.put(CACHEABLE, cacheables);
383+
result.put(EVICT, evicts);
384+
result.put(UPDATE, updates);
385+
return result;
394386
}
395387

396388

@@ -430,8 +422,7 @@ protected boolean isConditionPassing() {
430422
protected boolean isConditionPassing(Object result) {
431423
if (StringUtils.hasText(this.operation.getCondition())) {
432424
EvaluationContext evaluationContext = createEvaluationContext(result);
433-
return evaluator.condition(this.operation.getCondition(), this.method,
434-
evaluationContext);
425+
return evaluator.condition(this.operation.getCondition(), this.method, evaluationContext);
435426
}
436427
return true;
437428
}
@@ -444,7 +435,7 @@ protected boolean canPutToCache(Object value) {
444435
else if (this.operation instanceof CachePutOperation) {
445436
unless = ((CachePutOperation) this.operation).getUnless();
446437
}
447-
if(StringUtils.hasText(unless)) {
438+
if (StringUtils.hasText(unless)) {
448439
EvaluationContext evaluationContext = createEvaluationContext(value);
449440
return !evaluator.unless(unless, this.method, evaluationContext);
450441
}
@@ -464,8 +455,7 @@ protected Object generateKey() {
464455
}
465456

466457
private EvaluationContext createEvaluationContext(Object result) {
467-
return evaluator.createEvaluationContext(this.caches, this.method, this.args,
468-
this.target, this.targetClass, result);
458+
return evaluator.createEvaluationContext(this.caches, this.method, this.args, this.target, this.targetClass, result);
469459
}
470460

471461
protected Collection<Cache> getCaches() {

0 commit comments

Comments
 (0)