Skip to content

Commit 6c138d3

Browse files
committed
Reliable null value handling in ConcurrentMapCache, GuavaCache, JCacheCache
Issue: SPR-13553 (cherry picked from commit de93290)
1 parent bb8a12f commit 6c138d3

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,6 +126,10 @@ protected Object toStoreValue(Object userValue) {
126126

127127
@SuppressWarnings("serial")
128128
private static class NullHolder implements Serializable {
129+
130+
private Object readResolve() {
131+
return NULL_HOLDER;
132+
}
129133
}
130134

131135
}

spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,17 @@
1616

1717
package org.springframework.cache.concurrent;
1818

19-
import org.springframework.cache.Cache;
20-
import org.springframework.cache.support.SimpleValueWrapper;
21-
2219
import java.io.Serializable;
2320
import java.util.concurrent.ConcurrentHashMap;
2421
import java.util.concurrent.ConcurrentMap;
2522

23+
import org.springframework.cache.Cache;
24+
import org.springframework.cache.support.SimpleValueWrapper;
25+
import org.springframework.util.Assert;
26+
2627
/**
27-
* Simple {@link Cache} implementation based on the core JDK
28-
* {@code java.util.concurrent} package.
28+
* Simple {@link org.springframework.cache.Cache} implementation based on the
29+
* core JDK {@code java.util.concurrent} package.
2930
*
3031
* <p>Useful for testing or simple caching scenarios, typically in combination
3132
* with {@link org.springframework.cache.support.SimpleCacheManager} or
@@ -62,21 +63,24 @@ public ConcurrentMapCache(String name) {
6263
/**
6364
* Create a new ConcurrentMapCache with the specified name.
6465
* @param name the name of the cache
65-
* @param allowNullValues whether to accept and convert null values for this cache
66+
* @param allowNullValues whether to accept and convert {@code null}
67+
* values for this cache
6668
*/
6769
public ConcurrentMapCache(String name, boolean allowNullValues) {
6870
this(name, new ConcurrentHashMap<Object, Object>(256), allowNullValues);
6971
}
7072

7173
/**
7274
* Create a new ConcurrentMapCache with the specified name and the
73-
* given internal ConcurrentMap to use.
75+
* given internal {@link ConcurrentMap} to use.
7476
* @param name the name of the cache
7577
* @param store the ConcurrentMap to use as an internal store
7678
* @param allowNullValues whether to allow {@code null} values
7779
* (adapting them to an internal null holder value)
7880
*/
7981
public ConcurrentMapCache(String name, ConcurrentMap<Object, Object> store, boolean allowNullValues) {
82+
Assert.notNull(name, "Name must not be null");
83+
Assert.notNull(store, "Store must not be null");
8084
this.name = name;
8185
this.store = store;
8286
this.allowNullValues = allowNullValues;
@@ -142,6 +146,10 @@ protected Object toStoreValue(Object userValue) {
142146

143147
@SuppressWarnings("serial")
144148
private static class NullHolder implements Serializable {
149+
150+
private Object readResolve() {
151+
return NULL_HOLDER;
152+
}
145153
}
146154

147155
}

0 commit comments

Comments
 (0)