38
38
import org .springframework .data .redis .connection .Message ;
39
39
import org .springframework .data .redis .connection .RedisConnection ;
40
40
import org .springframework .data .redis .connection .RedisConnectionFactory ;
41
+ import org .springframework .data .redis .core .convert .CustomConversions ;
41
42
import org .springframework .data .redis .core .convert .IndexResolverImpl ;
42
43
import org .springframework .data .redis .core .convert .MappingRedisConverter ;
43
44
import org .springframework .data .redis .core .convert .RedisConverter ;
@@ -65,18 +66,44 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter implements App
65
66
private RedisMessageListenerContainer messageListenerContainer ;
66
67
private KeyExpirationEventMessageListener expirationListener ;
67
68
69
+ /**
70
+ * Creates new {@link RedisKeyValueAdapter} with default {@link RedisMappingContext} and default
71
+ * {@link CustomConversions}.
72
+ *
73
+ * @param redisOps must not be {@literal null}.
74
+ */
68
75
public RedisKeyValueAdapter (RedisOperations <?, ?> redisOps ) {
69
76
this (redisOps , new RedisMappingContext ());
70
77
}
71
78
79
+ /**
80
+ * Creates new {@link RedisKeyValueAdapter} with default {@link CustomConversions}.
81
+ *
82
+ * @param redisOps must not be {@literal null}.
83
+ * @param mappingContext must not be {@literal null}.
84
+ */
72
85
public RedisKeyValueAdapter (RedisOperations <?, ?> redisOps , RedisMappingContext mappingContext ) {
86
+ this (redisOps , mappingContext , new CustomConversions ());
87
+ }
88
+
89
+ /**
90
+ * Creates new {@link RedisKeyValueAdapter}.
91
+ *
92
+ * @param redisOps must not be {@literal null}.
93
+ * @param mappingContext must not be {@literal null}.
94
+ * @param customConversions can be {@literal null}.
95
+ */
96
+ public RedisKeyValueAdapter (RedisOperations <?, ?> redisOps , RedisMappingContext mappingContext ,
97
+ CustomConversions customConversions ) {
73
98
74
99
super (new RedisQueryEngine ());
75
100
76
101
Assert .notNull (redisOps , "RedisOperations must not be null!" );
102
+ Assert .notNull (mappingContext , "RedisMappingContext must not be null!" );
77
103
78
104
MappingRedisConverter mappingConverter = new MappingRedisConverter (mappingContext , new IndexResolverImpl (
79
105
mappingContext .getMappingConfiguration ().getIndexConfiguration ()), new ReferenceResolverImpl (this ));
106
+ mappingConverter .setCustomConversions (customConversions == null ? new CustomConversions () : customConversions );
80
107
mappingConverter .afterPropertiesSet ();
81
108
82
109
converter = mappingConverter ;
@@ -179,6 +206,16 @@ public Boolean doInRedis(RedisConnection connection) throws DataAccessException
179
206
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable)
180
207
*/
181
208
public Object get (Serializable id , Serializable keyspace ) {
209
+ return get (id , keyspace , Object .class );
210
+ }
211
+
212
+ /**
213
+ * @param id
214
+ * @param keyspace
215
+ * @param type
216
+ * @return
217
+ */
218
+ public <T > T get (Serializable id , Serializable keyspace , Class <T > type ) {
182
219
183
220
final byte [] binId = createKey (keyspace , id );
184
221
@@ -190,19 +227,31 @@ public Map<byte[], byte[]> doInRedis(RedisConnection connection) throws DataAcce
190
227
}
191
228
});
192
229
193
- return converter .read (Object .class , new RedisData (raw ));
230
+ RedisData data = new RedisData (raw );
231
+ data .setId (id );
232
+ data .setKeyspace (keyspace .toString ());
233
+
234
+ return converter .read (type , data );
194
235
}
195
236
196
237
/*
197
238
* (non-Javadoc)
198
239
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#delete(java.io.Serializable, java.io.Serializable)
199
240
*/
200
241
public Object delete (final Serializable id , final Serializable keyspace ) {
242
+ return delete (id , keyspace , Object .class );
243
+ }
244
+
245
+ /*
246
+ * (non-Javadoc)
247
+ * @see org.springframework.data.keyvalue.core.AbstractKeyValueAdapter#delete(java.io.Serializable, java.io.Serializable, java.lang.Class)
248
+ */
249
+ public <T > T delete (final Serializable id , final Serializable keyspace , final Class <T > type ) {
201
250
202
251
final byte [] binId = toBytes (id );
203
252
final byte [] binKeyspace = toBytes (keyspace );
204
253
205
- Object o = get (id , keyspace );
254
+ T o = get (id , keyspace , type );
206
255
207
256
if (o != null ) {
208
257
@@ -472,6 +521,11 @@ private boolean isKeyExpirationMessage(Message message) {
472
521
}
473
522
}
474
523
524
+ /**
525
+ * {@link ReferenceResolver} using {@link RedisKeyValueAdapter} to read and convert referenced entities.
526
+ *
527
+ * @author Christoph Strobl
528
+ */
475
529
static class ReferenceResolverImpl implements ReferenceResolver {
476
530
477
531
private RedisKeyValueAdapter adapter ;
@@ -487,6 +541,9 @@ public ReferenceResolverImpl(RedisKeyValueAdapter adapter) {
487
541
this .adapter = adapter ;
488
542
}
489
543
544
+ /**
545
+ * @param adapter
546
+ */
490
547
public void setAdapter (RedisKeyValueAdapter adapter ) {
491
548
this .adapter = adapter ;
492
549
}
@@ -497,7 +554,7 @@ public void setAdapter(RedisKeyValueAdapter adapter) {
497
554
*/
498
555
@ Override
499
556
public <T > T resolveReference (Serializable id , Serializable keyspace , Class <T > type ) {
500
- return (T ) adapter .get (id , keyspace );
557
+ return (T ) adapter .get (id , keyspace , type );
501
558
}
502
559
}
503
560
0 commit comments