@@ -101,18 +101,17 @@ public ValueWrapper get(Object key) {
101
101
public RedisCacheElement get (final RedisCacheKey cacheKey ) {
102
102
103
103
notNull (cacheKey , "CacheKey must not be null!" );
104
- return (RedisCacheElement ) redisOperations .execute (new AbstractRedisCacheCallback <RedisCacheElement >(
105
- new RedisCacheElement (cacheKey , null ), cacheMetadata ) {
106
104
107
- @ Override
108
- public RedisCacheElement doInRedis (RedisCacheElement element , RedisConnection connection )
109
- throws DataAccessException {
105
+ byte [] bytes = (byte []) redisOperations .execute (new AbstractRedisCacheCallback <byte []>(new BinaryRedisCacheElement (
106
+ new RedisCacheElement (cacheKey , null ), cacheValueAccessor ), cacheMetadata ) {
110
107
111
- byte [] bs = connection . get ( element . getKeyBytes ());
112
- Object value = redisOperations . getValueSerializer () != null ? redisOperations . getValueSerializer (). deserialize ( bs ) : bs ;
113
- return ( bs == null ? null : new RedisCacheElement ( element .getKey (), value ));
108
+ @ Override
109
+ public byte [] doInRedis ( BinaryRedisCacheElement element , RedisConnection connection ) throws DataAccessException {
110
+ return connection . get ( element .getKeyBytes ( ));
114
111
}
115
112
});
113
+
114
+ return (bytes == null ? null : new RedisCacheElement (cacheKey , cacheValueAccessor .deserializeIfNecessary (bytes )));
116
115
}
117
116
118
117
/*
@@ -137,7 +136,9 @@ public void put(final Object key, final Object value) {
137
136
public void put (RedisCacheElement element ) {
138
137
139
138
notNull (element , "Element must not be null!" );
140
- redisOperations .execute (new RedisCachePutCallback (element , cacheValueAccessor , cacheMetadata ));
139
+
140
+ redisOperations .execute (new RedisCachePutCallback (new BinaryRedisCacheElement (element , cacheValueAccessor ),
141
+ cacheMetadata ));
141
142
}
142
143
143
144
/*
@@ -147,7 +148,8 @@ public void put(RedisCacheElement element) {
147
148
public ValueWrapper putIfAbsent (Object key , final Object value ) {
148
149
149
150
return putIfAbsent (new RedisCacheElement (new RedisCacheKey (key ).usePrefix (cacheMetadata .getKeyPrefix ())
150
- .withKeySerializer (redisOperations .getKeySerializer ()), value ).expireAfter (cacheMetadata .getDefaultExpiration ()));
151
+ .withKeySerializer (redisOperations .getKeySerializer ()), value )
152
+ .expireAfter (cacheMetadata .getDefaultExpiration ()));
151
153
}
152
154
153
155
/**
@@ -161,7 +163,12 @@ public ValueWrapper putIfAbsent(Object key, final Object value) {
161
163
public ValueWrapper putIfAbsent (RedisCacheElement element ) {
162
164
163
165
notNull (element , "Element must not be null!" );
164
- return toWrapper (redisOperations .execute (new RedisCachePutIfAbsentCallback (element , cacheValueAccessor , cacheMetadata )));
166
+
167
+ new RedisCachePutIfAbsentCallback (new BinaryRedisCacheElement (element , cacheValueAccessor ), cacheMetadata );
168
+
169
+ return toWrapper (cacheValueAccessor .deserializeIfNecessary ((byte []) redisOperations
170
+ .execute (new RedisCachePutIfAbsentCallback (new BinaryRedisCacheElement (element , cacheValueAccessor ),
171
+ cacheMetadata ))));
165
172
}
166
173
167
174
/*
@@ -180,7 +187,8 @@ public void evict(Object key) {
180
187
public void evict (final RedisCacheElement element ) {
181
188
182
189
notNull (element , "Element must not be null!" );
183
- redisOperations .execute (new RedisCacheEvictCallback (element , cacheMetadata ));
190
+ redisOperations .execute (new RedisCacheEvictCallback (new BinaryRedisCacheElement (element , cacheValueAccessor ),
191
+ cacheMetadata ));
184
192
}
185
193
186
194
/*
@@ -323,6 +331,10 @@ static class CacheValueAccessor {
323
331
324
332
byte [] convertToBytesIfNecessary (Object value ) {
325
333
334
+ if (value == null ) {
335
+ return new byte [0 ];
336
+ }
337
+
326
338
if (valueSerializer == null && value instanceof byte []) {
327
339
return (byte []) value ;
328
340
}
@@ -340,6 +352,52 @@ Object deserializeIfNecessary(byte[] value) {
340
352
}
341
353
}
342
354
355
+ /**
356
+ * @author Christoph Strobl
357
+ * @since 1.6
358
+ */
359
+ static class BinaryRedisCacheElement extends RedisCacheElement {
360
+
361
+ private byte [] keyBytes ;
362
+ private byte [] valueBytes ;
363
+ private RedisCacheElement element ;
364
+
365
+ public BinaryRedisCacheElement (RedisCacheElement element , CacheValueAccessor accessor ) {
366
+
367
+ super (element .getKey (), element .get ());
368
+ this .element = element ;
369
+ this .keyBytes = element .getKeyBytes ();
370
+ this .valueBytes = accessor .convertToBytesIfNecessary (element .get ());
371
+ }
372
+
373
+ @ Override
374
+ public byte [] getKeyBytes () {
375
+ return keyBytes ;
376
+ }
377
+
378
+ public long getTimeToLive () {
379
+ return element .getTimeToLive ();
380
+ }
381
+
382
+ public boolean hasKeyPrefix () {
383
+ return element .hasKeyPrefix ();
384
+ }
385
+
386
+ public boolean isEternal () {
387
+ return element .isEternal ();
388
+ }
389
+
390
+ public RedisCacheElement expireAfter (long seconds ) {
391
+ return element .expireAfter (seconds );
392
+ }
393
+
394
+ @ Override
395
+ public byte [] get () {
396
+ return valueBytes ;
397
+ }
398
+
399
+ }
400
+
343
401
/**
344
402
* @author Christoph Strobl
345
403
* @since 1.5
@@ -348,10 +406,10 @@ Object deserializeIfNecessary(byte[] value) {
348
406
static abstract class AbstractRedisCacheCallback <T > implements RedisCallback <T > {
349
407
350
408
private long WAIT_FOR_LOCK_TIMEOUT = 300 ;
351
- private final RedisCacheElement element ;
409
+ private final BinaryRedisCacheElement element ;
352
410
private final RedisCacheMetadata cacheMetadata ;
353
411
354
- public AbstractRedisCacheCallback (RedisCacheElement element , RedisCacheMetadata metadata ) {
412
+ public AbstractRedisCacheCallback (BinaryRedisCacheElement element , RedisCacheMetadata metadata ) {
355
413
this .element = element ;
356
414
this .cacheMetadata = metadata ;
357
415
}
@@ -366,7 +424,7 @@ public T doInRedis(RedisConnection connection) throws DataAccessException {
366
424
return doInRedis (element , connection );
367
425
}
368
426
369
- public abstract T doInRedis (RedisCacheElement element , RedisConnection connection ) throws DataAccessException ;
427
+ public abstract T doInRedis (BinaryRedisCacheElement element , RedisConnection connection ) throws DataAccessException ;
370
428
371
429
protected void processKeyExpiration (RedisCacheElement element , RedisConnection connection ) {
372
430
if (!element .isEternal ()) {
@@ -526,7 +584,7 @@ public Void doInLock(RedisConnection connection) throws DataAccessException {
526
584
*/
527
585
static class RedisCacheEvictCallback extends AbstractRedisCacheCallback <Void > {
528
586
529
- public RedisCacheEvictCallback (RedisCacheElement element , RedisCacheMetadata metadata ) {
587
+ public RedisCacheEvictCallback (BinaryRedisCacheElement element , RedisCacheMetadata metadata ) {
530
588
super (element , metadata );
531
589
}
532
590
@@ -535,7 +593,7 @@ public RedisCacheEvictCallback(RedisCacheElement element, RedisCacheMetadata met
535
593
* @see org.springframework.data.redis.cache.RedisCache.AbstractRedisCacheCallback#doInRedis(org.springframework.data.redis.cache.RedisCacheElement, org.springframework.data.redis.connection.RedisConnection)
536
594
*/
537
595
@ Override
538
- public Void doInRedis (RedisCacheElement element , RedisConnection connection ) throws DataAccessException {
596
+ public Void doInRedis (BinaryRedisCacheElement element , RedisConnection connection ) throws DataAccessException {
539
597
540
598
connection .del (element .getKeyBytes ());
541
599
cleanKnownKeys (element , connection );
@@ -549,25 +607,21 @@ public Void doInRedis(RedisCacheElement element, RedisConnection connection) thr
549
607
*/
550
608
static class RedisCachePutCallback extends AbstractRedisCacheCallback <Void > {
551
609
552
- private final CacheValueAccessor valueAccessor ;
553
-
554
- public RedisCachePutCallback (RedisCacheElement element , CacheValueAccessor valueAccessor ,
555
- RedisCacheMetadata metadata ) {
610
+ public RedisCachePutCallback (BinaryRedisCacheElement element , RedisCacheMetadata metadata ) {
556
611
557
612
super (element , metadata );
558
- this .valueAccessor = valueAccessor ;
559
613
}
560
614
561
615
/*
562
616
* (non-Javadoc)
563
617
* @see org.springframework.data.redis.cache.RedisCache.AbstractRedisPutCallback#doInRedis(org.springframework.data.redis.cache.RedisCache.RedisCacheElement, org.springframework.data.redis.connection.RedisConnection)
564
618
*/
565
619
@ Override
566
- public Void doInRedis (RedisCacheElement element , RedisConnection connection ) throws DataAccessException {
620
+ public Void doInRedis (BinaryRedisCacheElement element , RedisConnection connection ) throws DataAccessException {
567
621
568
622
connection .multi ();
569
623
570
- connection .set (element .getKeyBytes (), valueAccessor . convertToBytesIfNecessary ( element .get () ));
624
+ connection .set (element .getKeyBytes (), element .get ());
571
625
572
626
processKeyExpiration (element , connection );
573
627
maintainKnownKeys (element , connection );
@@ -581,26 +635,21 @@ public Void doInRedis(RedisCacheElement element, RedisConnection connection) thr
581
635
* @author Christoph Strobl
582
636
* @since 1.5
583
637
*/
584
- static class RedisCachePutIfAbsentCallback extends AbstractRedisCacheCallback <Object > {
585
-
586
- private final CacheValueAccessor valueAccessor ;
587
-
588
- public RedisCachePutIfAbsentCallback (RedisCacheElement element , CacheValueAccessor valueAccessor ,
589
- RedisCacheMetadata metadata ) {
638
+ static class RedisCachePutIfAbsentCallback extends AbstractRedisCacheCallback <byte []> {
590
639
640
+ public RedisCachePutIfAbsentCallback (BinaryRedisCacheElement element , RedisCacheMetadata metadata ) {
591
641
super (element , metadata );
592
- this .valueAccessor = valueAccessor ;
593
642
}
594
643
595
644
/*
596
645
* (non-Javadoc)
597
646
* @see org.springframework.data.redis.cache.RedisCache.AbstractRedisPutCallback#doInRedis(org.springframework.data.redis.cache.RedisCache.RedisCacheElement, org.springframework.data.redis.connection.RedisConnection)
598
647
*/
599
648
@ Override
600
- public Object doInRedis (RedisCacheElement element , RedisConnection connection ) throws DataAccessException {
649
+ public byte [] doInRedis (BinaryRedisCacheElement element , RedisConnection connection ) throws DataAccessException {
601
650
602
651
waitForLock (connection );
603
- Object resultValue = put (element , connection );
652
+ byte [] resultValue = put (element , connection );
604
653
605
654
if (nullSafeEquals (element .get (), resultValue )) {
606
655
processKeyExpiration (element , connection );
@@ -610,11 +659,10 @@ public Object doInRedis(RedisCacheElement element, RedisConnection connection) t
610
659
return resultValue ;
611
660
}
612
661
613
- private Object put (RedisCacheElement element , RedisConnection connection ) {
662
+ private byte [] put (BinaryRedisCacheElement element , RedisConnection connection ) {
614
663
615
- boolean valueWasSet = connection .setNX (element .getKeyBytes (),
616
- valueAccessor .convertToBytesIfNecessary (element .get ()));
617
- return valueWasSet ? null : valueAccessor .deserializeIfNecessary (connection .get (element .getKeyBytes ()));
664
+ boolean valueWasSet = connection .setNX (element .getKeyBytes (), element .get ());
665
+ return valueWasSet ? null : connection .get (element .getKeyBytes ());
618
666
}
619
667
}
620
668
0 commit comments