Class CacheAspectSupport
- All Implemented Interfaces:
Aware, BeanFactoryAware, InitializingBean, SmartInitializingSingleton
- Direct Known Subclasses:
CacheInterceptor
CacheInterceptor or an
AspectJ aspect.
This enables the underlying Spring caching infrastructure to be used easily to implement an aspect for any aspect system.
Subclasses are responsible for calling relevant methods in the correct order.
Uses the Strategy design pattern. A CacheOperationSource is
used for determining caching operations, a KeyGenerator will build the
cache keys, and a CacheResolver will resolve the actual cache(s) to use.
Note: A cache aspect is serializable but does not perform any actual caching after deserialization.
- Since:
- 3.1
- Author:
- Costin Leau, Juergen Hoeller, Chris Beams, Phillip Webb, Sam Brannen, Stephane Nicoll, Sebastien Deleuze
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classACacheOperationInvocationContextcontext for aCacheOperation.protected static classMetadata of a cache operation that does not depend on a particular invocation which makes it a good candidate for caching. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSystem property that instructs Spring's caching infrastructure to ignore the presence of Reactive Streams, in particular Reactor'sMono/FluxinCacheablemethod return type declarations.protected final org.apache.commons.logging.LogFields inherited from class AbstractCacheInvoker
errorHandler -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.voidInvoked right at the end of the singleton pre-instantiation phase, with a guarantee that all regular singleton beans have been created already.protected voidClear the cached metadata.voidconfigure(@Nullable Supplier<? extends @Nullable CacheErrorHandler> errorHandler, @Nullable Supplier<? extends @Nullable KeyGenerator> keyGenerator, @Nullable Supplier<? extends @Nullable CacheResolver> cacheResolver, @Nullable Supplier<? extends @Nullable CacheManager> cacheManager) Configure this aspect with the given error handler, key generator and cache resolver/manager suppliers, applying the corresponding default if a supplier is not resolvable.protected <T> TRetrieve a bean with the specified name and type.getCacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass) Return theCacheAspectSupport.CacheOperationMetadatafor the specified operation.Return the CacheOperationSource for this cache aspect.Return the defaultCacheResolverthat this cache aspect delegates to.protected Collection<? extends Cache> getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver) Return the defaultKeyGeneratorthat this cache aspect delegates to.protected CacheAspectSupport.CacheOperationContextgetOperationContext(CacheOperation operation, Method method, @Nullable Object[] args, Object target, Class<?> targetClass) invokeOperation(CacheOperationInvoker invoker) Execute the underlying operation (typically in case of cache miss) and return the result of the invocation.protected StringmethodIdentification(Method method, Class<?> targetClass) Convenience method to return a String representation of this Method for use in logging.voidsetBeanFactory(BeanFactory beanFactory) Set the containingBeanFactoryforCacheManagerand other service lookups.voidsetCacheManager(CacheManager cacheManager) Set theCacheManagerto use to create a defaultCacheResolver.voidsetCacheOperationSource(@Nullable CacheOperationSource cacheOperationSource) Set the CacheOperationSource for this cache aspect.voidsetCacheOperationSources(CacheOperationSource... cacheOperationSources) Set one or more cache operation sources which are used to find the cache attributes.voidsetCacheResolver(@Nullable CacheResolver cacheResolver) Set the defaultCacheResolverthat this cache aspect should delegate to if no specific cache resolver has been set for the operation.voidsetKeyGenerator(KeyGenerator keyGenerator) Set the defaultKeyGeneratorthat this cache aspect should delegate to if no specific key generator has been set for the operation.Methods inherited from class AbstractCacheInvoker
doClear, doEvict, doGet, doGet, doPut, doRetrieve, doRetrieve, getErrorHandler, setErrorHandler
-
Field Details
-
IGNORE_REACTIVESTREAMS_PROPERTY_NAME
System property that instructs Spring's caching infrastructure to ignore the presence of Reactive Streams, in particular Reactor'sMono/FluxinCacheablemethod return type declarations.By default, as of 6.1, Reactive Streams Publishers such as Reactor's
Mono/Fluxwill be specifically processed for asynchronous caching of their produced values rather than trying to cache the returnedPublisherinstances themselves.Switch this flag to "true" in order to ignore Reactive Streams Publishers and process them as regular return values through synchronous caching, restoring 6.0 behavior. Note that this is not recommended and only works in very limited scenarios, for example, with manual
Mono.cache()/Flux.cache()calls.- Since:
- 6.1.3
- See Also:
-
logger
protected final org.apache.commons.logging.Log logger
-
-
Constructor Details
-
CacheAspectSupport
protected CacheAspectSupport()
-
-
Method Details
-
configure
public void configure(@Nullable Supplier<? extends @Nullable CacheErrorHandler> errorHandler, @Nullable Supplier<? extends @Nullable KeyGenerator> keyGenerator, @Nullable Supplier<? extends @Nullable CacheResolver> cacheResolver, @Nullable Supplier<? extends @Nullable CacheManager> cacheManager) Configure this aspect with the given error handler, key generator and cache resolver/manager suppliers, applying the corresponding default if a supplier is not resolvable.- Since:
- 5.1
-
setCacheOperationSources
Set one or more cache operation sources which are used to find the cache attributes. If more than one source is provided, they will be aggregated using aCompositeCacheOperationSource.- See Also:
-
setCacheOperationSource
Set the CacheOperationSource for this cache aspect.- Since:
- 5.1
- See Also:
-
getCacheOperationSource
Return the CacheOperationSource for this cache aspect. -
setKeyGenerator
Set the defaultKeyGeneratorthat this cache aspect should delegate to if no specific key generator has been set for the operation.The default is a
SimpleKeyGenerator. -
getKeyGenerator
Return the defaultKeyGeneratorthat this cache aspect delegates to. -
setCacheResolver
Set the defaultCacheResolverthat this cache aspect should delegate to if no specific cache resolver has been set for the operation.The default resolver resolves the caches against their names and the default cache manager.
- See Also:
-
getCacheResolver
Return the defaultCacheResolverthat this cache aspect delegates to. -
setCacheManager
Set theCacheManagerto use to create a defaultCacheResolver. Replace the currentCacheResolver, if any.- See Also:
-
setBeanFactory
Set the containingBeanFactoryforCacheManagerand other service lookups.- Specified by:
setBeanFactoryin interfaceBeanFactoryAware- Parameters:
beanFactory- owning BeanFactory (nevernull). The bean can immediately call methods on the factory.- Since:
- 4.3
- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()Description copied from interface:SmartInitializingSingletonInvoked right at the end of the singleton pre-instantiation phase, with a guarantee that all regular singleton beans have been created already.ListableBeanFactory.getBeansOfType(Class)calls within this method won't trigger accidental side effects during bootstrap.NOTE: This callback won't be triggered for singleton beans lazily initialized on demand after
BeanFactorybootstrap, and not for any other bean scope either. Carefully use it for beans with the intended bootstrap semantics only.- Specified by:
afterSingletonsInstantiatedin interfaceSmartInitializingSingleton
-
methodIdentification
Convenience method to return a String representation of this Method for use in logging. Can be overridden in subclasses to provide a different identifier for the given method.- Parameters:
method- the method we're interested intargetClass- class the method is on- Returns:
- log message identifying this method
- See Also:
-
getCaches
protected Collection<? extends Cache> getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver) -
getOperationContext
protected CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation, Method method, @Nullable Object[] args, Object target, Class<?> targetClass) -
getCacheOperationMetadata
protected CacheAspectSupport.CacheOperationMetadata getCacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass) Return theCacheAspectSupport.CacheOperationMetadatafor the specified operation.Resolve the
CacheResolverand theKeyGeneratorto be used for the operation.- Parameters:
operation- the operationmethod- the method on which the operation is invokedtargetClass- the target type- Returns:
- the resolved metadata for the operation
-
getBean
Retrieve a bean with the specified name and type. Used to resolve services that are referenced by name in aCacheOperation.- Parameters:
name- the name of the bean, as defined by the cache operationserviceType- the type expected by the operation's service reference- Returns:
- the bean matching the expected type, qualified by the given name
- Throws:
NoSuchBeanDefinitionException- if such bean does not exist- See Also:
-
clearMetadataCache
protected void clearMetadataCache()Clear the cached metadata. -
execute
-
invokeOperation
Execute the underlying operation (typically in case of cache miss) and return the result of the invocation. If an exception occurs it will be wrapped in aCacheOperationInvoker.ThrowableWrapper: the exception can be handled or modified but it must be wrapped in aCacheOperationInvoker.ThrowableWrapperas well.- Parameters:
invoker- the invoker handling the operation being cached- Returns:
- the result of the invocation
- See Also:
-