17
17
18
18
import java .nio .charset .StandardCharsets ;
19
19
import java .time .Duration ;
20
+ import java .util .function .BiFunction ;
20
21
import java .util .function .Consumer ;
21
22
22
23
import org .springframework .cache .Cache ;
40
41
* @author Christoph Strobl
41
42
* @author Mark Paluch
42
43
* @author John Blum
44
+ * @author Koy Zhuang
43
45
* @since 2.0
44
46
*/
45
47
public class RedisCacheConfiguration {
@@ -106,7 +108,7 @@ public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader c
106
108
107
109
registerDefaultConverters (conversionService );
108
110
109
- return new RedisCacheConfiguration (Duration .ZERO , DEFAULT_CACHE_NULL_VALUES , DEFAULT_USE_PREFIX ,
111
+ return new RedisCacheConfiguration (( k , v ) -> Duration .ZERO , DEFAULT_CACHE_NULL_VALUES , DEFAULT_USE_PREFIX ,
110
112
CacheKeyPrefix .simple (),
111
113
SerializationPair .fromSerializer (RedisSerializer .string ()),
112
114
SerializationPair .fromSerializer (RedisSerializer .java (classLoader )), conversionService );
@@ -119,17 +121,17 @@ public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader c
119
121
120
122
private final ConversionService conversionService ;
121
123
122
- private final Duration ttl ;
124
+ private final BiFunction < Object , Object , Duration > ttlProvider ;
123
125
124
126
private final SerializationPair <String > keySerializationPair ;
125
127
private final SerializationPair <Object > valueSerializationPair ;
126
128
127
129
@ SuppressWarnings ("unchecked" )
128
- private RedisCacheConfiguration (Duration ttl , Boolean cacheNullValues , Boolean usePrefix , CacheKeyPrefix keyPrefix ,
130
+ private RedisCacheConfiguration (BiFunction < Object , Object , Duration > ttlProvider , Boolean cacheNullValues , Boolean usePrefix , CacheKeyPrefix keyPrefix ,
129
131
SerializationPair <String > keySerializationPair , SerializationPair <?> valueSerializationPair ,
130
132
ConversionService conversionService ) {
131
133
132
- this .ttl = ttl ;
134
+ this .ttlProvider = ttlProvider ;
133
135
this .cacheNullValues = cacheNullValues ;
134
136
this .usePrefix = usePrefix ;
135
137
this .keyPrefix = keyPrefix ;
@@ -165,7 +167,7 @@ public RedisCacheConfiguration computePrefixWith(CacheKeyPrefix cacheKeyPrefix)
165
167
166
168
Assert .notNull (cacheKeyPrefix , "Function for computing prefix must not be null" );
167
169
168
- return new RedisCacheConfiguration (ttl , cacheNullValues , DEFAULT_USE_PREFIX , cacheKeyPrefix ,
170
+ return new RedisCacheConfiguration (ttlProvider , cacheNullValues , DEFAULT_USE_PREFIX , cacheKeyPrefix ,
169
171
keySerializationPair , valueSerializationPair , conversionService );
170
172
}
171
173
@@ -178,7 +180,7 @@ public RedisCacheConfiguration computePrefixWith(CacheKeyPrefix cacheKeyPrefix)
178
180
* @return new {@link RedisCacheConfiguration}.
179
181
*/
180
182
public RedisCacheConfiguration disableCachingNullValues () {
181
- return new RedisCacheConfiguration (ttl , DO_NOT_CACHE_NULL_VALUES , usePrefix , keyPrefix , keySerializationPair ,
183
+ return new RedisCacheConfiguration (ttlProvider , DO_NOT_CACHE_NULL_VALUES , usePrefix , keyPrefix , keySerializationPair ,
182
184
valueSerializationPair , conversionService );
183
185
}
184
186
@@ -191,12 +193,12 @@ public RedisCacheConfiguration disableCachingNullValues() {
191
193
*/
192
194
public RedisCacheConfiguration disableKeyPrefix () {
193
195
194
- return new RedisCacheConfiguration (ttl , cacheNullValues , DO_NOT_USE_PREFIX , keyPrefix , keySerializationPair ,
196
+ return new RedisCacheConfiguration (ttlProvider , cacheNullValues , DO_NOT_USE_PREFIX , keyPrefix , keySerializationPair ,
195
197
valueSerializationPair , conversionService );
196
198
}
197
199
198
200
/**
199
- * Set the ttl to apply for cache entries. Use {@link Duration#ZERO} to declare an eternal cache.
201
+ * Set the constant ttl to apply for cache entries. Use {@link Duration#ZERO} to declare an eternal cache.
200
202
*
201
203
* @param ttl must not be {@literal null}.
202
204
* @return new {@link RedisCacheConfiguration}.
@@ -205,10 +207,25 @@ public RedisCacheConfiguration entryTtl(Duration ttl) {
205
207
206
208
Assert .notNull (ttl , "TTL duration must not be null" );
207
209
208
- return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
210
+ return new RedisCacheConfiguration (( k , v ) -> ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
209
211
valueSerializationPair , conversionService );
210
212
}
211
213
214
+ /**
215
+ * Set the ttl Provider, which can dynamic provide ttl to apply for cache entries.
216
+ * @param ttlProvider {@link BiFunction} calculate ttl with the actual original cache key and value,
217
+ * which must not be {@literal null}, and the ttl must not be {@literal null} either.
218
+ *
219
+ * @return new {@link RedisCacheConfiguration}.
220
+ */
221
+ public RedisCacheConfiguration entryTtlProvider (BiFunction <Object , Object , Duration > ttlProvider ) {
222
+
223
+ Assert .notNull (ttlProvider , "ttlProvider must not be null" );
224
+
225
+ return new RedisCacheConfiguration (ttlProvider , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
226
+ valueSerializationPair , conversionService );
227
+ }
228
+
212
229
/**
213
230
* Define the {@link SerializationPair} used for de-/serializing cache keys.
214
231
*
@@ -219,7 +236,7 @@ public RedisCacheConfiguration serializeKeysWith(SerializationPair<String> keySe
219
236
220
237
Assert .notNull (keySerializationPair , "KeySerializationPair must not be null" );
221
238
222
- return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
239
+ return new RedisCacheConfiguration (ttlProvider , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
223
240
valueSerializationPair , conversionService );
224
241
}
225
242
@@ -233,7 +250,7 @@ public RedisCacheConfiguration serializeValuesWith(SerializationPair<?> valueSer
233
250
234
251
Assert .notNull (valueSerializationPair , "ValueSerializationPair must not be null" );
235
252
236
- return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
253
+ return new RedisCacheConfiguration (ttlProvider , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
237
254
valueSerializationPair , conversionService );
238
255
}
239
256
@@ -247,7 +264,7 @@ public RedisCacheConfiguration withConversionService(ConversionService conversio
247
264
248
265
Assert .notNull (conversionService , "ConversionService must not be null" );
249
266
250
- return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
267
+ return new RedisCacheConfiguration (ttlProvider , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
251
268
valueSerializationPair , conversionService );
252
269
}
253
270
@@ -301,9 +318,21 @@ public SerializationPair<Object> getValueSerializationPair() {
301
318
}
302
319
303
320
/**
304
- * @return The expiration time (ttl) for cache entries. Never {@literal null}.
321
+ * @return The constant expiration time (ttl) for cache entries. Never {@literal null}.
305
322
*/
306
323
public Duration getTtl () {
324
+ Duration ttl = ttlProvider .apply (Object .class , Object .class );
325
+ Assert .notNull (ttl , "TTL duration must not be null" );
326
+ return ttl ;
327
+ }
328
+
329
+ /**
330
+ * @return The expiration time (ttl) for cache entries with original key and value. Never {@literal null}.
331
+ */
332
+ public Duration getTtl (Object key , Object val ) {
333
+ Duration ttl = ttlProvider .apply (key , val );
334
+ Assert .notNull (ttl , "TTL duration must not be null" );
335
+
307
336
return ttl ;
308
337
}
309
338
0 commit comments