CacheInterceptor class final
The code that runs around every call of a cached method and does what its cache annotations say.
For one call it:
- removes the entries of
@CacheEvict(beforeInvocation: true); - looks the
@Cacheablekey up and, on a hit, returns the stored value without running the method (unless a@CachePuton the same method forces the call); - otherwise runs the method, stores the result for
@Cacheable(unless itsunlesssays no) and for@CachePut; - removes the entries of the other
@CacheEvicts, after a hit too.
For a method returning a Future, the value is stored when the future
completes (errors are never stored) and a hit returns a completed
future; every error, even of a SpEL expression, comes through the
future. Errors thrown by a cache go to the errorHandler, which by
default rethrows them to the caller.
The starter creates it as a bean and applies it with a
CacheAnnotationPointcut; applications never call it. Its defaults
(cacheManager, cacheResolver, keyGenerator, errorHandler) are
the ones passed in, else those of the CachingConfigurer bean, else
the CacheManager bean, a SimpleCacheResolver, SimpleKeyGenerator
and SimpleCacheErrorHandler. Build one yourself only to use caching
without the auto-configuration:
// A hand-made setup: caches in memory, applied to annotated methods.
final interceptor = CacheInterceptor(
cacheManager: InMemoryCacheManager(),
);
final advisor =
DefaultPointcutAdvisor(const CacheAnnotationPointcut(), interceptor);
Spring (Java):
CacheInterceptor interceptor = new CacheInterceptor();
interceptor.setCacheManager(new ConcurrentMapCacheManager());
interceptor.setCacheOperationSource(new AnnotationCacheOperationSource());
- Implemented types
Constructors
- CacheInterceptor({CacheManager? cacheManager, CacheResolver? cacheResolver, KeyGenerator? keyGenerator, CacheErrorHandler? errorHandler, BeanFactory? beanFactory, AnnotationCacheOperationSource? cacheOperationSource, BeanIntrospector? introspector, ExpressionParser? parser})
- An interceptor with the given defaults; every argument is optional.
Properties
- beanFactory → BeanFactory?
-
The container in which bean names from annotations, the
CachingConfigurerand theCacheManagerbean are looked up;nullwhen the interceptor was built without one.final - cacheManager → CacheManager
-
The cache manager of operations that name none: the one passed in,
else the
CachingConfigurer's, else theCacheManagerbean. Throws aStateErrorwhen there is neither a manager nor a beanFactory.no setter - cacheOperationSource → AnnotationCacheOperationSource
-
Turns the annotations of a method into the operations to apply.
final
- cacheResolver → CacheResolver
-
The cache resolver of operations that name neither a manager nor a
resolver: the one passed in, else the
CachingConfigurer's, else a SimpleCacheResolver of cacheManager.no setter - errorHandler → CacheErrorHandler
-
What happens when a cache throws: the handler passed in, else the
CachingConfigurer's, else SimpleCacheErrorHandler, which rethrows.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- introspector → BeanIntrospector
-
The generated accessors that let SpEL read properties and call
methods of
@Introspectedclasses (#book.isbn).final - keyGenerator → KeyGenerator
-
The key generator of operations with neither
keynorkeyGenerator: the one passed in, else theCachingConfigurer's, else SimpleKeyGenerator.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cacheOperations(
MethodSignature signature) → List< CacheOperation> -
The cache operations of the member
signature, checked; empty when it has none. Computed once per member. -
invoke(
MethodInvocation invocation) → Object? -
Runs around
invocationand returns the call's result.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited