16
16
package org .springframework .data .redis .cache ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
- import static org .awaitility .Awaitility .*;
20
-
21
- import io .netty .util .concurrent .DefaultThreadFactory ;
22
19
23
20
import java .io .Serializable ;
24
21
import java .nio .charset .StandardCharsets ;
29
26
import java .util .Date ;
30
27
import java .util .Objects ;
31
28
import java .util .concurrent .CompletableFuture ;
32
- import java .util .concurrent .CountDownLatch ;
33
- import java .util .concurrent .LinkedBlockingDeque ;
34
- import java .util .concurrent .ThreadPoolExecutor ;
29
+ import java .util .concurrent .ExecutionException ;
35
30
import java .util .concurrent .TimeUnit ;
36
31
import java .util .concurrent .atomic .AtomicBoolean ;
37
- import java .util .concurrent .atomic .AtomicInteger ;
38
32
import java .util .concurrent .atomic .AtomicLong ;
39
- import java .util .concurrent .atomic .AtomicReference ;
40
33
import java .util .function .Consumer ;
41
34
import java .util .function .Function ;
42
35
import java .util .function .Supplier ;
43
- import java .util .stream .IntStream ;
44
36
45
37
import org .junit .jupiter .api .BeforeEach ;
38
+
46
39
import org .springframework .cache .Cache .ValueWrapper ;
47
40
import org .springframework .cache .interceptor .SimpleKey ;
48
41
import org .springframework .cache .interceptor .SimpleKeyGenerator ;
@@ -478,11 +471,10 @@ void cacheGetWithTimeToIdleExpirationWhenEntryNotExpiredShouldReturnValue() {
478
471
479
472
assertThat (unwrap (cache .get (this .key ))).isEqualTo (this .sample );
480
473
481
- for ( int count = 0 ; count < 5 ; count ++) {
474
+ doWithConnection ( connection -> {
482
475
483
- await ().atMost (Duration .ofMillis (100 ));
484
- assertThat (unwrap (cache .get (this .key ))).isEqualTo (this .sample );
485
- }
476
+ assertThat (connection .keyCommands ().ttl (this .binaryCacheKey )).isGreaterThan (1 );
477
+ });
486
478
}
487
479
488
480
@ EnabledOnCommand ("GETEX" )
@@ -496,9 +488,9 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
496
488
497
489
assertThat (unwrap (cache .get (this .key ))).isEqualTo (this .sample );
498
490
499
- await (). atMost ( Duration . ofMillis ( 200 ));
500
-
501
- assertThat ( cache . get ( this . cacheKey , Person . class )). isNull ( );
491
+ doWithConnection ( connection -> {
492
+ assertThat ( connection . keyCommands (). ttl ( this . binaryCacheKey )). isGreaterThan ( 1 );
493
+ } );
502
494
}
503
495
504
496
@ ParameterizedRedisTest // GH-2650
@@ -533,6 +525,30 @@ void retrieveReturnsCachedValue() throws Exception {
533
525
assertThat (value .get (5 , TimeUnit .SECONDS )).isNotNull ();
534
526
assertThat (value .get ().get ()).isEqualTo (this .sample );
535
527
assertThat (value ).isDone ();
528
+
529
+ doWithConnection (connection -> {
530
+ assertThat (connection .keyCommands ().ttl (this .binaryCacheKey )).isEqualTo (-1 );
531
+ });
532
+ }
533
+
534
+ @ ParameterizedRedisTest // GH-2890
535
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
536
+ void retrieveAppliesTimeToIdle () throws ExecutionException , InterruptedException {
537
+
538
+ doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
539
+
540
+ RedisCache cache = new RedisCache ("cache" , usingRedisCacheWriter (),
541
+ usingRedisCacheConfiguration (withTtiExpiration ()));
542
+
543
+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
544
+
545
+ assertThat (value ).isNotNull ();
546
+ assertThat (value .get ().get ()).isEqualTo (this .sample );
547
+ assertThat (value ).isDone ();
548
+
549
+ doWithConnection (connection -> {
550
+ assertThat (connection .keyCommands ().ttl (this .binaryCacheKey )).isGreaterThan (1 );
551
+ });
536
552
}
537
553
538
554
@ ParameterizedRedisTest // GH-2650
@@ -689,7 +705,7 @@ private Object unwrap(@Nullable Object value) {
689
705
private Function <RedisCacheConfiguration , RedisCacheConfiguration > withTtiExpiration () {
690
706
691
707
Function <RedisCacheConfiguration , RedisCacheConfiguration > entryTtlFunction = cacheConfiguration -> cacheConfiguration
692
- .entryTtl (Duration .ofMillis ( 100 ));
708
+ .entryTtl (Duration .ofSeconds ( 10 ));
693
709
694
710
return entryTtlFunction .andThen (RedisCacheConfiguration ::enableTimeToIdle );
695
711
}
0 commit comments