javax.cache.annotation
Annotation Type CacheResult


@Target(value={METHOD,TYPE})
@Retention(value=RUNTIME)
public @interface CacheResult

When a method annotated with CacheResult is invoked a CacheKey will be generated and Cache.get(Object) is called before the invoked 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 and thrown exceptions are never 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.

 package my.app;
 
 public class DomainDao {
   @CacheResult
   public Domain getDomain(String domainId, int index) {
     ...
   }
 }
 

Example using the CacheKey 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) {
     ...
   }
 }
 

Since:
1.0
Author:
Eric Dalquist, Rick Hightower

Optional Element Summary
 Class<? extends CacheKeyGenerator> cacheKeyGenerator
          (Optional) The CacheKeyGenerator to use to generate the cache key used to call Cache.put(Object, Object)

Defaults to a key generator that uses Arrays.deepHashCode(Object[]) and Arrays.deepEquals(Object[], Object[]) with the array returned by CacheKeyInvocationContext.getKeyParameters()

 String cacheName
          (Optional) name of the cache.
 Class<? extends CacheResolverFactory> cacheResolverFactory
          (Optional) The CacheResolverFactory to use to find the CacheResolver the intercepter will interact with.
 boolean skipGet
          (Optional) If set to true the pre-invocation get is skipped and the annotated method is always executed with the returned value being cached as normal.
 

cacheName

public abstract String cacheName
(Optional) name of the cache.

If not specified defaults first to CacheDefaults.cacheName() an if that is not set it defaults to: package.name.ClassName.methodName(package.ParameterType,package.ParameterType)

Default:
""

skipGet

public abstract boolean skipGet
(Optional) If set to true the pre-invocation get 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.

Defaults to false

See Also:
CachePut
Default:
false

cacheResolverFactory

public abstract Class<? extends CacheResolverFactory> cacheResolverFactory
(Optional) The CacheResolverFactory to use to find the CacheResolver the intercepter will interact with.

Defaults to resolving the cache by name from the default CacheManager

Default:
javax.cache.annotation.CacheResolverFactory.class

cacheKeyGenerator

public abstract Class<? extends CacheKeyGenerator> cacheKeyGenerator
(Optional) The CacheKeyGenerator to use to generate the cache key used to call Cache.put(Object, Object)

Defaults to a key generator that uses Arrays.deepHashCode(Object[]) and Arrays.deepEquals(Object[], Object[]) with the array returned by CacheKeyInvocationContext.getKeyParameters()

Default:
javax.cache.annotation.CacheKeyGenerator.class


Copyright © 2011. All Rights Reserved.