Class HGetExOptions


  • public final class HGetExOptions
    extends java.lang.Object
    Optional arguments for the HGETEX command.

    HGETEX retrieves hash field values and optionally sets their expiration time in a single atomic operation. This class supports:

    • Standard expiry options (EX/PX/EXAT/PXAT) - set expiration times
    • PERSIST option - remove expiration from fields

    This class provides compile-time safety by only allowing parameter combinations that are valid for the HGETEX command. It excludes options like KEEPTTL (use PERSIST instead), field conditional changes (which are only for HSETEX), and expiration conditions (which are only for HEXPIRE-family commands).

    All instances of this class are immutable and thread-safe after construction.

    Since:
    Valkey 9.0.0
    See Also:
    HGETEX Command Documentation, HGetExExpiry
    Example:
    
     // Retrieve fields and set them to expire in 60 seconds
     HGetExOptions options = HGetExOptions.builder()
         .expiry(HGetExExpiry.Seconds(60L))
         .build();
    
     String[] values = client.hgetex("myHash", new String[]{"field1", "field2"}, options).get();
    
     // Retrieve fields and remove their expiration (make persistent)
     HGetExOptions options2 = HGetExOptions.builder()
         .expiry(HGetExExpiry.Persist())
         .build();
    
     // Retrieve fields and set expiration to specific Unix timestamp
     HGetExOptions options3 = HGetExOptions.builder()
         .expiry(HGetExExpiry.UnixMilliseconds(1640995200000L))
         .build();
    
     // Retrieve fields without changing expiration (no options needed)
     String[] values2 = client.hgetex("myHash", new String[]{"field1", "field2"}, null).get();
     
    • Method Detail

      • toArgs

        public java.lang.String[] toArgs()
        Converts options into command arguments for the HGETEX command.

        This method generates the appropriate command arguments based on the expiry configuration. If no expiry is specified, it returns an empty array, allowing the HGETEX command to retrieve field values without modifying their expiration.

        The argument order follows the HGETEX command specification:

        1. Expiry options (EX/PX/EXAT/PXAT/PERSIST) if specified
        Returns:
        String[] containing the command arguments for these options, or empty array if no options
        Example:
        
         HGetExOptions options1 = HGetExOptions.builder()
             .expiry(HGetExExpiry.Seconds(60L))
             .build();
         String[] args1 = options1.toArgs(); // Returns ["EX", "60"]
        
         HGetExOptions options2 = HGetExOptions.builder()
             .expiry(HGetExExpiry.Persist())
             .build();
         String[] args2 = options2.toArgs(); // Returns ["PERSIST"]
        
         HGetExOptions options3 = HGetExOptions.builder().build();
         String[] args3 = options3.toArgs(); // Returns []
         
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object