Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

GETEX

Last updated
GETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST]
Group
String
Complexity
O(1)
Arity
-2 (at least 2 words, counting the command name)
Flags
write, fast
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

GETEX returns the string at key like GET and, in the same step, sets or removes its time to live. Without an option it behaves like GET. Use it for sliding expiry, where each read extends the key’s life.

Option Effect
EX seconds Expire after this many seconds.
PX milliseconds Expire after this many milliseconds.
EXAT unix-time-seconds Expire at this Unix time, in seconds.
PXAT unix-time-milliseconds Expire at this Unix time, in milliseconds.
PERSIST Remove the time to live.

Only one option can be given. Times must be positive integers. An absolute time that has already passed returns the value and deletes the key.

Case RESP2 RESP3
The key holds a string Bulk string: the value Bulk string
The key does not exist Nil bulk string Null

Errors:

  • WRONGTYPE when the key holds a value that is not a string.
  • ERR syntax error for an unknown option, more than one option, or an option without its value.
  • ERR value is not an integer or out of range when the time is not an integer.
  • ERR invalid expire time in 'getex' command when the time is zero, negative or overflows.
127.0.0.1:6379> SET session "user:7"
OK
127.0.0.1:6379> GETEX session EX 900
"user:7"
127.0.0.1:6379> TTL session
(integer) 900
127.0.0.1:6379> GETEX session PERSIST
"user:7"
127.0.0.1:6379> TTL session
(integer) -1

Same as Redis 7 and Valkey 8.

GET, GETDEL, EXPIRE, PERSIST, SET