View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.cache.config;
18  
19  import java.util.concurrent.atomic.AtomicLong;
20  
21  import org.springframework.cache.annotation.CacheEvict;
22  import org.springframework.cache.annotation.CachePut;
23  import org.springframework.cache.annotation.Cacheable;
24  import org.springframework.cache.annotation.Caching;
25  
26  /**
27   * @author Costin Leau
28   * @author Phillip Webb
29   * @author Stephane Nicoll
30   */
31  @Cacheable("testCache")
32  public class AnnotatedClassCacheableService implements CacheableService<Object> {
33  
34  	private final AtomicLong counter = new AtomicLong();
35  
36  	public static final AtomicLong nullInvocations = new AtomicLong();
37  
38  	@Override
39  	public Object cache(Object arg1) {
40  		return counter.getAndIncrement();
41  	}
42  
43  	@Override
44  	public Object conditional(int field) {
45  		return null;
46  	}
47  
48  	@Override
49  	@Cacheable(value = "testCache", unless = "#result > 10")
50  	public Object unless(int arg) {
51  		return arg;
52  	}
53  
54  	@Override
55  	@CacheEvict("testCache")
56  	public void invalidate(Object arg1) {
57  	}
58  
59  	@Override
60  	@CacheEvict("testCache")
61  	public void evictWithException(Object arg1) {
62  		throw new RuntimeException("exception thrown - evict should NOT occur");
63  	}
64  
65  	@Override
66  	@CacheEvict(value = "testCache", allEntries = true)
67  	public void evictAll(Object arg1) {
68  	}
69  
70  	@Override
71  	@CacheEvict(value = "testCache", beforeInvocation = true)
72  	public void evictEarly(Object arg1) {
73  		throw new RuntimeException("exception thrown - evict should still occur");
74  	}
75  
76  	@Override
77  	@CacheEvict(value = "testCache", key = "#p0")
78  	public void evict(Object arg1, Object arg2) {
79  	}
80  
81  	@Override
82  	@CacheEvict(value = "testCache", key = "#p0", beforeInvocation = true)
83  	public void invalidateEarly(Object arg1, Object arg2) {
84  		throw new RuntimeException("exception thrown - evict should still occur");
85  	}
86  
87  	@Override
88  	@Cacheable(value = "testCache", key = "#p0")
89  	public Object key(Object arg1, Object arg2) {
90  		return counter.getAndIncrement();
91  	}
92  
93  	@Override
94  	@Cacheable(value = "testCache")
95  	public Object varArgsKey(Object... args) {
96  		return counter.getAndIncrement();
97  	}
98  
99  	@Override
100 	@Cacheable(value = "testCache", key = "#root.methodName + #root.caches[0].name")
101 	public Object name(Object arg1) {
102 		return counter.getAndIncrement();
103 	}
104 
105 	@Override
106 	@Cacheable(value = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
107 	public Object rootVars(Object arg1) {
108 		return counter.getAndIncrement();
109 	}
110 
111 	@Override
112 	@Cacheable(value = "testCache", keyGenerator = "customKyeGenerator")
113 	public Object customKeyGenerator(Object arg1) {
114 		return counter.getAndIncrement();
115 	}
116 
117 	@Override
118 	@Cacheable(value = "testCache", keyGenerator = "unknownBeanName")
119 	public Object unknownCustomKeyGenerator(Object arg1) {
120 		return counter.getAndIncrement();
121 	}
122 
123 	@Override
124 	@Cacheable(value = "testCache", cacheManager = "customCacheManager")
125 	public Object customCacheManager(Object arg1) {
126 		return counter.getAndIncrement();
127 	}
128 
129 	@Override
130 	@Cacheable(value = "testCache", cacheManager = "unknownBeanName")
131 	public Object unknownCustomCacheManager(Object arg1) {
132 		return counter.getAndIncrement();
133 	}
134 
135 	@Override
136 	@CachePut("testCache")
137 	public Object update(Object arg1) {
138 		return counter.getAndIncrement();
139 	}
140 
141 	@Override
142 	@CachePut(value = "testCache", condition = "#arg.equals(3)")
143 	public Object conditionalUpdate(Object arg) {
144 		return arg;
145 	}
146 
147 	@Override
148 	public Object nullValue(Object arg1) {
149 		nullInvocations.incrementAndGet();
150 		return null;
151 	}
152 
153 	@Override
154 	public Number nullInvocations() {
155 		return nullInvocations.get();
156 	}
157 
158 	@Override
159 	public Long throwChecked(Object arg1) throws Exception {
160 		throw new UnsupportedOperationException(arg1.toString());
161 	}
162 
163 	@Override
164 	public Long throwUnchecked(Object arg1) {
165 		throw new UnsupportedOperationException();
166 	}
167 
168 	// multi annotations
169 
170 	@Override
171 	@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
172 	public Object multiCache(Object arg1) {
173 		return counter.getAndIncrement();
174 	}
175 
176 	@Override
177 	@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#a0"),  @CacheEvict(value = "primary", key = "#p0 + 'A'") })
178 	public Object multiEvict(Object arg1) {
179 		return counter.getAndIncrement();
180 	}
181 
182 	@Override
183 	@Caching(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
184 	public Object multiCacheAndEvict(Object arg1) {
185 		return counter.getAndIncrement();
186 	}
187 
188 	@Override
189 	@Caching(cacheable = { @Cacheable(value = "primary", condition = "#a0 == 3") }, evict = { @CacheEvict("secondary") })
190 	public Object multiConditionalCacheAndEvict(Object arg1) {
191 		return counter.getAndIncrement();
192 	}
193 
194 	@Override
195 	@Caching(put = { @CachePut("primary"), @CachePut("secondary") })
196 	public Object multiUpdate(Object arg1) {
197 		return arg1;
198 	}
199 
200 	@Override
201 	@CachePut(value="primary", key="#result.id")
202 	public TestEntity putRefersToResult(TestEntity arg1) {
203 		arg1.setId(Long.MIN_VALUE);
204 		return arg1;
205 	}
206 }