001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.cache.annotation;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Inherited;
022import java.lang.annotation.Retention;
023import java.lang.annotation.RetentionPolicy;
024import java.lang.annotation.Target;
025import java.util.concurrent.Callable;
026
027import org.springframework.core.annotation.AliasFor;
028
029/**
030 * Annotation indicating that the result of invoking a method (or all methods
031 * in a class) can be cached.
032 *
033 * <p>Each time an advised method is invoked, caching behavior will be applied,
034 * checking whether the method has been already invoked for the given arguments.
035 * A sensible default simply uses the method parameters to compute the key, but
036 * a SpEL expression can be provided via the {@link #key} attribute, or a custom
037 * {@link org.springframework.cache.interceptor.KeyGenerator} implementation can
038 * replace the default one (see {@link #keyGenerator}).
039 *
040 * <p>If no value is found in the cache for the computed key, the target method
041 * will be invoked and the returned value stored in the associated cache. Note
042 * that Java8's {@code Optional} return types are automatically handled and its
043 * content is stored in the cache if present.
044 *
045 * <p>This annotation may be used as a <em>meta-annotation</em> to create custom
046 * <em>composed annotations</em> with attribute overrides.
047 *
048 * @author Costin Leau
049 * @author Phillip Webb
050 * @author Stephane Nicoll
051 * @author Sam Brannen
052 * @since 3.1
053 * @see CacheConfig
054 */
055@Target({ElementType.METHOD, ElementType.TYPE})
056@Retention(RetentionPolicy.RUNTIME)
057@Inherited
058@Documented
059public @interface Cacheable {
060
061        /**
062         * Alias for {@link #cacheNames}.
063         */
064        @AliasFor("cacheNames")
065        String[] value() default {};
066
067        /**
068         * Names of the caches in which method invocation results are stored.
069         * <p>Names may be used to determine the target cache (or caches), matching
070         * the qualifier value or bean name of a specific bean definition.
071         * @since 4.2
072         * @see #value
073         * @see CacheConfig#cacheNames
074         */
075        @AliasFor("value")
076        String[] cacheNames() default {};
077
078        /**
079         * Spring Expression Language (SpEL) expression for computing the key dynamically.
080         * <p>Default is {@code ""}, meaning all method parameters are considered as a key,
081         * unless a custom {@link #keyGenerator} has been configured.
082         * <p>The SpEL expression evaluates against a dedicated context that provides the
083         * following meta-data:
084         * <ul>
085         * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
086         * references to the {@link java.lang.reflect.Method method}, target object, and
087         * affected cache(s) respectively.</li>
088         * <li>Shortcuts for the method name ({@code #root.methodName}) and target class
089         * ({@code #root.targetClass}) are also available.
090         * <li>Method arguments can be accessed by index. For instance the second argument
091         * can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
092         * can also be accessed by name if that information is available.</li>
093         * </ul>
094         */
095        String key() default "";
096
097        /**
098         * The bean name of the custom {@link org.springframework.cache.interceptor.KeyGenerator}
099         * to use.
100         * <p>Mutually exclusive with the {@link #key} attribute.
101         * @see CacheConfig#keyGenerator
102         */
103        String keyGenerator() default "";
104
105        /**
106         * The bean name of the custom {@link org.springframework.cache.CacheManager} to use to
107         * create a default {@link org.springframework.cache.interceptor.CacheResolver} if none
108         * is set already.
109         * <p>Mutually exclusive with the {@link #cacheResolver}  attribute.
110         * @see org.springframework.cache.interceptor.SimpleCacheResolver
111         * @see CacheConfig#cacheManager
112         */
113        String cacheManager() default "";
114
115        /**
116         * The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver}
117         * to use.
118         * @see CacheConfig#cacheResolver
119         */
120        String cacheResolver() default "";
121
122        /**
123         * Spring Expression Language (SpEL) expression used for making the method
124         * caching conditional.
125         * <p>Default is {@code ""}, meaning the method result is always cached.
126         * <p>The SpEL expression evaluates against a dedicated context that provides the
127         * following meta-data:
128         * <ul>
129         * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
130         * references to the {@link java.lang.reflect.Method method}, target object, and
131         * affected cache(s) respectively.</li>
132         * <li>Shortcuts for the method name ({@code #root.methodName}) and target class
133         * ({@code #root.targetClass}) are also available.
134         * <li>Method arguments can be accessed by index. For instance the second argument
135         * can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
136         * can also be accessed by name if that information is available.</li>
137         * </ul>
138         */
139        String condition() default "";
140
141        /**
142         * Spring Expression Language (SpEL) expression used to veto method caching.
143         * <p>Unlike {@link #condition}, this expression is evaluated after the method
144         * has been called and can therefore refer to the {@code result}.
145         * <p>Default is {@code ""}, meaning that caching is never vetoed.
146         * <p>The SpEL expression evaluates against a dedicated context that provides the
147         * following meta-data:
148         * <ul>
149         * <li>{@code #result} for a reference to the result of the method invocation. For
150         * supported wrappers such as {@code Optional}, {@code #result} refers to the actual
151         * object, not the wrapper</li>
152         * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
153         * references to the {@link java.lang.reflect.Method method}, target object, and
154         * affected cache(s) respectively.</li>
155         * <li>Shortcuts for the method name ({@code #root.methodName}) and target class
156         * ({@code #root.targetClass}) are also available.
157         * <li>Method arguments can be accessed by index. For instance the second argument
158         * can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
159         * can also be accessed by name if that information is available.</li>
160         * </ul>
161         * @since 3.2
162         */
163        String unless() default "";
164
165        /**
166         * Synchronize the invocation of the underlying method if several threads are
167         * attempting to load a value for the same key. The synchronization leads to
168         * a couple of limitations:
169         * <ol>
170         * <li>{@link #unless()} is not supported</li>
171         * <li>Only one cache may be specified</li>
172         * <li>No other cache-related operation can be combined</li>
173         * </ol>
174         * This is effectively a hint and the actual cache provider that you are
175         * using may not support it in a synchronized fashion. Check your provider
176         * documentation for more details on the actual semantics.
177         * @since 4.3
178         * @see org.springframework.cache.Cache#get(Object, Callable)
179         */
180        boolean sync() default false;
181
182}