Allow null values to be cached with @CacheResult

Even though the JSR-107 spec forbids to store null values, our cache
abstraction allows that behaviour with a special handled (and this is
the default behaviour).

While this was working fine with our own set of annotations, the
JSR-107 interceptor counterpart was interpreting the spec sensu strictu.

We now allow for that special case as well.

Issue: SPR-13641
This commit is contained in:
Stephane Nicoll
2015-11-09 13:03:40 +01:00
parent 59b6600763
commit 2a2a8d3f93
13 changed files with 86 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,9 +59,7 @@ class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheResultOper
try {
Object invocationResult = invoker.invoke();
if (invocationResult != null) {
cache.put(cacheKey, invocationResult);
}
cache.put(cacheKey, invocationResult);
return invocationResult;
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -67,6 +67,23 @@ public abstract class AbstractJCacheAnnotationTests {
assertSame(first, second);
}
@Test
public void cacheNull() {
Cache cache = getCache(DEFAULT_CACHE);
String keyItem = name.getMethodName();
assertNull(cache.get(keyItem));
Object first = service.cacheNull(keyItem);
Object second = service.cacheNull(keyItem);
assertSame(first, second);
Cache.ValueWrapper wrapper = cache.get(keyItem);
assertNotNull(wrapper);
assertSame(first, wrapper.get());
assertNull("Cached value should be null", wrapper.get());
}
@Test
public void cacheException() {
String keyItem = name.getMethodName();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@ public interface JCacheableService<T> {
T cache(String id);
T cacheNull(String id);
T cacheWithException(String id, boolean matchFilter);
T cacheWithCheckedException(String id, boolean matchFilter) throws IOException;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,6 +57,12 @@ public class AnnotatedJCacheableService implements JCacheableService<Long> {
return counter.getAndIncrement();
}
@Override
@CacheResult
public Long cacheNull(String id) {
return null;
}
@Override
@CacheResult(exceptionCacheName = "exception", nonCachedExceptions = NullPointerException.class)
public Long cacheWithException(@CacheKey String id, boolean matchFilter) {