@@ -36,6 +36,8 @@ public class RedisCaffeineCache extends AbstractValueAdaptingCache {
3636
3737 private final Duration defaultExpiration ;
3838
39+ private final Duration defaultNullValuesExpiration ;
40+
3941 private final Map <String , Duration > expires ;
4042
4143 private final String topic ;
@@ -50,6 +52,7 @@ public RedisCaffeineCache(String name, RedisTemplate<Object, Object> stringKeyRe
5052 this .caffeineCache = caffeineCache ;
5153 this .cachePrefix = cacheConfigProperties .getCachePrefix ();
5254 this .defaultExpiration = cacheConfigProperties .getRedis ().getDefaultExpiration ();
55+ this .defaultNullValuesExpiration = cacheConfigProperties .getRedis ().getDefaultNullValuesExpiration ();
5356 this .expires = cacheConfigProperties .getRedis ().getExpires ();
5457 this .topic = cacheConfigProperties .getRedis ().getTopic ();
5558 }
@@ -115,9 +118,9 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
115118 }
116119
117120 private void doPut (Object key , Object value ) {
118- Duration expire = getExpire ();
121+ Duration expire = getExpire (value );
119122 value = toStoreValue (value );
120- if (!expire .isNegative ()) {
123+ if (!expire .isNegative () && ! expire . isZero () ) {
121124 stringKeyRedisTemplate .opsForValue ().set (getKey (key ), value , expire );
122125 }
123126 else {
@@ -178,9 +181,15 @@ private Object getKey(Object key) {
178181 StringUtils .isEmpty (cachePrefix ) ? key .toString () : cachePrefix .concat (":" ).concat (key .toString ()));
179182 }
180183
181- private Duration getExpire () {
184+ private Duration getExpire (Object value ) {
182185 Duration cacheNameExpire = expires .get (this .name );
183- return cacheNameExpire == null ? defaultExpiration : cacheNameExpire ;
186+ if (cacheNameExpire == null ) {
187+ cacheNameExpire = defaultExpiration ;
188+ }
189+ if (value == null && this .defaultNullValuesExpiration != null ) {
190+ cacheNameExpire = this .defaultNullValuesExpiration ;
191+ }
192+ return cacheNameExpire ;
184193 }
185194
186195 /**
0 commit comments