SPR-8015
+ update default key generator strategy to improve compatibility for implicit declaration on one arg method + updated docs
This commit is contained in:
@@ -20,7 +20,7 @@ import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Cache 'key' extractor. Used for creating a key based on the given method
|
||||
* (used as context) and its parameter.
|
||||
* (used as context) and its parameters.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
|
||||
@@ -21,14 +21,22 @@ import java.lang.reflect.Method;
|
||||
import org.springframework.cache.KeyGenerator;
|
||||
|
||||
/**
|
||||
* Default key generator. Computes a resulting key based on the hashcode of the
|
||||
* given parameters.
|
||||
* Default key generator. Returns 0 if no param is given, the param itself if only one is given or a hash code computed
|
||||
* from all given params hash code.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class DefaultKeyGenerator implements KeyGenerator<Object> {
|
||||
|
||||
public Object extract(Method method, Object... params) {
|
||||
if (params.length == 1) {
|
||||
return params[0];
|
||||
}
|
||||
|
||||
if (params.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hashCode = 17;
|
||||
|
||||
for (Object object : params) {
|
||||
|
||||
@@ -77,6 +77,21 @@ public abstract class AbstractAnnotationTest {
|
||||
assertSame(r3, r4);
|
||||
}
|
||||
|
||||
public void testInvalidateWKey(CacheableService service) throws Exception {
|
||||
Object o1 = new Object();
|
||||
Object o2 = new Object();
|
||||
|
||||
Object r1 = service.cache(o1);
|
||||
Object r2 = service.cache(o1);
|
||||
|
||||
assertSame(r1, r2);
|
||||
service.invalidate(o1, null);
|
||||
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);
|
||||
@@ -132,6 +147,11 @@ public abstract class AbstractAnnotationTest {
|
||||
testInvalidate(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidateWithKey() throws Exception {
|
||||
testInvalidateWKey(cs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConditionalExpression() throws Exception {
|
||||
testConditionalExpression(cs);
|
||||
@@ -152,6 +172,11 @@ public abstract class AbstractAnnotationTest {
|
||||
testInvalidate(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassCacheInvalidateWKey() throws Exception {
|
||||
testInvalidateWKey(ccs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValue() throws Exception {
|
||||
testNullValue(cs);
|
||||
|
||||
@@ -42,6 +42,10 @@ public class AnnotatedClassCacheableService implements CacheableService {
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void invalidate(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", key = "#p0")
|
||||
public Object key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
|
||||
@@ -28,6 +28,8 @@ public interface CacheableService<T> {
|
||||
|
||||
void invalidate(Object arg1);
|
||||
|
||||
void invalidate(Object arg1, Object arg2);
|
||||
|
||||
T conditional(int field);
|
||||
|
||||
T key(Object arg1, Object arg2);
|
||||
|
||||
@@ -40,6 +40,10 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
public void invalidate(Object arg1) {
|
||||
}
|
||||
|
||||
@CacheEvict(value = "default", key = "#p0")
|
||||
public void invalidate(Object arg1, Object arg2) {
|
||||
}
|
||||
|
||||
@Cacheable(value = "default", condition = "#classField == 3")
|
||||
public Long conditional(int classField) {
|
||||
return counter.getAndIncrement();
|
||||
|
||||
Reference in New Issue
Block a user