+ align @CacheEvict behaviour with @Cacheable and @CachePut
+ add flag for post method execution + add integration tests
This commit is contained in:
@@ -16,13 +16,7 @@
|
||||
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
@@ -76,7 +70,7 @@ public abstract class AbstractAnnotationTests {
|
||||
assertSame(r1, r3);
|
||||
}
|
||||
|
||||
public void testInvalidate(CacheableService<?> service) throws Exception {
|
||||
public void testEvict(CacheableService<?> service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
@@ -90,7 +84,43 @@ public abstract class AbstractAnnotationTests {
|
||||
assertSame(r3, r4);
|
||||
}
|
||||
|
||||
public void testInvalidateWKey(CacheableService<?> service) throws Exception {
|
||||
public void testEvictEarly(CacheableService<?> service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
Object r2 = service.cache(o1);
|
||||
|
||||
assertSame(r1, r2);
|
||||
try {
|
||||
service.evictEarly(o1);
|
||||
} catch (RuntimeException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
Object r3 = service.cache(o1);
|
||||
Object r4 = service.cache(o1);
|
||||
assertNotSame(r1, r3);
|
||||
assertSame(r3, r4);
|
||||
}
|
||||
|
||||
public void testEvictException(CacheableService<?> service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
Object r2 = service.cache(o1);
|
||||
|
||||
assertSame(r1, r2);
|
||||
try {
|
||||
service.evictWithException(o1);
|
||||
} catch (RuntimeException ex) {
|
||||
// expected
|
||||
}
|
||||
// exception occurred, eviction skipped, data should still be in the cache
|
||||
Object r3 = service.cache(o1);
|
||||
assertSame(r1, r3);
|
||||
}
|
||||
|
||||
public void testEvictWKey(CacheableService<?> service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
@@ -104,8 +134,48 @@ public abstract class AbstractAnnotationTests {
|
||||
assertSame(r3, r4);
|
||||
}
|
||||
|
||||
public void testConditionalExpression(CacheableService<?> service)
|
||||
throws Exception {
|
||||
public void testEvictWKeyEarly(CacheableService<?> service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
Object r2 = service.cache(o1);
|
||||
|
||||
assertSame(r1, r2);
|
||||
|
||||
try {
|
||||
service.invalidateEarly(o1, null);
|
||||
} catch (Exception ex) {
|
||||
// expected
|
||||
}
|
||||
Object r3 = service.cache(o1);
|
||||
Object r4 = service.cache(o1);
|
||||
assertNotSame(r1, r3);
|
||||
assertSame(r3, r4);
|
||||
}
|
||||
|
||||
public void testEvictAll(CacheableService<?> service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
Object r2 = service.cache(o1);
|
||||
|
||||
Object o2 = new Object();
|
||||
Object r10 = service.cache(o2);
|
||||
|
||||
assertSame(r1, r2);
|
||||
assertNotSame(r1, r10);
|
||||
service.evictAll(new Object());
|
||||
Cache cache = cm.getCache("default");
|
||||
assertNull(cache.get(o1));
|
||||
assertNull(cache.get(o2));
|
||||
|
||||
Object r3 = service.cache(o1);
|
||||
Object r4 = service.cache(o1);
|
||||
assertNotSame(r1, r3);
|
||||
assertSame(r3, r4);
|
||||
}
|
||||
|
||||
public void testConditionalExpression(CacheableService<?> service) throws Exception {
|
||||
Object r1 = service.conditional(4);
|
||||
Object r2 = service.conditional(4);
|
||||
|
||||
@@ -139,8 +209,7 @@ public abstract class AbstractAnnotationTests {
|
||||
assertEquals(nr + 1, service.nullInvocations().intValue());
|
||||
}
|
||||
|
||||
public void testMethodName(CacheableService<?> service, String keyName)
|
||||
throws Exception {
|
||||
public void testMethodName(CacheableService<?> service, String keyName) throws Exception {
|
||||
Object key = new Object();
|
||||
Object r1 = service.name(key);
|
||||
assertSame(r1, service.name(key));
|
||||
@@ -335,12 +404,32 @@ public abstract class AbstractAnnotationTests {
|
||||
|
||||
@Test
|
||||
public void testInvalidate() throws Exception {
|
||||
testInvalidate(cs);
|
||||
testEvict(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEarlyInvalidate() throws Exception {
|
||||
testEvictEarly(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvictWithException() throws Exception {
|
||||
testEvictException(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvictAll() throws Exception {
|
||||
testEvictAll(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidateWithKey() throws Exception {
|
||||
testInvalidateWKey(cs);
|
||||
testEvictWKey(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEarlyInvalidateWithKey() throws Exception {
|
||||
testEvictWKeyEarly(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -360,12 +449,32 @@ public abstract class AbstractAnnotationTests {
|
||||
|
||||
@Test
|
||||
public void testClassCacheInvalidate() throws Exception {
|
||||
testInvalidate(ccs);
|
||||
testEvict(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassEarlyInvalidate() throws Exception {
|
||||
testEvictEarly(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassEvictAll() throws Exception {
|
||||
testEvictAll(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassEvictWithException() throws Exception {
|
||||
testEvictException(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassCacheInvalidateWKey() throws Exception {
|
||||
testInvalidateWKey(ccs);
|
||||
testEvictWKey(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassEarlyInvalidateWithKey() throws Exception {
|
||||
testEvictWKeyEarly(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -383,8 +492,7 @@ public abstract class AbstractAnnotationTests {
|
||||
assertNull(ccs.nullValue(new Object()));
|
||||
// the check method is also cached
|
||||
assertEquals(nr, ccs.nullInvocations().intValue());
|
||||
assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations
|
||||
.intValue());
|
||||
assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -18,10 +18,10 @@ package org.springframework.cache.config;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
@@ -44,10 +44,29 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void evictWithException(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should NOT occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", allEntries = true)
|
||||
public void evictAll(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", afterInvocation = false)
|
||||
public void evictEarly(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void evict(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0", afterInvocation = false)
|
||||
public void invalidateEarly(Object arg1, Object arg2) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#p0")
|
||||
public Object key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
|
||||
@@ -27,8 +27,16 @@ public interface CacheableService<T> {
|
||||
|
||||
void invalidate(Object arg1);
|
||||
|
||||
void evictEarly(Object arg1);
|
||||
|
||||
void evictAll(Object arg1);
|
||||
|
||||
void evictWithException(Object arg1);
|
||||
|
||||
void evict(Object arg1, Object arg2);
|
||||
|
||||
void invalidateEarly(Object arg1, Object arg2);
|
||||
|
||||
T conditional(int field);
|
||||
|
||||
T key(Object arg1, Object arg2);
|
||||
|
||||
@@ -18,10 +18,10 @@ package org.springframework.cache.config;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* Simple cacheable service
|
||||
@@ -42,10 +42,29 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict("default")
|
||||
public void evictWithException(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should NOT occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", allEntries = true)
|
||||
public void evictAll(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", afterInvocation = false)
|
||||
public void evictEarly(Object arg1) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void evict(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0", afterInvocation = false)
|
||||
public void invalidateEarly(Object arg1, Object arg2) {
|
||||
throw new RuntimeException("exception thrown - evict should still occur");
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", condition = "#classField == 3")
|
||||
public Long conditional(int classField) {
|
||||
return counter.getAndIncrement();
|
||||
|
||||
Reference in New Issue
Block a user