AnnotatedClassCacheableService.java 3.6 KB
Newer Older
C
Costin Leau 已提交
1
/*
C
Costin Leau 已提交
2
 * Copyright 2010-2011 the original author or authors.
C
Costin Leau 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cache.config;

import java.util.concurrent.atomic.AtomicLong;

C
Costin Leau 已提交
21
import org.springframework.cache.annotation.CacheDefinitions;
C
Costin Leau 已提交
22
import org.springframework.cache.annotation.CacheEvict;
C
Costin Leau 已提交
23
import org.springframework.cache.annotation.CachePut;
C
Costin Leau 已提交
24 25 26 27 28
import org.springframework.cache.annotation.Cacheable;

/**
 * @author Costin Leau
 */
C
Costin Leau 已提交
29
@Cacheable("default")
30
public class AnnotatedClassCacheableService implements CacheableService<Object> {
C
Costin Leau 已提交
31

C
Costin Leau 已提交
32 33
	private final AtomicLong counter = new AtomicLong();
	public static final AtomicLong nullInvocations = new AtomicLong();
C
Costin Leau 已提交
34 35 36 37 38 39 40 41 42

	public Object cache(Object arg1) {
		return counter.getAndIncrement();
	}

	public Object conditional(int field) {
		return null;
	}

C
Costin Leau 已提交
43
	@CacheEvict("default")
C
Costin Leau 已提交
44 45 46
	public void invalidate(Object arg1) {
	}

C
Costin Leau 已提交
47
	@CacheEvict(value = "default", key = "#p0")
48
	public void evict(Object arg1, Object arg2) {
C
Costin Leau 已提交
49 50
	}

C
Costin Leau 已提交
51
	@Cacheable(value = "default", key = "#p0")
C
Costin Leau 已提交
52 53 54
	public Object key(Object arg1, Object arg2) {
		return counter.getAndIncrement();
	}
C
Costin Leau 已提交
55

C
Costin Leau 已提交
56 57 58 59 60
	@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
	public Object name(Object arg1) {
		return counter.getAndIncrement();
	}

C
Costin Leau 已提交
61 62 63 64 65
	@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
	public Object rootVars(Object arg1) {
		return counter.getAndIncrement();
	}

C
Costin Leau 已提交
66
	@CachePut("default")
C
Costin Leau 已提交
67 68 69 70
	public Object update(Object arg1) {
		return counter.getAndIncrement();
	}

C
Costin Leau 已提交
71
	@CachePut(value = "default", condition = "#arg.equals(3)")
C
Costin Leau 已提交
72 73 74 75
	public Object conditionalUpdate(Object arg) {
		return arg;
	}

C
Costin Leau 已提交
76 77 78 79 80 81 82 83
	public Object nullValue(Object arg1) {
		nullInvocations.incrementAndGet();
		return null;
	}

	public Number nullInvocations() {
		return nullInvocations.get();
	}
C
Costin Leau 已提交
84 85 86 87 88 89 90 91

	public Long throwChecked(Object arg1) throws Exception {
		throw new UnsupportedOperationException(arg1.toString());
	}

	public Long throwUnchecked(Object arg1) {
		throw new UnsupportedOperationException();
	}
C
Costin Leau 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

	// multi annotations

	@CacheDefinitions(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
	public Object multiCache(Object arg1) {
		return counter.getAndIncrement();
	}

	@CacheDefinitions(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0") })
	public Object multiEvict(Object arg1) {
		return counter.getAndIncrement();
	}

	@CacheDefinitions(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
	public Object multiCacheAndEvict(Object arg1) {
		return counter.getAndIncrement();
	}

	@CacheDefinitions(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
	public Object multiConditionalCacheAndEvict(Object arg1) {
		return counter.getAndIncrement();
	}

	@CacheDefinitions(put = { @CachePut("primary"), @CachePut("secondary") })
	public Object multiUpdate(Object arg1) {
		return arg1;
	}
}