15
15
*/
16
16
package org .springframework .data .redis .cache ;
17
17
18
- import static org .assertj .core .api .Assertions .assertThat ;
19
- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20
- import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
21
- import static org .awaitility .Awaitility .await ;
18
+ import static org .assertj .core .api .Assertions .*;
19
+ import static org .awaitility .Awaitility .*;
22
20
23
21
import io .netty .util .concurrent .DefaultThreadFactory ;
24
22
@@ -575,8 +573,7 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
575
573
void retrieveCacheValueUsingJedis () {
576
574
577
575
assertThatExceptionOfType (UnsupportedOperationException .class )
578
- .isThrownBy (() -> this .cache .retrieve (this .binaryCacheKey ))
579
- .withMessageContaining ("RedisCache" );
576
+ .isThrownBy (() -> this .cache .retrieve (this .binaryCacheKey )).withMessageContaining ("RedisCache" );
580
577
}
581
578
582
579
@ ParameterizedRedisTest // GH-2650
@@ -590,23 +587,51 @@ void retrieveLoadedValueUsingJedis() {
590
587
591
588
@ ParameterizedRedisTest // GH-2650
592
589
@ EnabledOnRedisDriver (RedisDriver .LETTUCE )
593
- @ SuppressWarnings ("unchecked" )
594
590
void retrieveReturnsCachedValue () throws Exception {
595
591
596
592
doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
597
593
594
+ RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (),
595
+ usingRedisCacheConfiguration ().disableCachingNullValues ());
596
+
597
+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
598
+
599
+ assertThat (value ).isNotNull ();
600
+ assertThat (value .get (5 , TimeUnit .SECONDS )).isNotNull ();
601
+ assertThat (value .get ().get ()).isEqualTo (this .sample );
602
+ assertThat (value ).isDone ();
603
+ }
604
+
605
+ @ ParameterizedRedisTest // GH-2650
606
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
607
+ void retrieveReturnsCachedNullableValue () throws Exception {
608
+
609
+ doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
610
+
598
611
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
599
612
600
- CompletableFuture <Person > value = ( CompletableFuture < Person >) cache .retrieve (this .key );
613
+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
601
614
602
615
assertThat (value ).isNotNull ();
603
- assertThat (value .get ()).isEqualTo (this .sample );
616
+ assertThat (value .get (). get () ).isEqualTo (this .sample );
604
617
assertThat (value ).isDone ();
605
618
}
606
619
620
+ @ ParameterizedRedisTest // GH-2783
621
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
622
+ void retrieveReturnsCachedNullValue () throws Exception {
623
+
624
+ doWithConnection (connection -> connection .set (binaryCacheKey , binaryNullValue ));
625
+
626
+ CompletableFuture <ValueWrapper > value = (CompletableFuture <ValueWrapper >) cache .retrieve (this .key );
627
+ ValueWrapper wrapper = value .get (5 , TimeUnit .SECONDS );
628
+
629
+ assertThat (wrapper ).isNotNull ();
630
+ assertThat (wrapper .get ()).isNull ();
631
+ }
632
+
607
633
@ ParameterizedRedisTest // GH-2650
608
634
@ EnabledOnRedisDriver (RedisDriver .LETTUCE )
609
- @ SuppressWarnings ("unchecked" )
610
635
void retrieveReturnsCachedValueWhenLockIsReleased () throws Exception {
611
636
612
637
String testValue = "TestValue" ;
@@ -622,13 +647,12 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
622
647
623
648
cacheWriter .lock ("cache" );
624
649
625
- CompletableFuture <String > value = (CompletableFuture <String >) cache .retrieve (this .key );
626
-
650
+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
627
651
assertThat (value ).isNotDone ();
628
652
629
653
cacheWriter .unlock ("cache" );
630
654
631
- assertThat (value .get (15L , TimeUnit .MILLISECONDS )).isEqualTo (testValue );
655
+ assertThat (value .get (15L , TimeUnit .MILLISECONDS ). get () ).isEqualTo (testValue );
632
656
assertThat (value ).isDone ();
633
657
}
634
658
@@ -665,8 +689,9 @@ void retrieveStoresLoadedValue() throws Exception {
665
689
666
690
cache .retrieve (this .key , valueLoaderSupplier ).get ();
667
691
668
- doWithConnection (connection ->
669
- assertThat (connection .keyCommands ().exists ("cache::key-1" .getBytes (StandardCharsets .UTF_8 ))).isTrue ());
692
+ doWithConnection (
693
+ connection -> assertThat (connection .keyCommands ().exists ("cache::key-1" .getBytes (StandardCharsets .UTF_8 )))
694
+ .isTrue ());
670
695
}
671
696
672
697
@ ParameterizedRedisTest // GH-2650
@@ -677,11 +702,18 @@ void retrieveReturnsNull() throws Exception {
677
702
678
703
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
679
704
680
- CompletableFuture <? > value = cache .retrieve (this .key );
705
+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
681
706
682
707
assertThat (value ).isNotNull ();
683
- assertThat (value .get ()).isNull ();
708
+ assertThat (value .get (5 , TimeUnit . SECONDS ). get ( )).isNull ();
684
709
assertThat (value ).isDone ();
710
+
711
+ doWithConnection (connection -> connection .keyCommands ().del (this .binaryCacheKey ));
712
+
713
+ value = cache .retrieve (this .key );
714
+
715
+ assertThat (value ).isNotNull ();
716
+ assertThat (value .get (5 , TimeUnit .SECONDS )).isNull ();
685
717
}
686
718
687
719
private <T > CompletableFuture <T > usingCompletedFuture (T value ) {
0 commit comments