Class HSetExOptions


  • public final class HSetExOptions
    extends java.lang.Object
    Optional arguments for the HSETEX command.

    HSETEX sets hash fields with expiration times and supports:

    • Field conditional changes (FNX/FXX) - control when fields are set based on existence
    • Standard expiry options (EX/PX/EXAT/PXAT/KEEPTTL) - set expiration times

    This class provides compile-time safety by only allowing parameter combinations that are valid for the HSETEX command. It excludes options like PERSIST (which is only for HGETEX) 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:
    HSETEX Command Documentation, FieldConditionalChange, ExpirySet
    Example:
    
     // Set fields only if none exist, with 60 second expiration
     HSetExOptions options = HSetExOptions.builder()
         .onlyIfNoneExist()
         .expiry(ExpirySet.Seconds(60L))
         .build();
    
     Map<String, String> fieldValueMap = Map.of("field1", "value1", "field2", "value2");
     Long result = client.hsetex("myHash", fieldValueMap, options).get();
    
     // Set fields only if all exist, keeping existing expiration
     HSetExOptions options2 = HSetExOptions.builder()
         .onlyIfAllExist()
         .expiry(ExpirySet.KeepExisting())
         .build();
    
     // Set fields with Unix timestamp expiration
     HSetExOptions options3 = HSetExOptions.builder()
         .expiry(ExpirySet.UnixSeconds(1640995200L))
         .build();
     
    • Method Detail

      • toArgs

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

        This method validates that the expiry options are compatible with HSETEX and generates the appropriate command arguments in the correct order.

        The argument order follows the HSETEX command specification:

        1. Field conditional change (FNX/FXX) if specified
        2. Expiry options (EX/PX/EXAT/PXAT/KEEPTTL) if specified
        Returns:
        String[] containing the command arguments for these options
        Throws:
        java.lang.IllegalArgumentException - if expiry options are not compatible with HSETEX
        Example:
        
         HSetExOptions options = HSetExOptions.builder()
             .onlyIfNoneExist()
             .expiry(ExpirySet.Seconds(60L))
             .build();
        
         String[] args = options.toArgs(); // Returns ["FNX", "EX", "60"]
         
      • 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