Class ExpirySet


  • public final class ExpirySet
    extends java.lang.Object
    Configuration of field expiration lifetime for commands that support standard expiry options.

    This class provides various ways to set expiration times for hash fields, including:

    • Relative expiration in seconds or milliseconds
    • Absolute expiration using Unix timestamps
    • Keeping existing expiration times
    • Persisting fields (removing expiration)

    Different commands support different subsets of these options. Use validateForCommand(String) to ensure compatibility with specific commands.

    Since:
    Valkey 9.0.0
    See Also:
    HSETEX Command Documentation, HGETEX Command Documentation
    Example:
    
     // Set expiration to 60 seconds from now
     ExpirySet expiry = ExpirySet.Seconds(60L);
    
     // Set expiration to 5000 milliseconds from now
     ExpirySet expiry = ExpirySet.Milliseconds(5000L);
    
     // Set expiration to specific Unix timestamp (seconds)
     ExpirySet expiry = ExpirySet.UnixSeconds(1640995200L);
    
     // Keep existing expiration time (KEEPTTL)
     ExpirySet expiry = ExpirySet.KeepExisting();
    
     // Remove expiration (PERSIST) - only supported by HGETEX
     ExpirySet expiry = ExpirySet.Persist();
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  ExpirySet.ExpiryType
      Types of field expiration configuration.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)
      Indicates whether some other object is "equal to" this one.
      int hashCode()
      Returns a hash code value for the object.
      static ExpirySet KeepExisting()
      Retain the time to live associated with the field.
      static ExpirySet Milliseconds​(java.lang.Long milliseconds)
      Set the specified expire time, in milliseconds.
      static ExpirySet Persist()
      Remove the time to live associated with the field.
      static ExpirySet Seconds​(java.lang.Long seconds)
      Set the specified expire time, in seconds.
      java.lang.String[] toArgs()
      Converts ExpirySet into a String[] to add to command arguments.
      java.lang.String toString()
      Returns a string representation of the object.
      static ExpirySet UnixMilliseconds​(java.lang.Long unixMilliseconds)
      Set the specified Unix time at which the field will expire, in milliseconds.
      static ExpirySet UnixSeconds​(java.lang.Long unixSeconds)
      Set the specified Unix time at which the field will expire, in seconds.
      void validateForCommand​(java.lang.String commandName)
      Validates that this ExpirySet is compatible with the given command.
      • Methods inherited from class java.lang.Object

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

      • KeepExisting

        public static ExpirySet KeepExisting()
        Retain the time to live associated with the field. Equivalent to KEEPTTL in the Valkey API.

        This option preserves any existing expiration time on the hash fields. If a field has no expiration, it will remain without expiration after the operation.

        Note: This option is not supported by the HGETEX command.

        Returns:
        ExpirySet configured to keep existing expiration
        Example:
        
         // Keep existing expiration times when setting new field values
         HSetExOptions options = HSetExOptions.builder()
             .expiry(ExpirySet.KeepExisting())
             .build();
         
      • Seconds

        public static ExpirySet 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 command is executed.

        Parameters:
        seconds - time to expire, in seconds (must be positive)
        Returns:
        ExpirySet configured with seconds-based expiration
        Throws:
        java.lang.IllegalArgumentException - if seconds is null or non-positive
        Example:
        
         // Set fields to expire in 60 seconds
         HSetExOptions options = HSetExOptions.builder()
             .expiry(ExpirySet.Seconds(60L))
             .build();
         
      • Milliseconds

        public static ExpirySet 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 command is executed.

        Parameters:
        milliseconds - time to expire, in milliseconds (must be positive)
        Returns:
        ExpirySet configured with milliseconds-based expiration
        Throws:
        java.lang.IllegalArgumentException - if milliseconds is null or non-positive
        Example:
        
         // Set fields to expire in 5000 milliseconds (5 seconds)
         HSetExOptions options = HSetExOptions.builder()
             .expiry(ExpirySet.Milliseconds(5000L))
             .build();
         
      • UnixSeconds

        public static ExpirySet 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).

        Parameters:
        unixSeconds - UNIX TIME to expire, in seconds (must be positive)
        Returns:
        ExpirySet configured with Unix seconds timestamp
        Throws:
        java.lang.IllegalArgumentException - if unixSeconds is null or non-positive
        Example:
        
         // Set fields to expire at Unix timestamp 1640995200 (January 1, 2022)
         HSetExOptions options = HSetExOptions.builder()
             .expiry(ExpirySet.UnixSeconds(1640995200L))
             .build();
         
      • UnixMilliseconds

        public static ExpirySet 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).

        Parameters:
        unixMilliseconds - UNIX TIME to expire, in milliseconds (must be positive)
        Returns:
        ExpirySet configured with Unix milliseconds timestamp
        Throws:
        java.lang.IllegalArgumentException - if unixMilliseconds is null or non-positive
        Example:
        
         // Set fields to expire at Unix timestamp 1640995200000 (January 1, 2022)
         HSetExOptions options = HSetExOptions.builder()
             .expiry(ExpirySet.UnixMilliseconds(1640995200000L))
             .build();
         
      • Persist

        public static ExpirySet 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, making them persistent (never expire) until explicitly set again.

        Note: This option is only supported by the HGETEX command.

        Returns:
        ExpirySet configured to persist fields (remove expiration)
        Example:
        
         // Remove expiration from fields (only works with HGETEX)
         HGetExOptions options = HGetExOptions.builder()
             .expiry(HGetExExpiry.Persist())
             .build();
         
      • validateForCommand

        public void validateForCommand​(java.lang.String commandName)
        Validates that this ExpirySet is compatible with the given command.

        Different hash field expiration commands support different subsets of expiry options:

        • HSETEX: Supports all options including KEEPTTL, but not PERSIST
        • HGETEX: Supports all options including PERSIST, but not KEEPTTL
        • Other commands: May have their own restrictions
        Parameters:
        commandName - the name of the command to validate against (e.g., "HSETEX", "HGETEX")
        Throws:
        java.lang.IllegalArgumentException - if this ExpirySet is not compatible with the command
        Example:
        
         ExpirySet expiry = ExpirySet.Persist();
         expiry.validateForCommand("HGETEX"); // OK
         expiry.validateForCommand("HSETEX"); // Throws IllegalArgumentException
         
      • toArgs

        public java.lang.String[] toArgs()
        Converts ExpirySet into a String[] to add to 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 KEEPTTL and 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:
        
         ExpirySet.Seconds(60L).toArgs();        // Returns ["EX", "60"]
         ExpirySet.KeepExisting().toArgs();       // Returns ["KEEPTTL"]
         ExpirySet.Persist().toArgs();            // Returns ["PERSIST"]
         
      • equals

        public boolean equals​(java.lang.Object obj)
        Indicates whether some other object is "equal to" this one.

        Two ExpirySet instances are considered equal if they have the same expiry type and count value.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the reference object with which to compare
        Returns:
        true if this object is the same as the obj argument; false otherwise
      • hashCode

        public int hashCode()
        Returns a hash code value for the object.

        The hash code is computed based on the expiry type and count value.

        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a hash code value for this object
      • toString

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

        The string representation includes the class name and the values of the expiry type and count in a readable format.

        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of the object
        Example:
        
         ExpirySet expiry = ExpirySet.Seconds(60L);
         System.out.println(expiry.toString());
         // Output: ExpirySet{type=SECONDS, count=60}