org.springframework.cache.interceptor
Class AbstractFallbackCacheOperationSource

java.lang.Object
  extended by org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource
All Implemented Interfaces:
CacheOperationSource
Direct Known Subclasses:
AnnotationCacheOperationSource

public abstract class AbstractFallbackCacheOperationSource
extends java.lang.Object
implements CacheOperationSource

Abstract implementation of CacheOperation that caches attributes for methods and implements a fallback policy: 1. specific target method; 2. target class; 3. declaring method; 4. declaring class/interface.

Defaults to using the target class's caching attribute if none is associated with the target method. Any caching attribute associated with the target method completely overrides a class caching attribute. If none found on the target class, the interface that the invoked method has been called through (in case of a JDK proxy) will be checked.

This implementation caches attributes by method after they are first used. If it is ever desirable to allow dynamic changing of cacheable attributes (which is very unlikely), caching could be made configurable.

Since:
3.1
Author:
Costin Leau

Nested Class Summary
private static class AbstractFallbackCacheOperationSource.DefaultCacheKey
          Default cache key for the CacheOperation cache.
 
Field Summary
(package private)  java.util.Map<java.lang.Object,java.util.Collection<CacheOperation>> attributeCache
          Cache of CacheOperations, keyed by DefaultCacheKey (Method + target Class).
protected  Log logger
          Logger available to subclasses.
private static java.util.Collection<CacheOperation> NULL_CACHING_ATTRIBUTE
          Canonical value held in cache to indicate no caching attribute was found for this method and we don't need to look again.
 
Constructor Summary
AbstractFallbackCacheOperationSource()
           
 
Method Summary
protected  boolean allowPublicMethodsOnly()
          Should only public methods be allowed to have caching semantics?
private  java.util.Collection<CacheOperation> computeCacheOperations(java.lang.reflect.Method method, java.lang.Class<?> targetClass)
           
protected abstract  java.util.Collection<CacheOperation> findCacheOperations(java.lang.Class<?> clazz)
          Subclasses need to implement this to return the caching attribute for the given class, if any.
protected abstract  java.util.Collection<CacheOperation> findCacheOperations(java.lang.reflect.Method method)
          Subclasses need to implement this to return the caching attribute for the given method, if any.
protected  java.lang.Object getCacheKey(java.lang.reflect.Method method, java.lang.Class<?> targetClass)
          Determine a cache key for the given method and target class.
 java.util.Collection<CacheOperation> getCacheOperations(java.lang.reflect.Method method, java.lang.Class<?> targetClass)
          Determine the caching attribute for this method invocation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL_CACHING_ATTRIBUTE

private static final java.util.Collection<CacheOperation> NULL_CACHING_ATTRIBUTE
Canonical value held in cache to indicate no caching attribute was found for this method and we don't need to look again.


logger

protected final Log logger
Logger available to subclasses.

As this base class is not marked Serializable, the logger will be recreated after serialization - provided that the concrete subclass is Serializable.


attributeCache

final java.util.Map<java.lang.Object,java.util.Collection<CacheOperation>> attributeCache
Cache of CacheOperations, keyed by DefaultCacheKey (Method + target Class).

As this base class is not marked Serializable, the cache will be recreated after serialization - provided that the concrete subclass is Serializable.

Constructor Detail

AbstractFallbackCacheOperationSource

public AbstractFallbackCacheOperationSource()
Method Detail

getCacheOperations

public java.util.Collection<CacheOperation> getCacheOperations(java.lang.reflect.Method method,
                                                               java.lang.Class<?> targetClass)
Determine the caching attribute for this method invocation.

Defaults to the class's caching attribute if no method attribute is found.

Specified by:
getCacheOperations in interface CacheOperationSource
Parameters:
method - the method for the current invocation (never null)
targetClass - the target class for this invocation (may be null)
Returns:
CacheOperation for this method, or null if the method is not cacheable

getCacheKey

protected java.lang.Object getCacheKey(java.lang.reflect.Method method,
                                       java.lang.Class<?> targetClass)
Determine a cache key for the given method and target class.

Must not produce same key for overloaded methods. Must produce same key for different instances of the same method.

Parameters:
method - the method (never null)
targetClass - the target class (may be null)
Returns:
the cache key (never null)

computeCacheOperations

private java.util.Collection<CacheOperation> computeCacheOperations(java.lang.reflect.Method method,
                                                                    java.lang.Class<?> targetClass)

findCacheOperations

protected abstract java.util.Collection<CacheOperation> findCacheOperations(java.lang.reflect.Method method)
Subclasses need to implement this to return the caching attribute for the given method, if any.

Parameters:
method - the method to retrieve the attribute for
Returns:
all caching attribute associated with this method (or null if none)

findCacheOperations

protected abstract java.util.Collection<CacheOperation> findCacheOperations(java.lang.Class<?> clazz)
Subclasses need to implement this to return the caching attribute for the given class, if any.

Parameters:
clazz - the class to retrieve the attribute for
Returns:
all caching attribute associated with this class (or null if none)

allowPublicMethodsOnly

protected boolean allowPublicMethodsOnly()
Should only public methods be allowed to have caching semantics?

The default implementation returns false.