1 /*
2  * Copyright 2010-2011 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  */
29 @Cacheable("default")
30 public class AnnotatedClassCacheableService implements CacheableService<Object> {
31 
32 	private final AtomicLong counter = new AtomicLong();
33 	public static final AtomicLong nullInvocations = new AtomicLong();
34 
cache(Object arg1)35 	public Object cache(Object arg1) {
36 		return counter.getAndIncrement();
37 	}
38 
conditional(int field)39 	public Object conditional(int field) {
40 		return null;
41 	}
42 
43 	@CacheEvict("default")
invalidate(Object arg1)44 	public void invalidate(Object arg1) {
45 	}
46 
47 	@CacheEvict("default")
evictWithException(Object arg1)48 	public void evictWithException(Object arg1) {
49 		throw new RuntimeException("exception thrown - evict should NOT occur");
50 	}
51 
52 	@CacheEvict(value = "default", allEntries = true)
evictAll(Object arg1)53 	public void evictAll(Object arg1) {
54 	}
55 
56 	@CacheEvict(value = "default", beforeInvocation = true)
evictEarly(Object arg1)57 	public void evictEarly(Object arg1) {
58 		throw new RuntimeException("exception thrown - evict should still occur");
59 	}
60 
61 	@CacheEvict(value = "default", key = "#p0")
evict(Object arg1, Object arg2)62 	public void evict(Object arg1, Object arg2) {
63 	}
64 
65 	@CacheEvict(value = "default", key = "#p0", beforeInvocation = true)
invalidateEarly(Object arg1, Object arg2)66 	public void invalidateEarly(Object arg1, Object arg2) {
67 		throw new RuntimeException("exception thrown - evict should still occur");
68 	}
69 
70 	@Cacheable(value = "default", key = "#p0")
key(Object arg1, Object arg2)71 	public Object key(Object arg1, Object arg2) {
72 		return counter.getAndIncrement();
73 	}
74 
75 	@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
name(Object arg1)76 	public Object name(Object arg1) {
77 		return counter.getAndIncrement();
78 	}
79 
80 	@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
rootVars(Object arg1)81 	public Object rootVars(Object arg1) {
82 		return counter.getAndIncrement();
83 	}
84 
85 	@CachePut("default")
update(Object arg1)86 	public Object update(Object arg1) {
87 		return counter.getAndIncrement();
88 	}
89 
90 	@CachePut(value = "default", condition = "#arg.equals(3)")
conditionalUpdate(Object arg)91 	public Object conditionalUpdate(Object arg) {
92 		return arg;
93 	}
94 
nullValue(Object arg1)95 	public Object nullValue(Object arg1) {
96 		nullInvocations.incrementAndGet();
97 		return null;
98 	}
99 
nullInvocations()100 	public Number nullInvocations() {
101 		return nullInvocations.get();
102 	}
103 
throwChecked(Object arg1)104 	public Long throwChecked(Object arg1) throws Exception {
105 		throw new UnsupportedOperationException(arg1.toString());
106 	}
107 
throwUnchecked(Object arg1)108 	public Long throwUnchecked(Object arg1) {
109 		throw new UnsupportedOperationException();
110 	}
111 
112 	// multi annotations
113 
114 	@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
multiCache(Object arg1)115 	public Object multiCache(Object arg1) {
116 		return counter.getAndIncrement();
117 	}
118 
119 	@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0") })
multiEvict(Object arg1)120 	public Object multiEvict(Object arg1) {
121 		return counter.getAndIncrement();
122 	}
123 
124 	@Caching(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
multiCacheAndEvict(Object arg1)125 	public Object multiCacheAndEvict(Object arg1) {
126 		return counter.getAndIncrement();
127 	}
128 
129 	@Caching(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
multiConditionalCacheAndEvict(Object arg1)130 	public Object multiConditionalCacheAndEvict(Object arg1) {
131 		return counter.getAndIncrement();
132 	}
133 
134 	@Caching(put = { @CachePut("primary"), @CachePut("secondary") })
multiUpdate(Object arg1)135 	public Object multiUpdate(Object arg1) {
136 		return arg1;
137 	}
138 }