GETEX

Introduction and Use Case(s)

The GETEX command in Redis is used to get the value of a key and optionally set its expiration. This command is particularly useful when you need to retrieve the value of a key while simultaneously updating its time-to-live (TTL). Typical scenarios include caching mechanisms where you want to extend the TTL of frequently accessed items without additional commands.

Syntax

  1. GETEX key [EX seconds] [PX milliseconds] [EXAT timestamp-seconds] [PXAT timestamp-milliseconds] [PERSIST]

Parameter Explanations

  • key: The key whose value you want to retrieve.
  • EX seconds: Sets the expiration time of the key in seconds.
  • PX milliseconds: Sets the expiration time of the key in milliseconds.
  • EXAT timestamp-seconds: Sets the expiration time of the key as a Unix timestamp in seconds.
  • PXAT timestamp-milliseconds: Sets the expiration time of the key as a Unix timestamp in milliseconds.
  • PERSIST: Removes the expiration time from the key.

Return Values

The GETEX command returns the value of the specified key if it exists, or nil if the key does not exist.

Examples:

  • If the key exists:

    1. "value"
  • If the key does not exist:

    1. (nil)

Code Examples

  1. dragonfly> SET mykey "Hello"
  2. OK
  3. dragonfly> GETEX mykey EX 10
  4. "Hello"
  5. dragonfly> TTL mykey
  6. (integer) 10
  7. dragonfly> GETEX mykey PX 5000
  8. "Hello"
  9. dragonfly> TTL mykey
  10. (integer) 5
  11. dragonfly> GETEX mykey PERSIST
  12. "Hello"
  13. dragonfly> TTL mykey
  14. (integer) -1
  15. dragonfly> GETEX nonexistingkey EX 10
  16. (nil)

Best Practices

  • Use the GETEX command to minimize the number of operations needed for cache access and TTL updates, improving performance and reducing complexity.
  • Be mindful of setting appropriate expiration times to balance between cache freshness and resource utilization.

Common Mistakes

  • Forgetting that if the key does not exist, GETEX will return nil, which can lead to unexpected null value handling if not properly checked.
  • Misunderstanding TTL values, especially when switching between seconds and milliseconds, which may cause keys to expire sooner or later than intended.

FAQs

What happens if no expiration parameter is given?

If no expiration parameter is provided, GETEX behaves like the GET command and returns the value without modifying the TTL.

Can I use GETEX on non-string data types?

No, GETEX is designed to work with string values. Using it on other data types will result in an error.