16
16
17
17
package org .springframework .cache .guava ;
18
18
19
- import java .io .Serializable ;
20
19
import java .util .concurrent .Callable ;
21
20
import java .util .concurrent .ExecutionException ;
22
21
23
22
import com .google .common .cache .LoadingCache ;
24
23
import com .google .common .util .concurrent .UncheckedExecutionException ;
25
24
26
- import org .springframework .cache .Cache ;
27
- import org .springframework .cache .support .SimpleValueWrapper ;
25
+ import org .springframework .cache .support .AbstractValueAdaptingCache ;
28
26
import org .springframework .util .Assert ;
29
27
30
28
/**
31
- * Spring {@link Cache} adapter implementation on top of a
32
- * Guava {@link com.google.common.cache.Cache} instance.
29
+ * Spring {@link org.springframework.cache. Cache} adapter implementation
30
+ * on top of a Guava {@link com.google.common.cache.Cache} instance.
33
31
*
34
32
* <p>Requires Google Guava 12.0 or higher.
35
33
*
36
34
* @author Juergen Hoeller
37
35
* @author Stephane Nicoll
38
36
* @since 4.0
39
37
*/
40
- public class GuavaCache implements Cache {
41
-
42
- private static final Object NULL_HOLDER = new NullHolder ();
38
+ public class GuavaCache extends AbstractValueAdaptingCache {
43
39
44
40
private final String name ;
45
41
46
42
private final com .google .common .cache .Cache <Object , Object > cache ;
47
43
48
- private final boolean allowNullValues ;
49
-
50
44
51
45
/**
52
46
* Create a {@link GuavaCache} instance with the specified name and the
@@ -67,11 +61,11 @@ public GuavaCache(String name, com.google.common.cache.Cache<Object, Object> cac
67
61
* values for this cache
68
62
*/
69
63
public GuavaCache (String name , com .google .common .cache .Cache <Object , Object > cache , boolean allowNullValues ) {
64
+ super (allowNullValues );
70
65
Assert .notNull (name , "Name must not be null" );
71
66
Assert .notNull (cache , "Cache must not be null" );
72
67
this .name = name ;
73
68
this .cache = cache ;
74
- this .allowNullValues = allowNullValues ;
75
69
}
76
70
77
71
@@ -85,32 +79,23 @@ public final com.google.common.cache.Cache<Object, Object> getNativeCache() {
85
79
return this .cache ;
86
80
}
87
81
88
- public final boolean isAllowNullValues () {
89
- return this .allowNullValues ;
90
- }
91
-
92
82
@ Override
93
83
public ValueWrapper get (Object key ) {
94
84
if (this .cache instanceof LoadingCache ) {
95
85
try {
96
86
Object value = ((LoadingCache <Object , Object >) this .cache ).get (key );
97
- return toWrapper (value );
87
+ return toValueWrapper (value );
98
88
}
99
89
catch (ExecutionException ex ) {
100
90
throw new UncheckedExecutionException (ex .getMessage (), ex );
101
91
}
102
92
}
103
- return toWrapper ( this . cache . getIfPresent (key ) );
93
+ return super . get (key );
104
94
}
105
95
106
96
@ Override
107
- @ SuppressWarnings ("unchecked" )
108
- public <T > T get (Object key , Class <T > type ) {
109
- Object value = fromStoreValue (this .cache .getIfPresent (key ));
110
- if (value != null && type != null && !type .isInstance (value )) {
111
- throw new IllegalStateException ("Cached value is not of required type [" + type .getName () + "]: " + value );
112
- }
113
- return (T ) value ;
97
+ protected Object lookup (Object key ) {
98
+ return this .cache .getIfPresent (key );
114
99
}
115
100
116
101
@ Override
@@ -123,7 +108,7 @@ public ValueWrapper putIfAbsent(Object key, final Object value) {
123
108
try {
124
109
PutIfAbsentCallable callable = new PutIfAbsentCallable (value );
125
110
Object result = this .cache .get (key , callable );
126
- return (callable .called ? null : toWrapper (result ));
111
+ return (callable .called ? null : toValueWrapper (result ));
127
112
}
128
113
catch (ExecutionException ex ) {
129
114
throw new IllegalStateException (ex );
@@ -141,42 +126,6 @@ public void clear() {
141
126
}
142
127
143
128
144
- /**
145
- * Convert the given value from the internal store to a user value
146
- * returned from the get method (adapting {@code null}).
147
- * @param storeValue the store value
148
- * @return the value to return to the user
149
- */
150
- protected Object fromStoreValue (Object storeValue ) {
151
- if (this .allowNullValues && storeValue == NULL_HOLDER ) {
152
- return null ;
153
- }
154
- return storeValue ;
155
- }
156
-
157
- /**
158
- * Convert the given user value, as passed into the put method,
159
- * to a value in the internal store (adapting {@code null}).
160
- * @param userValue the given user value
161
- * @return the value to store
162
- */
163
- protected Object toStoreValue (Object userValue ) {
164
- if (this .allowNullValues && userValue == null ) {
165
- return NULL_HOLDER ;
166
- }
167
- return userValue ;
168
- }
169
-
170
- private ValueWrapper toWrapper (Object value ) {
171
- return (value != null ? new SimpleValueWrapper (fromStoreValue (value )) : null );
172
- }
173
-
174
-
175
- @ SuppressWarnings ("serial" )
176
- private static class NullHolder implements Serializable {
177
- }
178
-
179
-
180
129
private class PutIfAbsentCallable implements Callable <Object > {
181
130
182
131
private final Object value ;
0 commit comments