Package org.springframework.cache.interceptor

# Class CacheAspectSupport

java.lang.Object
org.springframework.cache.interceptor.AbstractCacheInvoker
org.springframework.cache.interceptor.CacheAspectSupport

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanFactoryAware`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.beans.factory.SmartInitializingSingleton`

Direct Known Subclasses:
  `CacheInterceptor`

public abstract class CacheAspectSupport extends AbstractCacheInvoker implements org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.SmartInitializingSingleton
Base class for caching aspects, such as the `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 Classes
Modifier and Type
Class
Description
`protected class`
`CacheAspectSupport.CacheOperationContext`
A `CacheOperationInvocationContext` context for a `CacheOperation`.
`protected static class`
`CacheAspectSupport.CacheOperationMetadata`
Metadata of a cache operation that does not depend on a particular invocation which makes it a good candidate for caching.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`IGNORE_REACTIVESTREAMS_PROPERTY_NAME`
System property that instructs Spring's caching infrastructure to ignore the presence of Reactive Streams, in particular Reactor's `Mono`/`Flux` in `Cacheable` method return type declarations.
`protected final org.apache.commons.logging.Log`
`logger`

### Fields inherited from class org.springframework.cache.interceptor.AbstractCacheInvoker

`errorHandler`

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`CacheAspectSupport()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
`void`
`afterSingletonsInstantiated()`
`protected void`
`clearMetadataCache()`
Clear the cached metadata.
`void`
`configure(Supplier<CacheErrorHandler> errorHandler, Supplier<KeyGenerator> keyGenerator, Supplier<CacheResolver> cacheResolver, Supplier<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 Object`
`execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args)`
`protected <T> T`
`getBean(String name, Class<T> serviceType)`
Retrieve a bean with the specified name and type.
`protected CacheAspectSupport.CacheOperationMetadata`
`getCacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass)`
Return the `CacheAspectSupport.CacheOperationMetadata` for the specified operation.
`CacheOperationSource`
`getCacheOperationSource()`
Return the CacheOperationSource for this cache aspect.
`CacheResolver`
`getCacheResolver()`
Return the default `CacheResolver` that this cache aspect delegates to.
`protected Collection<? extends Cache>`
`getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver)`
`KeyGenerator`
`getKeyGenerator()`
Return the default `KeyGenerator` that this cache aspect delegates to.
`protected CacheAspectSupport.CacheOperationContext`
`getOperationContext(CacheOperation operation, Method method, Object[] args, Object target, Class<?> targetClass)`
`protected Object`
`invokeOperation(CacheOperationInvoker invoker)`
Execute the underlying operation (typically in case of cache miss) and return the result of the invocation.
`protected String`
`methodIdentification(Method method, Class<?> targetClass)`
Convenience method to return a String representation of this Method for use in logging.
`void`
`setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)`
Set the containing `BeanFactory` for `CacheManager` and other service lookups.
`void`
`setCacheManager(CacheManager cacheManager)`
Set the `CacheManager` to use to create a default `CacheResolver`.
`void`
`setCacheOperationSource(CacheOperationSource cacheOperationSource)`
Set the CacheOperationSource for this cache aspect.
`void`
`setCacheOperationSources(CacheOperationSource... cacheOperationSources)`
Set one or more cache operation sources which are used to find the cache attributes.
`void`
`setCacheResolver(CacheResolver cacheResolver)`
Set the default `CacheResolver` that this cache aspect should delegate to if no specific cache resolver has been set for the operation.
`void`
`setKeyGenerator(KeyGenerator keyGenerator)`
Set the default `KeyGenerator` that this cache aspect should delegate to if no specific key generator has been set for the operation.

### Methods inherited from class org.springframework.cache.interceptor.AbstractCacheInvoker

`doClear, doEvict, doGet, doGet, doPut, doRetrieve, doRetrieve, getErrorHandler, setErrorHandler`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### IGNORE_REACTIVESTREAMS_PROPERTY_NAME

public static final String IGNORE_REACTIVESTREAMS_PROPERTY_NAME
System property that instructs Spring's caching infrastructure to ignore the presence of Reactive Streams, in particular Reactor's `Mono`/`Flux` in `Cacheable` method return type declarations.

By default, as of 6.1, Reactive Streams Publishers such as Reactor's `Mono`/`Flux` will be specifically processed for asynchronous caching of their produced values rather than trying to cache the returned `Publisher` instances 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:
  - `Publisher`
  - Constant Field Values

### logger

protected final org.apache.commons.logging.Log logger

## Constructor Details

### CacheAspectSupport

protected CacheAspectSupport()

## Method Details

### configure

public void configure(@Nullable Supplier<CacheErrorHandler> errorHandler, @Nullable Supplier<KeyGenerator> keyGenerator, @Nullable Supplier<CacheResolver> cacheResolver, @Nullable Supplier<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

public void setCacheOperationSources(CacheOperationSource... cacheOperationSources)
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 a `CompositeCacheOperationSource`.

See Also:
  - `setCacheOperationSource(org.springframework.cache.interceptor.CacheOperationSource)`

### setCacheOperationSource

public void setCacheOperationSource(@Nullable CacheOperationSource cacheOperationSource)
Set the CacheOperationSource for this cache aspect.

Since:
  5.1
See Also:
  - `setCacheOperationSources(org.springframework.cache.interceptor.CacheOperationSource...)`

### getCacheOperationSource

@Nullable public CacheOperationSource getCacheOperationSource()
Return the CacheOperationSource for this cache aspect.

### setKeyGenerator

public void setKeyGenerator(KeyGenerator keyGenerator)
Set the default `KeyGenerator` that this cache aspect should delegate to if no specific key generator has been set for the operation.

The default is a `SimpleKeyGenerator`.

### getKeyGenerator

public KeyGenerator getKeyGenerator()
Return the default `KeyGenerator` that this cache aspect delegates to.

### setCacheResolver

public void setCacheResolver(@Nullable CacheResolver cacheResolver)
Set the default `CacheResolver` that 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:
  - `setCacheManager(org.springframework.cache.CacheManager)`
  - `SimpleCacheResolver`

### getCacheResolver

@Nullable public CacheResolver getCacheResolver()
Return the default `CacheResolver` that this cache aspect delegates to.

### setCacheManager

public void setCacheManager(CacheManager cacheManager)
Set the `CacheManager` to use to create a default `CacheResolver`. Replace the current `CacheResolver`, if any.

See Also:
  - `setCacheResolver(org.springframework.cache.interceptor.CacheResolver)`
  - `SimpleCacheResolver`

### setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
Set the containing `BeanFactory` for `CacheManager` and other service lookups.

Specified by:
  `setBeanFactory` in interface `org.springframework.beans.factory.BeanFactoryAware`
Since:
  4.3

### afterPropertiesSet

public void afterPropertiesSet()

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### afterSingletonsInstantiated

public void afterSingletonsInstantiated()

Specified by:
  `afterSingletonsInstantiated` in interface `org.springframework.beans.factory.SmartInitializingSingleton`

### methodIdentification

protected String methodIdentification(Method method, Class<?> targetClass)
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 in
  `targetClass` - class the method is on
Returns:
  log message identifying this method
See Also:
  - `ClassUtils.getQualifiedMethodName(java.lang.reflect.Method)`

### getCaches

protected Collection<? extends Cache> getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver)

### getOperationContext

protected CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation, Method method, Object[] args, Object target, Class<?> targetClass)

### getCacheOperationMetadata

protected CacheAspectSupport.CacheOperationMetadata getCacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass)
Return the `CacheAspectSupport.CacheOperationMetadata` for the specified operation.

Resolve the `CacheResolver` and the `KeyGenerator` to be used for the operation.

Parameters:
  `operation` - the operation
  `method` - the method on which the operation is invoked
  `targetClass` - the target type
Returns:
  the resolved metadata for the operation

### getBean

protected <T> T getBean(String name, Class<T> serviceType)
Retrieve a bean with the specified name and type. Used to resolve services that are referenced by name in a `CacheOperation`.

Parameters:
  `name` - the name of the bean, as defined by the cache operation
  `serviceType` - the type expected by the operation's service reference
Returns:
  the bean matching the expected type, qualified by the given name
Throws:
  `org.springframework.beans.factory.NoSuchBeanDefinitionException` - if such bean does not exist
See Also:
  - `CacheOperation.getKeyGenerator()`
  - `CacheOperation.getCacheManager()`
  - `CacheOperation.getCacheResolver()`

### clearMetadataCache

protected void clearMetadataCache()
Clear the cached metadata.

### execute

@Nullable protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args)

### invokeOperation

@Nullable protected Object invokeOperation(CacheOperationInvoker invoker)
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 a `CacheOperationInvoker.ThrowableWrapper`: the exception can be handled or modified but it must be wrapped in a `CacheOperationInvoker.ThrowableWrapper` as well.

Parameters:
  `invoker` - the invoker handling the operation being cached
Returns:
  the result of the invocation
See Also:
  - `CacheOperationInvoker.invoke()`