1616package org .springframework .data .redis .cache ;
1717
1818import static org .assertj .core .api .Assertions .*;
19- import static org .awaitility .Awaitility .*;
20-
21- import io .netty .util .concurrent .DefaultThreadFactory ;
2219
2320import java .io .Serializable ;
2421import java .nio .charset .StandardCharsets ;
2926import java .util .Date ;
3027import java .util .Objects ;
3128import 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 ;
3530import java .util .concurrent .TimeUnit ;
3631import java .util .concurrent .atomic .AtomicBoolean ;
37- import java .util .concurrent .atomic .AtomicInteger ;
3832import java .util .concurrent .atomic .AtomicLong ;
39- import java .util .concurrent .atomic .AtomicReference ;
4033import java .util .function .Consumer ;
4134import java .util .function .Function ;
4235import java .util .function .Supplier ;
43- import java .util .stream .IntStream ;
4436
4537import org .junit .jupiter .api .BeforeEach ;
38+
4639import org .springframework .cache .Cache .ValueWrapper ;
4740import org .springframework .cache .interceptor .SimpleKey ;
4841import org .springframework .cache .interceptor .SimpleKeyGenerator ;
@@ -478,11 +471,10 @@ void cacheGetWithTimeToIdleExpirationWhenEntryNotExpiredShouldReturnValue() {
478471
479472 assertThat (unwrap (cache .get (this .key ))).isEqualTo (this .sample );
480473
481- for ( int count = 0 ; count < 5 ; count ++) {
474+ doWithConnection ( connection -> {
482475
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+ });
486478 }
487479
488480 @ EnabledOnCommand ("GETEX" )
@@ -496,9 +488,9 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
496488
497489 assertThat (unwrap (cache .get (this .key ))).isEqualTo (this .sample );
498490
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+ } );
502494 }
503495
504496 @ ParameterizedRedisTest // GH-2650
@@ -533,6 +525,30 @@ void retrieveReturnsCachedValue() throws Exception {
533525 assertThat (value .get (5 , TimeUnit .SECONDS )).isNotNull ();
534526 assertThat (value .get ().get ()).isEqualTo (this .sample );
535527 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+ });
536552 }
537553
538554 @ ParameterizedRedisTest // GH-2650
@@ -689,7 +705,7 @@ private Object unwrap(@Nullable Object value) {
689705 private Function <RedisCacheConfiguration , RedisCacheConfiguration > withTtiExpiration () {
690706
691707 Function <RedisCacheConfiguration , RedisCacheConfiguration > entryTtlFunction = cacheConfiguration -> cacheConfiguration
692- .entryTtl (Duration .ofMillis ( 100 ));
708+ .entryTtl (Duration .ofSeconds ( 10 ));
693709
694710 return entryTtlFunction .andThen (RedisCacheConfiguration ::enableTimeToIdle );
695711 }
0 commit comments