Class HGetExExpiry


  • public final class HGetExExpiry
    extends java.lang.Object
    Configuration of field expiration lifetime specifically for HGETEX command.

    This class provides expiry options that are specifically tailored for the HGETEX command. Unlike the general ExpirySet class, HGetExExpiry:

    • Supports the PERSIST option to remove expiration from fields
    • Does not support the KEEPTTL option (use PERSIST instead)
    • Provides the same relative and absolute time-based expiration options

    The HGETEX command retrieves hash field values and optionally sets their expiration time in a single atomic operation.

    Since:
    Valkey 9.0.0
    See Also:
    HGETEX Command Documentation
    Example:
    
     // Set expiration to 60 seconds from now
     HGetExExpiry expiry = HGetExExpiry.Seconds(60L);
    
     // Set expiration to 5000 milliseconds from now
     HGetExExpiry expiry = HGetExExpiry.Milliseconds(5000L);
    
     // Set expiration to specific Unix timestamp (seconds)
     HGetExExpiry expiry = HGetExExpiry.UnixSeconds(1640995200L);
    
     // Remove expiration (make fields persistent)
     HGetExExpiry expiry = HGetExExpiry.Persist();
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  HGetExExpiry.HGetExExpiryType
      Types of field expiration configuration specific to HGETEX command.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)  
      int hashCode()  
      static HGetExExpiry Milliseconds​(java.lang.Long milliseconds)
      Set the specified expire time, in milliseconds.
      static HGetExExpiry Persist()
      Remove the time to live associated with the field.
      static HGetExExpiry Seconds​(java.lang.Long seconds)
      Set the specified expire time, in seconds.
      java.lang.String[] toArgs()
      Converts HGetExExpiry into a String[] to add to HGETEX command arguments.
      java.lang.String toString()
      Returns a string representation of the object.
      static HGetExExpiry UnixMilliseconds​(java.lang.Long unixMilliseconds)
      Set the specified Unix time at which the field will expire, in milliseconds.
      static HGetExExpiry UnixSeconds​(java.lang.Long unixSeconds)
      Set the specified Unix time at which the field will expire, in seconds.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • Seconds

        public static HGetExExpiry Seconds​(java.lang.Long seconds)
        Set the specified expire time, in seconds. Equivalent to EX in the Valkey API.

        The expiration time is relative to the current time when the HGETEX command is executed. After retrieving the field values, they will be set to expire after the specified number of seconds.

        Parameters:
        seconds - time to expire, in seconds (must be positive)
        Returns:
        HGetExExpiry configured with seconds-based expiration
        Throws:
        java.lang.IllegalArgumentException - if seconds is null or non-positive
        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();
         
      • Milliseconds

        public static HGetExExpiry Milliseconds​(java.lang.Long milliseconds)
        Set the specified expire time, in milliseconds. Equivalent to PX in the Valkey API.

        The expiration time is relative to the current time when the HGETEX command is executed. After retrieving the field values, they will be set to expire after the specified number of milliseconds.

        Parameters:
        milliseconds - time to expire, in milliseconds (must be positive)
        Returns:
        HGetExExpiry configured with milliseconds-based expiration
        Throws:
        java.lang.IllegalArgumentException - if milliseconds is null or non-positive
        Example:
        
         // Retrieve fields and set them to expire in 5000 milliseconds (5 seconds)
         HGetExOptions options = HGetExOptions.builder()
             .expiry(HGetExExpiry.Milliseconds(5000L))
             .build();
         String[] values = client.hgetex("myHash", new String[]{"field1", "field2"}, options).get();
         
      • UnixSeconds

        public static HGetExExpiry UnixSeconds​(java.lang.Long unixSeconds)
        Set the specified Unix time at which the field will expire, in seconds. Equivalent to EXAT in the Valkey API.

        The expiration time is an absolute Unix timestamp in seconds since epoch (January 1, 1970). After retrieving the field values, they will be set to expire at the specified timestamp.

        Parameters:
        unixSeconds - UNIX TIME to expire, in seconds (must be positive)
        Returns:
        HGetExExpiry configured with Unix seconds timestamp
        Throws:
        java.lang.IllegalArgumentException - if unixSeconds is null or non-positive
        Example:
        
         // Retrieve fields and set them to expire at Unix timestamp 1640995200 (January 1, 2022)
         HGetExOptions options = HGetExOptions.builder()
             .expiry(HGetExExpiry.UnixSeconds(1640995200L))
             .build();
         String[] values = client.hgetex("myHash", new String[]{"field1", "field2"}, options).get();
         
      • UnixMilliseconds

        public static HGetExExpiry UnixMilliseconds​(java.lang.Long unixMilliseconds)
        Set the specified Unix time at which the field will expire, in milliseconds. Equivalent to PXAT in the Valkey API.

        The expiration time is an absolute Unix timestamp in milliseconds since epoch (January 1, 1970). After retrieving the field values, they will be set to expire at the specified timestamp.

        Parameters:
        unixMilliseconds - UNIX TIME to expire, in milliseconds (must be positive)
        Returns:
        HGetExExpiry configured with Unix milliseconds timestamp
        Throws:
        java.lang.IllegalArgumentException - if unixMilliseconds is null or non-positive
        Example:
        
         // Retrieve fields and set them to expire at Unix timestamp 1640995200000 (January 1, 2022)
         HGetExOptions options = HGetExOptions.builder()
             .expiry(HGetExExpiry.UnixMilliseconds(1640995200000L))
             .build();
         String[] values = client.hgetex("myHash", new String[]{"field1", "field2"}, options).get();
         
      • Persist

        public static HGetExExpiry Persist()
        Remove the time to live associated with the field. Equivalent to PERSIST in the Valkey API.

        This option removes any existing expiration from the hash fields after retrieving their values, making them persistent (never expire) until explicitly set again. This is the HGETEX equivalent of the KEEPTTL option in other commands.

        Returns:
        HGetExExpiry configured to persist fields (remove expiration)
        Example:
        
         // Retrieve field values and remove their expiration
         HGetExOptions options = HGetExOptions.builder()
             .expiry(HGetExExpiry.Persist())
             .build();
         String[] values = client.hgetex("myHash", new String[]{"field1", "field2"}, options).get();
         // Now field1 and field2 have no expiration time
         
      • toArgs

        public java.lang.String[] toArgs()
        Converts HGetExExpiry into a String[] to add to HGETEX command arguments.

        This method generates the appropriate command arguments based on the expiry type and value. For time-based expiry types, it includes both the type argument and the time value. For PERSIST, it includes only the type argument.

        Returns:
        String[] containing the command arguments for this expiry configuration
        Throws:
        java.lang.AssertionError - if a time-based expiry type has no count value set
        Example:
        
         HGetExExpiry.Seconds(60L).toArgs();      // Returns ["EX", "60"]
         HGetExExpiry.Persist().toArgs();         // Returns ["PERSIST"]
         HGetExExpiry.UnixSeconds(1640995200L).toArgs(); // Returns ["EXAT", "1640995200"]
         
      • toString

        public java.lang.String toString()
        Returns a string representation of the object.

        The string representation includes the class name and the values of all fields in a readable format.

        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of the object
        Example:
        
         HGetExExpiry expiry = HGetExExpiry.Seconds(60L);
         System.out.println(expiry.toString());
         // Output: HGetExExpiry{type=SECONDS, count=60}
        
         HGetExExpiry persist = HGetExExpiry.Persist();
         System.out.println(persist.toString());
         // Output: HGetExExpiry{type=PERSIST, count=null}
         
      • 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