提交 2d646ca7 编写于 作者: J Juergen Hoeller

Polishing

上级 12459193
...@@ -135,8 +135,8 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -135,8 +135,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
throw new IllegalStateException("'cacheManager' is required"); throw new IllegalStateException("'cacheManager' is required");
} }
if (this.cacheOperationSource == null) { if (this.cacheOperationSource == null) {
throw new IllegalStateException("The 'cacheOperationSources' property is required: " throw new IllegalStateException("The 'cacheOperationSources' property is required: " +
+ "If there are no cacheable methods, then don't use a cache aspect."); "If there are no cacheable methods, then don't use a cache aspect.");
} }
this.initialized = true; this.initialized = true;
...@@ -163,7 +163,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -163,7 +163,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
for (String cacheName : cacheNames) { for (String cacheName : cacheNames) {
Cache cache = this.cacheManager.getCache(cacheName); Cache cache = this.cacheManager.getCache(cacheName);
if (cache == null) { if (cache == null) {
throw new IllegalArgumentException("Cannot find cache named [" + cacheName + "] for " + operation); throw new IllegalArgumentException("Cannot find cache named '" + cacheName + "' for " + operation);
} }
caches.add(cache); caches.add(cache);
} }
...@@ -188,7 +188,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -188,7 +188,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
if (targetClass == null && target != null) { if (targetClass == null && target != null) {
targetClass = target.getClass(); targetClass = target.getClass();
} }
final Collection<CacheOperation> cacheOp = getCacheOperationSource().getCacheOperations(method, targetClass); Collection<CacheOperation> cacheOp = getCacheOperationSource().getCacheOperations(method, targetClass);
// analyze caching information // analyze caching information
if (!CollectionUtils.isEmpty(cacheOp)) { if (!CollectionUtils.isEmpty(cacheOp)) {
...@@ -237,7 +237,6 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -237,7 +237,6 @@ public abstract class CacheAspectSupport implements InitializingBean {
// for each cache // for each cache
// lazy key initialization // lazy key initialization
Object key = null; Object key = null;
for (Cache cache : context.getCaches()) { for (Cache cache : context.getCaches()) {
// cache-wide flush // cache-wide flush
if (evictOp.isCacheWide()) { if (evictOp.isCacheWide()) {
...@@ -284,9 +283,8 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -284,9 +283,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
logger.trace("Computed cache key " + key + " for operation " + context.operation); logger.trace("Computed cache key " + key + " for operation " + context.operation);
} }
if (key == null) { if (key == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("Null key returned for cache operation (maybe you " +
"Null key returned for cache operation (maybe you are using named params on classes without debug info?) " "are using named params on classes without debug info?) " + context.operation);
+ context.operation);
} }
// add op/key (in case an update is discovered later on) // add op/key (in case an update is discovered later on)
cacheUpdates.put(context, key); cacheUpdates.put(context, key);
...@@ -313,7 +311,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -313,7 +311,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
} }
} }
// return a status only if at least on cacheable matched // return a status only if at least one cacheable matched
if (atLeastOnePassed) { if (atLeastOnePassed) {
return new CacheStatus(cacheUpdates, updateRequired, retVal); return new CacheStatus(cacheUpdates, updateRequired, retVal);
} }
...@@ -333,9 +331,8 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -333,9 +331,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
logger.trace("Computed cache key " + key + " for operation " + context.operation); logger.trace("Computed cache key " + key + " for operation " + context.operation);
} }
if (key == null) { if (key == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("Null key returned for cache operation (maybe you " +
"Null key returned for cache operation (maybe you are using named params on classes without debug info?) " "are using named params on classes without debug info?) " + context.operation);
+ context.operation);
} }
// add op/key (in case an update is discovered later on) // add op/key (in case an update is discovered later on)
cacheUpdates.put(context, key); cacheUpdates.put(context, key);
...@@ -353,7 +350,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -353,7 +350,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
private void update(Map<CacheOperationContext, Object> updates, Object retVal) { private void update(Map<CacheOperationContext, Object> updates, Object retVal) {
for (Map.Entry<CacheOperationContext, Object> entry : updates.entrySet()) { for (Map.Entry<CacheOperationContext, Object> entry : updates.entrySet()) {
CacheOperationContext operationContext = entry.getKey(); CacheOperationContext operationContext = entry.getKey();
if(operationContext.canPutToCache(retVal)) { if (operationContext.canPutToCache(retVal)) {
for (Cache cache : operationContext.getCaches()) { for (Cache cache : operationContext.getCaches()) {
cache.put(entry.getValue(), retVal); cache.put(entry.getValue(), retVal);
} }
...@@ -361,36 +358,31 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -361,36 +358,31 @@ public abstract class CacheAspectSupport implements InitializingBean {
} }
} }
private Map<String, Collection<CacheOperationContext>> createOperationContext(Collection<CacheOperation> cacheOp, private Map<String, Collection<CacheOperationContext>> createOperationContext(
Method method, Object[] args, Object target, Class<?> targetClass) { Collection<CacheOperation> cacheOperations, Method method, Object[] args, Object target, Class<?> targetClass) {
Map<String, Collection<CacheOperationContext>> map = new LinkedHashMap<String, Collection<CacheOperationContext>>(3);
Map<String, Collection<CacheOperationContext>> result = new LinkedHashMap<String, Collection<CacheOperationContext>>(3);
Collection<CacheOperationContext> cacheables = new ArrayList<CacheOperationContext>(); Collection<CacheOperationContext> cacheables = new ArrayList<CacheOperationContext>();
Collection<CacheOperationContext> evicts = new ArrayList<CacheOperationContext>(); Collection<CacheOperationContext> evicts = new ArrayList<CacheOperationContext>();
Collection<CacheOperationContext> updates = new ArrayList<CacheOperationContext>(); Collection<CacheOperationContext> updates = new ArrayList<CacheOperationContext>();
for (CacheOperation cacheOperation : cacheOp) { for (CacheOperation cacheOperation : cacheOperations) {
CacheOperationContext opContext = getOperationContext(cacheOperation, method, args, target, targetClass); CacheOperationContext opContext = getOperationContext(cacheOperation, method, args, target, targetClass);
if (cacheOperation instanceof CacheableOperation) { if (cacheOperation instanceof CacheableOperation) {
cacheables.add(opContext); cacheables.add(opContext);
} }
if (cacheOperation instanceof CacheEvictOperation) { if (cacheOperation instanceof CacheEvictOperation) {
evicts.add(opContext); evicts.add(opContext);
} }
if (cacheOperation instanceof CachePutOperation) { if (cacheOperation instanceof CachePutOperation) {
updates.add(opContext); updates.add(opContext);
} }
} }
map.put(CACHEABLE, cacheables); result.put(CACHEABLE, cacheables);
map.put(EVICT, evicts); result.put(EVICT, evicts);
map.put(UPDATE, updates); result.put(UPDATE, updates);
return result;
return map;
} }
...@@ -430,8 +422,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -430,8 +422,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
protected boolean isConditionPassing(Object result) { protected boolean isConditionPassing(Object result) {
if (StringUtils.hasText(this.operation.getCondition())) { if (StringUtils.hasText(this.operation.getCondition())) {
EvaluationContext evaluationContext = createEvaluationContext(result); EvaluationContext evaluationContext = createEvaluationContext(result);
return evaluator.condition(this.operation.getCondition(), this.method, return evaluator.condition(this.operation.getCondition(), this.method, evaluationContext);
evaluationContext);
} }
return true; return true;
} }
...@@ -444,7 +435,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -444,7 +435,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
else if (this.operation instanceof CachePutOperation) { else if (this.operation instanceof CachePutOperation) {
unless = ((CachePutOperation) this.operation).getUnless(); unless = ((CachePutOperation) this.operation).getUnless();
} }
if(StringUtils.hasText(unless)) { if (StringUtils.hasText(unless)) {
EvaluationContext evaluationContext = createEvaluationContext(value); EvaluationContext evaluationContext = createEvaluationContext(value);
return !evaluator.unless(unless, this.method, evaluationContext); return !evaluator.unless(unless, this.method, evaluationContext);
} }
...@@ -464,8 +455,7 @@ public abstract class CacheAspectSupport implements InitializingBean { ...@@ -464,8 +455,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
} }
private EvaluationContext createEvaluationContext(Object result) { private EvaluationContext createEvaluationContext(Object result) {
return evaluator.createEvaluationContext(this.caches, this.method, this.args, return evaluator.createEvaluationContext(this.caches, this.method, this.args, this.target, this.targetClass, result);
this.target, this.targetClass, result);
} }
protected Collection<Cache> getCaches() { protected Collection<Cache> getCaches() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册