@@ -506,44 +506,62 @@ private void CalculateExpiryValues(DateTime expiry, out RedisValue precision, ou
506506
507507 private Message HashFieldGetAndSetExpiryMessage < T > ( RedisKey key , RedisValue hashField , T ? expiry , CalculateExpiryArgs < T > calculateExpiryArgs , bool persist , CommandFlags flags ) where T : struct
508508 {
509- if ( expiry != null && persist )
509+ if ( expiry != null && persist ) throw new ArgumentException ( "Cannot specify both expiry and persist" ) ;
510+
511+ if ( persist ) // Case when persist is true (expiry is disregarded)
510512 {
511- throw new ArgumentException ( "Cannot specify both expiry and persist" ) ;
513+ return Message . Create ( Database , flags , RedisCommand . HGETEX , key , RedisLiterals . PERSIST , RedisLiterals . FIELDS , 1 , hashField ) ;
512514 }
513- switch ( ( expiry , persist ) )
515+
516+ if ( expiry != null ) // Check if expiry is not null
514517 {
515- case ( _, true ) : // Case when persist is true (expiry is disregarded)
516- return Message . Create ( Database , flags , RedisCommand . HGETEX , key , RedisLiterals . PERSIST , RedisLiterals . FIELDS , 1 , hashField ) ;
517- case ( not null , _) : // Case when expiry is not null
518- calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
519- return Message . Create ( Database , flags , RedisCommand . HGETEX , key , precision , time , RedisLiterals . FIELDS , 1 , hashField ) ;
520- default : // Default case when both expiry and persist are default
521- return Message . Create ( Database , flags , RedisCommand . HGETEX , RedisLiterals . FIELDS , 1 , hashField ) ;
518+ calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
519+ return Message . Create ( Database , flags , RedisCommand . HGETEX , key , precision , time , RedisLiterals . FIELDS , 1 , hashField ) ;
522520 }
521+
522+ // Default case when neither expiry nor persist are set
523+ return Message . Create ( Database , flags , RedisCommand . HGETEX , RedisLiterals . FIELDS , 1 , hashField ) ;
523524 }
524525
525526 private Message HashFieldGetAndSetExpiryMessage < T > ( RedisKey key , RedisValue [ ] hashFields , T ? expiry , CalculateExpiryArgs < T > calculateExpiryArgs , bool persist , CommandFlags flags ) where T : struct
526527 {
527- if ( expiry != null && persist )
528+ if ( expiry != null && persist ) throw new ArgumentException ( "Cannot specify both expiry and persist" ) ;
529+
530+ // Calculate the total size of the array based on conditions
531+ int arraySize = 0 ;
532+ if ( persist ) // Case when persist is true (expiry is disregarded)
528533 {
529- throw new ArgumentException ( "Cannot specify both expiry and persist" ) ;
534+ arraySize = 3 ; // PERSIST, FIELDS, hashFields.Length
530535 }
531- List < RedisValue > ? values = null ;
532- switch ( ( expiry , persist ) )
536+ else if ( expiry != null ) // Case when expiry is not null
533537 {
534- case ( _, true ) : // Case when persist is true (expiry is disregarded)
535- values = new List < RedisValue > { RedisLiterals . PERSIST , RedisLiterals . FIELDS , hashFields . Length } ;
536- break ;
537- case ( not null , _) : // Case when expiry is not null
538- calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
539- values = new List < RedisValue > { precision , time , RedisLiterals . FIELDS , hashFields . Length } ;
540- break ;
541- default : // Default case when both expiry and persist are default
542- values = new List < RedisValue > { RedisLiterals . FIELDS , hashFields . Length } ;
543- break ;
538+ arraySize = 4 ; // precision, time, FIELDS, hashFields.Length
544539 }
545- values . AddRange ( hashFields ) ;
546- return Message . Create ( Database , flags , RedisCommand . HGETEX , key , values . ToArray ( ) ) ;
540+ else // Default case when both expiry and persist are default
541+ {
542+ arraySize = 2 ; // FIELDS, hashFields.Length
543+ }
544+
545+ // Create an array to hold the values (including hashFields)
546+ RedisValue [ ] values = new RedisValue [ arraySize + hashFields . Length ] ;
547+
548+ int index = 0 ;
549+ // Add PERSIST or expiry values, or just FIELDS
550+ if ( persist ) // Case when persist is true (expiry is disregarded)
551+ {
552+ values [ index ++ ] = RedisLiterals . PERSIST ;
553+ }
554+ else if ( expiry != null ) // Check if expiry is not null
555+ {
556+ calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
557+ values [ index ++ ] = precision ;
558+ values [ index ++ ] = time ;
559+ }
560+ values [ index ++ ] = RedisLiterals . FIELDS ;
561+ values [ index ++ ] = hashFields . Length ;
562+ // Add hash fields to the array
563+ Array . Copy ( hashFields , 0 , values , index , hashFields . Length ) ;
564+ return Message . Create ( Database , flags , RedisCommand . HGETEX , key , values ) ;
547565 }
548566
549567 public RedisValue HashFieldGetAndSetExpiry ( RedisKey key , RedisValue hashField , TimeSpan ? expiry = null , bool persist = false , CommandFlags flags = CommandFlags . None )
@@ -628,88 +646,81 @@ public Task<RedisValue[]> HashFieldGetAndSetExpiryAsync(RedisKey key, RedisValue
628646
629647 private Message HashFieldSetAndSetExpiryMessage < T > ( RedisKey key , RedisValue hashField , T ? expiry , CalculateExpiryArgs < T > calculateExpiryArgs , When when , bool keepTtl , CommandFlags flags ) where T : struct
630648 {
631- if ( expiry != null && keepTtl )
632- {
633- throw new ArgumentException ( "Cannot specify both expiry and keepTtl" ) ;
634- }
635- switch ( ( when , expiry , keepTtl ) )
649+ if ( expiry != null && keepTtl ) throw new ArgumentException ( "Cannot specify both expiry and keepTtl" ) ;
650+
651+ if ( when == When . Always )
636652 {
637- case ( When . Always , _ , true ) : // Case when keepTtl is true (expiry is disregarded)
653+ if ( keepTtl ) // Case when keepTtl is true (expiry is disregarded)
654+ {
638655 return Message . Create ( Database , flags , RedisCommand . HSETEX , key , RedisLiterals . KEEPTTL , RedisLiterals . FIELDS , 1 , hashField ) ;
639- case ( When . Always , not null , _ ) : // Case when expiry is not null
640- {
641- calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
642- return Message . Create ( Database , flags , RedisCommand . HSETEX , key , precision , time , RedisLiterals . FIELDS , 1 , hashField ) ;
643- }
644- case ( When . Always , _ , _ ) : // Default case when both expiry and keepTtl are default
645- return Message . Create ( Database , flags , RedisCommand . HSETEX , RedisLiterals . FIELDS , 1 , hashField ) ;
656+ }
646657
647- case ( When . Exists , _ , true ) : // Case when keepTtl is true (expiry is disregarded)
648- case ( When . NotExists , _ , true ) :
649- {
650- var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
651- return Message . Create ( Database , flags , RedisCommand . HSETEX , key , existance , RedisLiterals . KEEPTTL , RedisLiterals . FIELDS , 1 , hashField ) ;
652- }
653- case ( When . Exists , not null , _ ) : // Case when expiry is not null
654- case ( When . NotExists , not null , _ ) :
655- {
656- calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
657- var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
658- return Message . Create ( Database , flags , RedisCommand . HSETEX , key , existance , precision , time , RedisLiterals . FIELDS , 1 , hashField ) ;
659- }
660- default : // Only existance is specified
661- {
662- var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
663- return Message . Create ( Database , flags , RedisCommand . HSETEX , key , existance , RedisLiterals . FIELDS , 1 , hashField ) ;
664- }
658+ if ( expiry != null ) // Case when expiry is not null
659+ {
660+ calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
661+ return Message . Create ( Database , flags , RedisCommand . HSETEX , key , precision , time , RedisLiterals . FIELDS , 1 , hashField ) ;
662+ }
663+ // Default case when both expiry and keepTtl are default
664+ return Message . Create ( Database , flags , RedisCommand . HSETEX , RedisLiterals . FIELDS , 1 , hashField ) ;
665665 }
666+
667+ var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
668+ if ( keepTtl ) // Case when keepTtl is true (expiry is disregarded)
669+ {
670+ return Message . Create ( Database , flags , RedisCommand . HSETEX , key , existance , RedisLiterals . KEEPTTL , RedisLiterals . FIELDS , 1 , hashField ) ;
671+ }
672+ if ( expiry != null ) // Case when expiry is not null
673+ {
674+ calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
675+ return Message . Create ( Database , flags , RedisCommand . HSETEX , key , existance , precision , time , RedisLiterals . FIELDS , 1 , hashField ) ;
676+ }
677+ // Only existance is specified
678+ return Message . Create ( Database , flags , RedisCommand . HSETEX , key , existance , RedisLiterals . FIELDS , 1 , hashField ) ;
666679 }
667680
668681 private Message HashFieldSetAndSetExpiryMessage < T > ( RedisKey key , RedisValue [ ] hashFields , T ? expiry , CalculateExpiryArgs < T > calculateExpiryArgs , When when , bool keepTtl , CommandFlags flags ) where T : struct
669682 {
670- if ( expiry != null && keepTtl )
683+ if ( expiry != null && keepTtl ) throw new ArgumentException ( "Cannot specify both expiry and keepTtl" ) ;
684+
685+ // Determine the base array size
686+ int arraySize = when == When . Always ? 0 : 1 ;
687+
688+ if ( keepTtl )
671689 {
672- throw new ArgumentException ( "Cannot specify both expiry and keepTtl" ) ;
690+ arraySize += 3 ; // KEEPTTL, FIELDS, hashFields.Length
673691 }
674- List < RedisValue > ? values = null ;
675- switch ( ( when , expiry , keepTtl ) )
692+ else if ( expiry != null )
676693 {
677- case ( When . Always , _ , true ) : // Case when keepTtl is true (expiry is disregarded)
678- values = new List < RedisValue > { RedisLiterals . KEEPTTL , RedisLiterals . FIELDS , hashFields . Length } ;
679- break ;
680- case ( When . Always , not null , _ ) : // Case when expiry is not null
681- {
682- calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
683- values = new List < RedisValue > { precision , time , RedisLiterals . FIELDS , hashFields . Length } ;
684- }
685- break ;
686- case ( When . Always , _ , _ ) : // Default case when both expiry and keepTtl are default
687- values = new List < RedisValue > { RedisLiterals . FIELDS , hashFields . Length } ;
688- break ;
689- case ( When . Exists , _ , true ) : // Case when keepTtl is true (expiry is disregarded)
690- case ( When . NotExists , _ , true ) :
691- {
692- var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
693- values = new List < RedisValue > { existance , RedisLiterals . KEEPTTL , RedisLiterals . FIELDS , hashFields . Length } ;
694- }
695- break ;
696- case ( When . Exists , not null , _ ) : // Case when expiry is not null
697- case ( When . NotExists , not null , _ ) :
698- {
699- calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
700- var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
701- values = new List < RedisValue > { existance , precision , time , RedisLiterals . FIELDS , hashFields . Length } ;
702- }
703- break ;
704- default : // Only existance is specified
705- {
706- var existance = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
707- values = new List < RedisValue > { existance , RedisLiterals . FIELDS , hashFields . Length } ;
708- }
709- break ;
694+ arraySize += 4 ; // precision, time, FIELDS, hashFields.Length
710695 }
711- values . AddRange ( hashFields ) ;
712- return Message . Create ( Database , flags , RedisCommand . HSETEX , key , values . ToArray ( ) ) ;
696+ else
697+ {
698+ arraySize += 2 ; // FIELDS, hashFields.Length
699+ }
700+
701+ arraySize += hashFields . Length ;
702+ RedisValue [ ] values = new RedisValue [ arraySize ] ;
703+ int index = 0 ;
704+
705+ if ( when != When . Always )
706+ {
707+ values [ index ++ ] = when == When . Exists ? RedisLiterals . FXX : RedisLiterals . FNX ;
708+ }
709+
710+ if ( keepTtl ) // Case when keepTtl is true (expiry is disregarded)
711+ {
712+ values [ index ++ ] = RedisLiterals . KEEPTTL ;
713+ }
714+ else if ( expiry != null ) // Case when expiry is not null
715+ {
716+ calculateExpiryArgs ( ( T ) expiry ! , out RedisValue precision , out RedisValue time ) ;
717+ values [ index ++ ] = precision ;
718+ values [ index ++ ] = time ;
719+ }
720+ values [ index ++ ] = RedisLiterals . FIELDS ;
721+ values [ index ++ ] = hashFields . Length ;
722+ Array . Copy ( hashFields , 0 , values , index , hashFields . Length ) ;
723+ return Message . Create ( Database , flags , RedisCommand . HSETEX , key , values ) ;
713724 }
714725
715726 public RedisValue HashFieldSetAndSetExpiry ( RedisKey key , RedisValue hashField , TimeSpan ? expiry = null , bool keepTtl = false , When when = When . Always , CommandFlags flags = CommandFlags . None )
0 commit comments