17
17
18
18
import java .lang .reflect .Method ;
19
19
import java .nio .ByteBuffer ;
20
+ import java .time .Duration ;
20
21
import java .util .Arrays ;
21
22
import java .util .Collection ;
22
23
import java .util .Map ;
@@ -93,9 +94,9 @@ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfig
93
94
94
95
95
96
/**
96
- * Get {@link RedisCacheConfiguration} used.
97
+ * Gets {@link RedisCacheConfiguration} used to configure this cache .
97
98
*
98
- * @return immutable {@link RedisCacheConfiguration}. Never {@literal null}.
99
+ * @return immutable {@link RedisCacheConfiguration} used to configure this cache; never {@literal null}.
99
100
*/
100
101
public RedisCacheConfiguration getCacheConfiguration () {
101
102
return this .cacheConfiguration ;
@@ -130,6 +131,15 @@ public CacheStatistics getStatistics() {
130
131
return getCacheWriter ().getCacheStatistics (getName ());
131
132
}
132
133
134
+ /**
135
+ * Return the {@link TtlFunction} used to compute the per cache entry {@link Duration time to live expiration}.
136
+ *
137
+ * @return the {@link TtlFunction} used to compute the per cache entry {@link Duration time to live expiration}.
138
+ */
139
+ protected TtlFunction getTtlFunction () {
140
+ return this .ttlFunction ;
141
+ }
142
+
133
143
@ Override
134
144
@ SuppressWarnings ("unchecked" )
135
145
public <T > T get (Object key , Callable <T > valueLoader ) {
@@ -178,7 +188,7 @@ public void put(Object key, @Nullable Object value) {
178
188
179
189
Object cacheValue = preProcessCacheValue (value );
180
190
181
- if (! isAllowNullValues () && cacheValue == null ) {
191
+ if (isNonAllowedNullCacheValue ( cacheValue ) ) {
182
192
183
193
String message = String .format ("Cache '%s' does not allow 'null' values; Avoid storing null"
184
194
+ " via '@Cacheable(unless=\" #result == null\" )' or configure RedisCache to allow 'null'"
@@ -189,20 +199,20 @@ public void put(Object key, @Nullable Object value) {
189
199
}
190
200
191
201
getCacheWriter ().put (getName (), createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ),
192
- ttlFunction . getTimeToLive (key , value ));
202
+ getTtlFunction (). computeTimeToLive (key , value ));
193
203
}
194
204
195
205
@ Override
196
206
public ValueWrapper putIfAbsent (Object key , @ Nullable Object value ) {
197
207
198
208
Object cacheValue = preProcessCacheValue (value );
199
209
200
- if (! isAllowNullValues () && cacheValue == null ) {
210
+ if (isNonAllowedNullCacheValue ( cacheValue ) ) {
201
211
return get (key );
202
212
}
203
213
204
214
byte [] result = getCacheWriter ().putIfAbsent (getName (), createAndConvertCacheKey (key ),
205
- serializeCacheValue (cacheValue ), ttlFunction . getTimeToLive (key , value ));
215
+ serializeCacheValue (cacheValue ), getTtlFunction (). computeTimeToLive (key , value ));
206
216
207
217
return result != null ? new SimpleValueWrapper (fromStoreValue (deserializeCacheValue (result ))) : null ;
208
218
}
@@ -320,8 +330,8 @@ protected String createCacheKey(Object key) {
320
330
*/
321
331
protected String convertKey (Object key ) {
322
332
323
- if (key instanceof String ) {
324
- return ( String ) key ;
333
+ if (key instanceof String stringKey ) {
334
+ return stringKey ;
325
335
}
326
336
327
337
TypeDescriptor source = TypeDescriptor .forObject (key );
@@ -368,6 +378,10 @@ private boolean isCollectionLikeOrMap(TypeDescriptor source) {
368
378
return source .isArray () || source .isCollection () || source .isMap ();
369
379
}
370
380
381
+ private boolean isNonAllowedNullCacheValue (@ Nullable Object cacheValue ) {
382
+ return cacheValue == null && !isAllowNullValues ();
383
+ }
384
+
371
385
private String convertCollectionLikeOrMapKey (Object key , TypeDescriptor source ) {
372
386
373
387
if (source .isMap ()) {
0 commit comments