Skip to content

Commit 1245919

Browse files
committed
Polishing
1 parent bcbd338 commit 1245919

File tree

1 file changed

+52
-63
lines changed

1 file changed

+52
-63
lines changed

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

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
28+
2829
import org.springframework.aop.framework.AopProxyUtils;
2930
import org.springframework.beans.factory.InitializingBean;
3031
import org.springframework.cache.Cache;
@@ -61,23 +62,25 @@
6162
*/
6263
public abstract class CacheAspectSupport implements InitializingBean {
6364

64-
public interface Invoker {
65-
Object invoke();
66-
}
65+
private static final String CACHEABLE = "cacheable";
66+
67+
private static final String UPDATE = "cacheupdate";
68+
69+
private static final String EVICT = "cacheevict";
70+
6771

6872
protected final Log logger = LogFactory.getLog(getClass());
6973

74+
private final ExpressionEvaluator evaluator = new ExpressionEvaluator();
75+
7076
private CacheManager cacheManager;
7177

7278
private CacheOperationSource cacheOperationSource;
7379

74-
private final ExpressionEvaluator evaluator = new ExpressionEvaluator();
75-
7680
private KeyGenerator keyGenerator = new DefaultKeyGenerator();
7781

7882
private boolean initialized = false;
7983

80-
private static final String CACHEABLE = "cacheable", UPDATE = "cacheupdate", EVICT = "cacheevict";
8184

8285
/**
8386
* Set the CacheManager that this cache aspect should delegate to.
@@ -101,10 +104,8 @@ public CacheManager getCacheManager() {
101104
*/
102105
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) {
103106
Assert.notEmpty(cacheOperationSources);
104-
this.cacheOperationSource =
105-
(cacheOperationSources.length > 1 ?
106-
new CompositeCacheOperationSource(cacheOperationSources) :
107-
cacheOperationSources[0]);
107+
this.cacheOperationSource = (cacheOperationSources.length > 1 ?
108+
new CompositeCacheOperationSource(cacheOperationSources) : cacheOperationSources[0]);
108109
}
109110

110111
/**
@@ -141,6 +142,7 @@ public void afterPropertiesSet() {
141142
this.initialized = true;
142143
}
143144

145+
144146
/**
145147
* Convenience method to return a String representation of this Method
146148
* for use in logging. Can be overridden in subclasses to provide a
@@ -191,34 +193,26 @@ protected Object execute(Invoker invoker, Object target, Method method, Object[]
191193
// analyze caching information
192194
if (!CollectionUtils.isEmpty(cacheOp)) {
193195
Map<String, Collection<CacheOperationContext>> ops = createOperationContext(cacheOp, method, args, target, targetClass);
194-
195196
// start with evictions
196197
inspectBeforeCacheEvicts(ops.get(EVICT));
197-
198198
// follow up with cacheable
199199
CacheStatus status = inspectCacheables(ops.get(CACHEABLE));
200-
201-
Object retVal = null;
200+
Object retVal;
202201
Map<CacheOperationContext, Object> updates = inspectCacheUpdates(ops.get(UPDATE));
203-
204202
if (status != null) {
205203
if (status.updateRequired) {
206-
updates.putAll(status.cUpdates);
204+
updates.putAll(status.cacheUpdates);
207205
}
208206
// return cached object
209207
else {
210208
return status.retVal;
211209
}
212210
}
213-
214211
retVal = invoker.invoke();
215-
216212
inspectAfterCacheEvicts(ops.get(EVICT), retVal);
217-
218213
if (!updates.isEmpty()) {
219214
update(updates, retVal);
220215
}
221-
222216
return retVal;
223217
}
224218

@@ -229,21 +223,15 @@ private void inspectBeforeCacheEvicts(Collection<CacheOperationContext> eviction
229223
inspectCacheEvicts(evictions, true, ExpressionEvaluator.NO_RESULT);
230224
}
231225

232-
private void inspectAfterCacheEvicts(Collection<CacheOperationContext> evictions,
233-
Object result) {
226+
private void inspectAfterCacheEvicts(Collection<CacheOperationContext> evictions, Object result) {
234227
inspectCacheEvicts(evictions, false, result);
235228
}
236229

237-
private void inspectCacheEvicts(Collection<CacheOperationContext> evictions,
238-
boolean beforeInvocation, Object result) {
239-
230+
private void inspectCacheEvicts(Collection<CacheOperationContext> evictions, boolean beforeInvocation, Object result) {
240231
if (!evictions.isEmpty()) {
241-
242232
boolean log = logger.isTraceEnabled();
243-
244233
for (CacheOperationContext context : evictions) {
245234
CacheEvictOperation evictOp = (CacheEvictOperation) context.operation;
246-
247235
if (beforeInvocation == evictOp.isBeforeInvocation()) {
248236
if (context.isConditionPassing(result)) {
249237
// for each cache
@@ -257,7 +245,8 @@ private void inspectCacheEvicts(Collection<CacheOperationContext> evictions,
257245
if (log) {
258246
logger.trace("Invalidating entire cache for operation " + evictOp + " on method " + context.method);
259247
}
260-
} else {
248+
}
249+
else {
261250
// check key
262251
if (key == null) {
263252
key = context.generateKey();
@@ -268,7 +257,8 @@ private void inspectCacheEvicts(Collection<CacheOperationContext> evictions,
268257
cache.evict(key);
269258
}
270259
}
271-
} else {
260+
}
261+
else {
272262
if (log) {
273263
logger.trace("Cache condition failed on method " + context.method + " for operation " + context.operation);
274264
}
@@ -279,20 +269,17 @@ private void inspectCacheEvicts(Collection<CacheOperationContext> evictions,
279269
}
280270

281271
private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheables) {
282-
Map<CacheOperationContext, Object> cUpdates = new LinkedHashMap<CacheOperationContext, Object>(cacheables.size());
283-
272+
Map<CacheOperationContext, Object> cacheUpdates = new LinkedHashMap<CacheOperationContext, Object>(cacheables.size());
284273
boolean updateRequired = false;
285274
Object retVal = null;
286275

287276
if (!cacheables.isEmpty()) {
288277
boolean log = logger.isTraceEnabled();
289278
boolean atLeastOnePassed = false;
290-
291279
for (CacheOperationContext context : cacheables) {
292280
if (context.isConditionPassing()) {
293281
atLeastOnePassed = true;
294282
Object key = context.generateKey();
295-
296283
if (log) {
297284
logger.trace("Computed cache key " + key + " for operation " + context.operation);
298285
}
@@ -301,12 +288,9 @@ private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheabl
301288
"Null key returned for cache operation (maybe you are using named params on classes without debug info?) "
302289
+ context.operation);
303290
}
304-
305291
// add op/key (in case an update is discovered later on)
306-
cUpdates.put(context, key);
307-
292+
cacheUpdates.put(context, key);
308293
boolean localCacheHit = false;
309-
310294
// check whether the cache needs to be inspected or not (the method will be invoked anyway)
311295
if (!updateRequired) {
312296
for (Cache cache : context.getCaches()) {
@@ -318,7 +302,6 @@ private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheabl
318302
}
319303
}
320304
}
321-
322305
if (!localCacheHit) {
323306
updateRequired = true;
324307
}
@@ -332,38 +315,20 @@ private CacheStatus inspectCacheables(Collection<CacheOperationContext> cacheabl
332315

333316
// return a status only if at least on cacheable matched
334317
if (atLeastOnePassed) {
335-
return new CacheStatus(cUpdates, updateRequired, retVal);
318+
return new CacheStatus(cacheUpdates, updateRequired, retVal);
336319
}
337320
}
338321

339322
return null;
340323
}
341324

342-
private static class CacheStatus {
343-
// caches/key
344-
final Map<CacheOperationContext, Object> cUpdates;
345-
final boolean updateRequired;
346-
final Object retVal;
347-
348-
CacheStatus(Map<CacheOperationContext, Object> cUpdates, boolean updateRequired, Object retVal) {
349-
this.cUpdates = cUpdates;
350-
this.updateRequired = updateRequired;
351-
this.retVal = retVal;
352-
}
353-
}
354-
355325
private Map<CacheOperationContext, Object> inspectCacheUpdates(Collection<CacheOperationContext> updates) {
356-
357-
Map<CacheOperationContext, Object> cUpdates = new LinkedHashMap<CacheOperationContext, Object>(updates.size());
358-
326+
Map<CacheOperationContext, Object> cacheUpdates = new LinkedHashMap<CacheOperationContext, Object>(updates.size());
359327
if (!updates.isEmpty()) {
360328
boolean log = logger.isTraceEnabled();
361-
362329
for (CacheOperationContext context : updates) {
363330
if (context.isConditionPassing()) {
364-
365331
Object key = context.generateKey();
366-
367332
if (log) {
368333
logger.trace("Computed cache key " + key + " for operation " + context.operation);
369334
}
@@ -372,9 +337,8 @@ private Map<CacheOperationContext, Object> inspectCacheUpdates(Collection<CacheO
372337
"Null key returned for cache operation (maybe you are using named params on classes without debug info?) "
373338
+ context.operation);
374339
}
375-
376340
// add op/key (in case an update is discovered later on)
377-
cUpdates.put(context, key);
341+
cacheUpdates.put(context, key);
378342
}
379343
else {
380344
if (log) {
@@ -383,8 +347,7 @@ private Map<CacheOperationContext, Object> inspectCacheUpdates(Collection<CacheO
383347
}
384348
}
385349
}
386-
387-
return cUpdates;
350+
return cacheUpdates;
388351
}
389352

390353
private void update(Map<CacheOperationContext, Object> updates, Object retVal) {
@@ -400,6 +363,7 @@ private void update(Map<CacheOperationContext, Object> updates, Object retVal) {
400363

401364
private Map<String, Collection<CacheOperationContext>> createOperationContext(Collection<CacheOperation> cacheOp,
402365
Method method, Object[] args, Object target, Class<?> targetClass) {
366+
403367
Map<String, Collection<CacheOperationContext>> map = new LinkedHashMap<String, Collection<CacheOperationContext>>(3);
404368

405369
Collection<CacheOperationContext> cacheables = new ArrayList<CacheOperationContext>();
@@ -429,6 +393,13 @@ private Map<String, Collection<CacheOperationContext>> createOperationContext(Co
429393
return map;
430394
}
431395

396+
397+
public interface Invoker {
398+
399+
Object invoke();
400+
}
401+
402+
432403
protected class CacheOperationContext {
433404

434405
private final CacheOperation operation;
@@ -501,4 +472,22 @@ protected Collection<Cache> getCaches() {
501472
return this.caches;
502473
}
503474
}
475+
476+
477+
private static class CacheStatus {
478+
479+
// caches/key
480+
final Map<CacheOperationContext, Object> cacheUpdates;
481+
482+
final boolean updateRequired;
483+
484+
final Object retVal;
485+
486+
CacheStatus(Map<CacheOperationContext, Object> cacheUpdates, boolean updateRequired, Object retVal) {
487+
this.cacheUpdates = cacheUpdates;
488+
this.updateRequired = updateRequired;
489+
this.retVal = retVal;
490+
}
491+
}
492+
504493
}

0 commit comments

Comments
 (0)