@Target(value={METHOD,TYPE}) @Retention(value=RUNTIME) public @interface CacheResult
CacheResult
is invoked a
GeneratedCacheKey
will be generated and
Cache.get(Object)
is called before the annotated method
actually executes. If a value is found in the cache it is returned and the
annotated method is never actually executed. If no value is found the
annotated method is invoked and the returned value is stored in the cache
with the generated key.
null return values are cached by default, this behavior can be disabled by
setting the cacheNull()
property to false.
Exceptions are not cached by default. Caching of exceptions can be enabled by
specifying an exceptionCacheName()
. If an exception cache is specified
it is checked before invoking the annotated method and if a cached exception is
found it is re-thrown.
The cachedExceptions()
and nonCachedExceptions()
properties
can be used to control which exceptions are cached and which are not.
To always invoke the annotated method and still cache the result set
skipGet()
to true. This will disable the pre-invocation
Cache.get(Object)
call. If exceptionCacheName()
is
specified the pre-invocation exception check is also disabled. This feature is
useful for methods that create or update objects to be cached.
Example of caching the Domain object with a key generated from the
String
and int
parameters.
With no cacheName()
specified a cache name of
"my.app.DomainDao.getDomain(java.lang.String,int)" will be generated.
Example using thepackage my.app; public class DomainDao { @CacheResult public Domain getDomain(String domainId, int index) { ... } }
GeneratedCacheKey
annotation so that only the domainId
parameter is used in key generation:
If exception caching is enabled via specification ofpackage my.app; public class DomainDao { @CacheResult public Domain getDomain(@CacheKey String domainId, Monitor mon) { ... } }
exceptionCacheName()
the following rules are used to determine if a
thrown exception is cached:
cachedExceptions()
and nonCachedExceptions()
are both
empty then all exceptions are cachedcachedExceptions()
is specified and
nonCachedExceptions()
is not specified then only exceptions
which pass an instanceof check against the cachedExceptions list are cachednonCachedExceptions()
is specified and
cachedExceptions()
is not specified then all exceptions
which do not pass an instanceof check against the nonCachedExceptions list are
cachedcachedExceptions()
and nonCachedExceptions()
are both
specified then exceptions which pass an instanceof check against the
cachedExceptions list but do not pass an instanceof check against the
nonCachedExceptions list are cachedCacheKey
Modifier and Type | Optional Element and Description |
---|---|
Class<? extends Throwable>[] |
cachedExceptions
|
Class<? extends CacheKeyGenerator> |
cacheKeyGenerator
The
CacheKeyGenerator to use to generate the GeneratedCacheKey
for interacting with the specified Cache. |
String |
cacheName
The name of the cache.
|
boolean |
cacheNull
If set to false null return values will not be cached.
|
Class<? extends CacheResolverFactory> |
cacheResolverFactory
The
CacheResolverFactory used to find the CacheResolver to
use at runtime. |
String |
exceptionCacheName
The name of the cache to cache exceptions.
|
Class<? extends Throwable>[] |
nonCachedExceptions
|
boolean |
skipGet
If set to true the pre-invocation
Cache.get(Object) is
skipped and the annotated method is always executed with the returned value
being cached as normal. |
public abstract String cacheName
CacheDefaults.cacheName()
an if
that is not set it defaults to:
package.name.ClassName.methodName(package.ParameterType,package.ParameterType)public abstract boolean skipGet
Cache.get(Object)
is
skipped and the annotated method is always executed with the returned value
being cached as normal. This is useful for create or update methods which
should always be executed and have their returned value placed in the cache.
If true and an exceptionCacheName()
is specified the pre-invocation
check for a thrown exception is also skipped. If an exception is thrown during
invocation it will be cached following the standard exception caching rules.
Defaults to false.CachePut
public abstract boolean cacheNull
public abstract Class<? extends CacheResolverFactory> cacheResolverFactory
CacheResolverFactory
used to find the CacheResolver
to
use at runtime.
The default resolver pair will resolve the cache by name from the default
CacheManager
public abstract Class<? extends CacheKeyGenerator> cacheKeyGenerator
CacheKeyGenerator
to use to generate the GeneratedCacheKey
for interacting with the specified Cache.
Defaults to a key generator that uses
Arrays.deepHashCode(Object[])
and
Arrays.deepEquals(Object[], Object[])
with the array
returned by CacheKeyInvocationContext.getKeyParameters()
CacheKey
public abstract String exceptionCacheName
public abstract Class<? extends Throwable>[] cachedExceptions
classes
, which must be a
subclass of Throwable
, indicating which exception types which
must be cached. Only consulted if exceptionCacheName()
is
specified.public abstract Class<? extends Throwable>[] nonCachedExceptions
Classes
, which must be a
subclass of Throwable
, indicating which exception types
must not be cached. Only consulted if exceptionCacheName()
is specified.Copyright © 2013. All Rights Reserved.