+ introduced @CacheUpdate annotation

+ introduced @CacheDefinition annotation
+ introduced meta-annotation to allow multiple @Cache annotations
SPR-7833
SPR-8082
This commit is contained in:
Costin Leau
2011-11-09 10:00:44 +00:00
parent c3f0f31243
commit eddb0ac3be
21 changed files with 540 additions and 136 deletions

View File

@@ -176,6 +176,31 @@ public abstract class AbstractAnnotationTests {
assertSame(r1, service.cache(null));
}
public void testCacheUpdate(CacheableService service) {
Object o = new Object();
Cache cache = cm.getCache("default");
assertNull(cache.get(o));
Object r1 = service.update(o);
assertSame(r1, cache.get(o).get());
o = new Object();
assertNull(cache.get(o));
Object r2 = service.update(o);
assertSame(r2, cache.get(o).get());
}
public void testConditionalCacheUpdate(CacheableService service) {
Integer one = Integer.valueOf(1);
Integer three = Integer.valueOf(3);
Cache cache = cm.getCache("default");
assertEquals(one, Integer.valueOf(service.conditionalUpdate(one).toString()));
assertNull(cache.get(one));
assertEquals(three, Integer.valueOf(service.conditionalUpdate(three).toString()));
assertEquals(three, Integer.valueOf(cache.get(three).get().toString()));
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
@@ -284,4 +309,24 @@ public abstract class AbstractAnnotationTests {
public void testClassUncheckedException() throws Exception {
testUncheckedThrowable(ccs);
}
@Test
public void testUpdate() {
testCacheUpdate(cs);
}
@Test
public void testClassUpdate() {
testCacheUpdate(ccs);
}
@Test
public void testConditionalUpdate() {
testConditionalCacheUpdate(cs);
}
@Test
public void testClassConditionalUpdate() {
testConditionalCacheUpdate(ccs);
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CacheUpdate;
import org.springframework.cache.annotation.Cacheable;
/**
@@ -61,6 +62,16 @@ public class AnnotatedClassCacheableService implements CacheableService {
return counter.getAndIncrement();
}
@CacheUpdate("default")
public Object update(Object arg1) {
return counter.getAndIncrement();
}
@CacheUpdate(value = "default", condition = "#arg.equals(3)")
public Object conditionalUpdate(Object arg) {
return arg;
}
public Object nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;

View File

@@ -38,6 +38,10 @@ public interface CacheableService<T> {
T nullValue(Object arg1);
T update(Object arg1);
T conditionalUpdate(Object arg2);
Number nullInvocations();
T rootVars(Object arg1);

View File

@@ -19,6 +19,7 @@ package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CacheUpdate;
import org.springframework.cache.annotation.Cacheable;
/**
@@ -64,6 +65,16 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@CacheUpdate("default")
public Long update(Object arg1) {
return counter.getAndIncrement();
}
@CacheUpdate(value = "default", condition = "#arg.equals(3)")
public Long conditionalUpdate(Object arg) {
return Long.valueOf(arg.toString());
}
@Cacheable("default")
public Long nullValue(Object arg1) {
nullInvocations.incrementAndGet();