26
26
import org .springframework .core .convert .support .ConfigurableConversionService ;
27
27
import org .springframework .data .redis .serializer .JdkSerializationRedisSerializer ;
28
28
import org .springframework .format .support .DefaultFormattingConversionService ;
29
+ import org .springframework .lang .Nullable ;
29
30
import org .springframework .util .Assert ;
30
31
import org .springframework .util .ObjectUtils ;
31
32
import org .springframework .util .ReflectionUtils ;
32
33
33
34
/**
34
- * {@link org.springframework.cache.Cache} implementation suitable for Redis.
35
+ * {@link org.springframework.cache.Cache} implementation using for Redis as underlying store .
35
36
*
36
37
* @author Christoph Strobl
37
38
* @since 2.0
@@ -74,7 +75,7 @@ public NRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfigur
74
75
this .cacheWriter = cacheWriter ;
75
76
this .cacheConfig = cacheConfig ;
76
77
77
- conversionService .addConverter (String .class , byte [].class , source -> source .getBytes (Charset .forName ("UTF8 " )));
78
+ conversionService .addConverter (String .class , byte [].class , source -> source .getBytes (Charset .forName ("UTF-8 " )));
78
79
}
79
80
80
81
@ Override
@@ -125,7 +126,7 @@ public void put(Object key, Object value) {
125
126
name ));
126
127
}
127
128
128
- cacheWriter .put (name , createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ), cacheConfig .getTimeout ());
129
+ cacheWriter .put (name , createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ), cacheConfig .getTtl ());
129
130
}
130
131
131
132
@ Override
@@ -138,7 +139,7 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
138
139
}
139
140
140
141
byte [] result = cacheWriter .putIfAbsent (name , createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ),
141
- cacheConfig .getTimeout ());
142
+ cacheConfig .getTtl ());
142
143
143
144
if (result == null ) {
144
145
return null ;
@@ -159,7 +160,24 @@ public void clear() {
159
160
cacheWriter .clean (name , pattern );
160
161
}
161
162
162
- protected Object preProcessCacheValue (Object value ) {
163
+ /**
164
+ * Get {@link RedisCacheConfiguration} used.
165
+ *
166
+ * @return immutable {@link RedisCacheConfiguration}. Never {@literal null}.
167
+ */
168
+ public RedisCacheConfiguration getCacheConfiguration () {
169
+ return cacheConfig ;
170
+ }
171
+
172
+ /**
173
+ * Customization hook called before passing object to
174
+ * {@link org.springframework.data.redis.serializer.RedisSerializer}.
175
+ *
176
+ * @param value can be {@literal null}.
177
+ * @return preprocessed value. Can be {@literal null}.
178
+ */
179
+ @ Nullable
180
+ protected Object preProcessCacheValue (@ Nullable Object value ) {
163
181
164
182
if (value != null ) {
165
183
return value ;
@@ -168,10 +186,22 @@ protected Object preProcessCacheValue(Object value) {
168
186
return isAllowNullValues () ? NULL_VALUE : null ;
169
187
}
170
188
189
+ /**
190
+ * Serialize the key.
191
+ *
192
+ * @param cacheKey must not be {@literal null}.
193
+ * @return never {@literal null}.
194
+ */
171
195
protected byte [] serializeCacheKey (String cacheKey ) {
172
196
return cacheConfig .getKeySerializationPair ().write (cacheKey ).array ();
173
197
}
174
198
199
+ /**
200
+ * Serialize the value to cache.
201
+ *
202
+ * @param value must not be {@literal null}.
203
+ * @return never {@literal null}.
204
+ */
175
205
protected byte [] serializeCacheValue (Object value ) {
176
206
177
207
if (isAllowNullValues () && value instanceof NullValue ) {
@@ -181,6 +211,13 @@ protected byte[] serializeCacheValue(Object value) {
181
211
return cacheConfig .getValueSerializationPair ().write (value ).array ();
182
212
}
183
213
214
+ /**
215
+ * Deserialize the given value to the actual cache value.
216
+ *
217
+ * @param value must not be {@literal null}.
218
+ * @return can be {@literal null}.
219
+ */
220
+ @ Nullable
184
221
protected Object deserializeCacheValue (byte [] value ) {
185
222
186
223
if (isAllowNullValues () && ObjectUtils .nullSafeEquals (value , BINARY_NULL_VALUE )) {
@@ -205,7 +242,7 @@ private String createCacheKey(Object key) {
205
242
}
206
243
207
244
private String prefixCacheKey (String key ) {
208
- return cacheConfig .getDefaultPrefix ().orElseGet (() -> name + "::" ) + key ;
245
+ return cacheConfig .getKeyPrefix ().orElseGet (() -> name + "::" ) + key ;
209
246
}
210
247
211
248
private <T > T valueFromLoader (Object key , Callable <T > valueLoader ) {
0 commit comments