Annotation Type CacheResult
-
@Target({METHOD,TYPE}) @Retention(RUNTIME) public @interface CacheResult
When a method annotated withCacheResult
is invoked aGeneratedCacheKey
will be generated andCache.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.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()
andnonCachedExceptions()
properties can be used to control the exceptions are cached and those that are not.To always invoke the annotated method and still cache the result set
skipGet()
to true. This will disable the pre-invocationCache.get(Object)
call. IfexceptionCacheName()
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
andint
parameters.With no
cacheName()
specified a cache name of "my.app.DomainDao.getDomain(java.lang.String,int)" will be generated.package my.app; public class DomainDao { @CacheResult public Domain getDomain(String domainId, int index) { ... } }
Example using the
GeneratedCacheKey
annotation so that only the domainId parameter is used in key generation:package my.app; public class DomainDao { @CacheResult public Domain getDomain(@CacheKey String domainId, Monitor mon) { ... } }
If exception caching is enabled via specification of
exceptionCacheName()
the following rules are used to determine if a thrown exception is cached:- If
cachedExceptions()
andnonCachedExceptions()
are both empty then all exceptions are cached - If
cachedExceptions()
is specified andnonCachedExceptions()
is not specified then only exceptions that pass an instanceof check against the cachedExceptions list are cached - If
nonCachedExceptions()
is specified andcachedExceptions()
is not specified then all exceptions that do not pass an instanceof check against the nonCachedExceptions list are cached - If
cachedExceptions()
andnonCachedExceptions()
are both specified then exceptions that pass an instanceof check against the cachedExceptions list but do not pass an instanceof check against the nonCachedExceptions list are cached
- Since:
- 1.0
- See Also:
CacheKey
- If
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.Class<? extends java.lang.Throwable>[]
cachedExceptions
Defines zero (0) or more exceptionclasses
, that must be a subclass ofThrowable
, indicating the exception types that must be cached.java.lang.Class<? extends CacheKeyGenerator>
cacheKeyGenerator
TheCacheKeyGenerator
to use to generate theGeneratedCacheKey
for interacting with the specified Cache.java.lang.String
cacheName
The name of the cache.java.lang.Class<? extends CacheResolverFactory>
cacheResolverFactory
TheCacheResolverFactory
used to find theCacheResolver
to use at runtime.java.lang.String
exceptionCacheName
The name of the cache to cache exceptions.java.lang.Class<? extends java.lang.Throwable>[]
nonCachedExceptions
Defines zero (0) or more exceptionClasses
, that must be a subclass ofThrowable
, indicating the exception types that must not be cached.boolean
skipGet
If set to true the pre-invocationCache.get(Object)
is skipped and the annotated method is always executed with the returned value being cached as normal.
-
-
-
Element Detail
-
cacheName
java.lang.String cacheName
The name of the cache.
If not specified defaults first toCacheDefaults.cacheName()
and if that is not set it defaults to: package.name.ClassName.methodName(package.ParameterType,package.ParameterType)- Default:
- ""
-
-
-
skipGet
boolean skipGet
If set to true the pre-invocationCache.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 that 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.
- See Also:
CachePut
- Default:
- false
-
-
-
cacheResolverFactory
java.lang.Class<? extends CacheResolverFactory> cacheResolverFactory
TheCacheResolverFactory
used to find theCacheResolver
to use at runtime.The default resolver pair will resolve the cache by name from the default
CacheManager
- Default:
- javax.cache.annotation.CacheResolverFactory.class
-
-
-
cacheKeyGenerator
java.lang.Class<? extends CacheKeyGenerator> cacheKeyGenerator
TheCacheKeyGenerator
to use to generate theGeneratedCacheKey
for interacting with the specified Cache.Defaults to a key generator that uses
Arrays.deepHashCode(Object[])
andArrays.deepEquals(Object[], Object[])
with the array returned byCacheKeyInvocationContext.getKeyParameters()
- See Also:
CacheKey
- Default:
- javax.cache.annotation.CacheKeyGenerator.class
-
-
-
cachedExceptions
java.lang.Class<? extends java.lang.Throwable>[] cachedExceptions
Defines zero (0) or more exceptionclasses
, that must be a subclass ofThrowable
, indicating the exception types that must be cached. Only consulted ifexceptionCacheName()
is specified.- Default:
- {}
-
-
-
nonCachedExceptions
java.lang.Class<? extends java.lang.Throwable>[] nonCachedExceptions
Defines zero (0) or more exceptionClasses
, that must be a subclass ofThrowable
, indicating the exception types that must not be cached. Only consulted ifexceptionCacheName()
is specified.- Default:
- {}
-
-