@@ -512,3 +512,107 @@ func TestMultipleClientLL(t *testing.T) {
512512 }
513513 }
514514}
515+
516+ func TestOverrideCacheTTL (t * testing.T ) {
517+ client := makeClient (t , addr )
518+ defer client .Close ()
519+ key := strconv .Itoa (rand .Int ())
520+
521+ val , err := client .Get (context .Background (), time .Second * 5 , key , func (ctx context.Context , key string ) (val string , err error ) {
522+ OverrideCacheTTL (ctx , time .Millisecond * 300 )
523+ return "1" , nil
524+ })
525+ if err != nil || val != "1" {
526+ t .Fatal (err )
527+ }
528+
529+ val , err = client .Get (context .Background (), time .Second * 5 , key , nil )
530+ if err != nil || val != "1" {
531+ t .Fatal (err )
532+ }
533+
534+ time .Sleep (time .Millisecond * 400 )
535+ val , err = client .Get (context .Background (), time .Second * 5 , key , nil ) // should miss
536+ if ! rueidis .IsRedisNil (err ) {
537+ t .Fatal ("expected cache miss after overridden TTL expired" )
538+ }
539+ }
540+
541+ func TestOverrideCacheTTLLL (t * testing.T ) {
542+ client := makeClientWithLuaLock (t , addr )
543+ defer client .Close ()
544+ key := strconv .Itoa (rand .Int ())
545+
546+ val , err := client .Get (context .Background (), time .Second * 5 , key , func (ctx context.Context , key string ) (val string , err error ) {
547+ OverrideCacheTTL (ctx , time .Millisecond * 300 )
548+ return "1" , nil
549+ })
550+ if err != nil || val != "1" {
551+ t .Fatal (err )
552+ }
553+
554+ val , err = client .Get (context .Background (), time .Second * 5 , key , nil )
555+ if err != nil || val != "1" {
556+ t .Fatal (err )
557+ }
558+
559+ time .Sleep (time .Millisecond * 400 )
560+ val , err = client .Get (context .Background (), time .Second * 5 , key , nil ) // should miss
561+ if ! rueidis .IsRedisNil (err ) {
562+ t .Fatal ("expected cache miss after overridden TTL expired" )
563+ }
564+ }
565+
566+ func TestOverrideCacheTTLNegativeCaching (t * testing.T ) {
567+ client := makeClient (t , addr )
568+ defer client .Close ()
569+ key := strconv .Itoa (rand .Int ())
570+
571+ val , err := client .Get (context .Background (), time .Second * 5 , key , func (ctx context.Context , key string ) (val string , err error ) {
572+ OverrideCacheTTL (ctx , time .Millisecond * 300 )
573+ return "NOT_FOUND" , nil
574+ })
575+ if err != nil || val != "NOT_FOUND" {
576+ t .Fatal (err )
577+ }
578+
579+ val , err = client .Get (context .Background (), time .Second * 5 , key , nil )
580+ if err != nil || val != "NOT_FOUND" {
581+ t .Fatal (err )
582+ }
583+
584+ time .Sleep (time .Millisecond * 400 )
585+ val , err = client .Get (context .Background (), time .Second * 5 , key , func (ctx context.Context , key string ) (val string , err error ) {
586+ return "FOUND" , nil
587+ })
588+ if err != nil || val != "FOUND" {
589+ t .Fatal (err )
590+ }
591+ }
592+
593+ func TestOverrideCacheTTLNegativeCachingLL (t * testing.T ) {
594+ client := makeClientWithLuaLock (t , addr )
595+ defer client .Close ()
596+ key := strconv .Itoa (rand .Int ())
597+
598+ val , err := client .Get (context .Background (), time .Second * 5 , key , func (ctx context.Context , key string ) (val string , err error ) {
599+ OverrideCacheTTL (ctx , time .Millisecond * 300 )
600+ return "NOT_FOUND" , nil
601+ })
602+ if err != nil || val != "NOT_FOUND" {
603+ t .Fatal (err )
604+ }
605+
606+ val , err = client .Get (context .Background (), time .Second * 5 , key , nil )
607+ if err != nil || val != "NOT_FOUND" {
608+ t .Fatal (err )
609+ }
610+
611+ time .Sleep (time .Millisecond * 400 )
612+ val , err = client .Get (context .Background (), time .Second * 5 , key , func (ctx context.Context , key string ) (val string , err error ) {
613+ return "FOUND" , nil
614+ })
615+ if err != nil || val != "FOUND" {
616+ t .Fatal (err )
617+ }
618+ }
0 commit comments