PSETEX

Introduction and Use Case(s)

The PSETEX command in Redis sets the value of a key and specifies a time-to-live (TTL) for the key in milliseconds. It’s typically used when you need to store temporary data that should expire after a precise duration.

Syntax

  1. PSETEX key milliseconds value

Parameter Explanations

  • key: The name of the key where the value will be set.
  • milliseconds: The TTL for the key in milliseconds. After this period, the key will automatically be deleted.
  • value: The value to be stored in the key.

Return Values

  • OK: If the operation is successful.

Example:

  1. dragonfly> PSETEX mykey 1000 "Hello, World!"
  2. "OK"

Code Examples

Setting a key with PSETEX and verifying its expiration:

  1. dragonfly> PSETEX mykey 5000 "temporary_value"
  2. "OK"
  3. dragonfly> GET mykey
  4. "temporary_value"
  5. # Wait for 5 seconds
  6. dragonfly> GET mykey
  7. (nil)

Best Practices

  • Use PSETEX when you need operations with millisecond precision for TTLs.
  • Ensure that your application logic accounts for the possible expiry of keys.

Common Mistakes

  • Providing an invalid TTL value (e.g., non-integer or negative values).
  • Expecting the key to persist without accounting for its expiration.

FAQs

What happens if I set a TTL of zero milliseconds?

If you set a TTL of zero milliseconds, the key will be instantly expired and deleted.

Can I update the TTL of an existing key with PSETEX?

No, PSETEX sets both the value and TTL at once. To update the TTL of an existing key, use the PEXPIRE command.