15
15
*/
16
16
package org .springframework .data .redis .cache ;
17
17
18
+ import java .nio .charset .StandardCharsets ;
18
19
import java .time .Duration ;
19
20
import java .util .Optional ;
20
21
21
22
import org .springframework .cache .Cache ;
23
+ import org .springframework .cache .interceptor .SimpleKey ;
24
+ import org .springframework .core .convert .ConversionService ;
25
+ import org .springframework .core .convert .converter .ConverterRegistry ;
22
26
import org .springframework .data .redis .serializer .JdkSerializationRedisSerializer ;
23
27
import org .springframework .data .redis .serializer .RedisSerializationContext .SerializationPair ;
24
28
import org .springframework .data .redis .serializer .StringRedisSerializer ;
29
+ import org .springframework .format .support .DefaultFormattingConversionService ;
25
30
import org .springframework .util .Assert ;
26
31
27
32
/**
@@ -44,16 +49,20 @@ public class RedisCacheConfiguration {
44
49
private final SerializationPair <String > keySerializationPair ;
45
50
private final SerializationPair <Object > valueSerializationPair ;
46
51
52
+ private final ConversionService conversionService ;
53
+
47
54
@ SuppressWarnings ("unchecked" )
48
55
private RedisCacheConfiguration (Duration ttl , Boolean cacheNullValues , Boolean usePrefix , String keyPrefix ,
49
- SerializationPair <String > keySerializationPair , SerializationPair <?> valueSerializationPair ) {
56
+ SerializationPair <String > keySerializationPair , SerializationPair <?> valueSerializationPair ,
57
+ ConversionService conversionService ) {
50
58
51
59
this .ttl = ttl ;
52
60
this .cacheNullValues = cacheNullValues ;
53
61
this .usePrefix = usePrefix ;
54
62
this .keyPrefix = keyPrefix ;
55
63
this .keySerializationPair = keySerializationPair ;
56
64
this .valueSerializationPair = (SerializationPair <Object >) valueSerializationPair ;
65
+ this .conversionService = conversionService ;
57
66
}
58
67
59
68
/**
@@ -71,15 +80,22 @@ private RedisCacheConfiguration(Duration ttl, Boolean cacheNullValues, Boolean u
71
80
* <dd>StringRedisSerializer.class</dd>
72
81
* <dt>value serializer</dt>
73
82
* <dd>JdkSerializationRedisSerializer.class</dd>
83
+ * <dt>conversion service</dt>
84
+ * <dd>{@link DefaultFormattingConversionService} with {@link #registerDefaultConverters(ConverterRegistry) default}
85
+ * cache key converters</dd>
74
86
* </dl>
75
87
*
76
88
* @return new {@link RedisCacheConfiguration}.
77
89
*/
78
90
public static RedisCacheConfiguration defaultCacheConfig () {
79
91
92
+ DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService ();
93
+
94
+ registerDefaultConverters (conversionService );
95
+
80
96
return new RedisCacheConfiguration (Duration .ZERO , true , true , null ,
81
97
SerializationPair .fromSerializer (new StringRedisSerializer ()),
82
- SerializationPair .fromSerializer (new JdkSerializationRedisSerializer ()));
98
+ SerializationPair .fromSerializer (new JdkSerializationRedisSerializer ()), conversionService );
83
99
}
84
100
85
101
/**
@@ -93,7 +109,7 @@ public RedisCacheConfiguration entryTtl(Duration ttl) {
93
109
Assert .notNull (ttl , "TTL duration must not be null!" );
94
110
95
111
return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
96
- valueSerializationPair );
112
+ valueSerializationPair , conversionService );
97
113
}
98
114
99
115
/**
@@ -106,8 +122,8 @@ public RedisCacheConfiguration prefixKeysWith(String prefix) {
106
122
107
123
Assert .notNull (prefix , "Prefix must not be null!" );
108
124
109
- return new RedisCacheConfiguration (ttl , cacheNullValues , true , prefix , keySerializationPair ,
110
- valueSerializationPair );
125
+ return new RedisCacheConfiguration (ttl , cacheNullValues , true , prefix , keySerializationPair , valueSerializationPair ,
126
+ conversionService );
111
127
}
112
128
113
129
/**
@@ -119,7 +135,8 @@ public RedisCacheConfiguration prefixKeysWith(String prefix) {
119
135
* @return new {@link RedisCacheConfiguration}.
120
136
*/
121
137
public RedisCacheConfiguration disableCachingNullValues () {
122
- return new RedisCacheConfiguration (ttl , false , usePrefix , keyPrefix , keySerializationPair , valueSerializationPair );
138
+ return new RedisCacheConfiguration (ttl , false , usePrefix , keyPrefix , keySerializationPair , valueSerializationPair ,
139
+ conversionService );
123
140
}
124
141
125
142
/**
@@ -132,7 +149,21 @@ public RedisCacheConfiguration disableCachingNullValues() {
132
149
public RedisCacheConfiguration disableKeyPrefix () {
133
150
134
151
return new RedisCacheConfiguration (ttl , cacheNullValues , false , keyPrefix , keySerializationPair ,
135
- valueSerializationPair );
152
+ valueSerializationPair , conversionService );
153
+ }
154
+
155
+ /**
156
+ * Define the {@link ConversionService} used for cache key to {@link String} conversion.
157
+ *
158
+ * @param conversionService must not be {@literal null}.
159
+ * @return new {@link RedisCacheConfiguration}.
160
+ */
161
+ public RedisCacheConfiguration withConversionService (ConversionService conversionService ) {
162
+
163
+ Assert .notNull (conversionService , "ConversionService must not be null!" );
164
+
165
+ return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
166
+ valueSerializationPair , conversionService );
136
167
}
137
168
138
169
/**
@@ -146,7 +177,7 @@ public RedisCacheConfiguration serializeKeysWith(SerializationPair<String> keySe
146
177
Assert .notNull (keySerializationPair , "KeySerializationPair must not be null!" );
147
178
148
179
return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
149
- valueSerializationPair );
180
+ valueSerializationPair , conversionService );
150
181
}
151
182
152
183
/**
@@ -160,7 +191,7 @@ public RedisCacheConfiguration serializeValuesWith(SerializationPair<?> valueSer
160
191
Assert .notNull (valueSerializationPair , "ValueSerializationPair must not be null!" );
161
192
162
193
return new RedisCacheConfiguration (ttl , cacheNullValues , usePrefix , keyPrefix , keySerializationPair ,
163
- valueSerializationPair );
194
+ valueSerializationPair , conversionService );
164
195
}
165
196
166
197
/**
@@ -206,4 +237,26 @@ public Duration getTtl() {
206
237
return ttl ;
207
238
}
208
239
240
+ /**
241
+ * @return The {@link ConversionService} used for cache key to {@link String} conversion. Never {@literal null}.
242
+ */
243
+ public ConversionService getConversionService () {
244
+ return conversionService ;
245
+ }
246
+
247
+ /**
248
+ * Registers default cache key converters. The following converters get registered:
249
+ * <ul>
250
+ * <li>{@link String} to {@link byte byte[]} using UTF-8 encoding.</li>
251
+ * <li>{@link SimpleKey} to {@link String}</li>
252
+ *
253
+ * @param registry must not be {@literal null}.
254
+ */
255
+ public static void registerDefaultConverters (ConverterRegistry registry ) {
256
+
257
+ Assert .notNull (registry , "ConverterRegistry must not be null!" );
258
+
259
+ registry .addConverter (String .class , byte [].class , source -> source .getBytes (StandardCharsets .UTF_8 ));
260
+ registry .addConverter (SimpleKey .class , String .class , SimpleKey ::toString );
261
+ }
209
262
}
0 commit comments