Use GETEX to read a value and change its expiration in the same call.
EX, PX, EXAT, and PXAT set a new lifetime or deadline, and PERSIST removes the expiration so the key stops expiring. With no option at all the expiration is left exactly as it was, which is the difference from GET plus a separate EXPIRE.
Doing both at once is what makes a sliding expiration correct: reading a session extends it atomically, with no window in which the key could expire between the read and the refresh.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
(EX <seconds> | PX <milliseconds> | EXAT <unix-time-seconds> | PXAT <unix-time-milliseconds> | PERSIST) | No | No | Choose one form: EX (set a lifetime in seconds); PX (set a lifetime in milliseconds); EXAT (expire at a Unix timestamp in seconds); PXAT (expire at a Unix timestamp in milliseconds); PERSIST (remove the expiration). Left unchanged when omitted. |
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Bulk string or Null bulk string or null array |
| RESP3 | Bulk string or Null |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.