PEXPIREAT

Introduction and Use Case(s)

The PEXPIREAT command in Redis is used to set the expiration time of a key in milliseconds, using an absolute Unix timestamp. This allows for precise control over when a key should expire. Typical use cases include setting timed cache items or implementing time-sensitive data invalidation.

Syntax

  1. PEXPIREAT key milliseconds-timestamp

Parameter Explanations

  • key: The name of the key you want to set an expiration for.
  • milliseconds-timestamp: A Unix timestamp in milliseconds indicating the exact time at which the key will expire.

Return Values

  • (integer) 1: If the timeout was set successfully.
  • (integer) 0: If the key does not exist or the timeout could not be set.

Code Examples

  1. dragonfly> SET mykey "Hello"
  2. OK
  3. dragonfly> PEXPIREAT mykey 1655100000000
  4. (integer) 1
  5. dragonfly> TTL mykey
  6. (integer) 2592000
  7. dragonfly> PEXPIREAT nonexistingkey 1655100000000
  8. (integer) 0

Best Practices

  • Ensure that the Unix timestamp is accurate and correctly represents the intended expiration time.
  • Use PTTL to verify the remaining time-to-live (TTL) of a key if needed.

Common Mistakes

  • Miscalculating the Unix timestamp in milliseconds can lead to unexpected expiration times.
  • Using PEXPIREAT on a non-existent key will result in no operation, returning 0.

FAQs

How is PEXPIREAT different from EXPIREAT?

PEXPIREAT sets the expiration in milliseconds, allowing for more precise timing, whereas EXPIREAT sets it in seconds.

Can I use PEXPIREAT to remove the expiration from a key?

No, PEXPIREAT only sets an expiration. To remove an expiration, use the PERSIST command.