Cache entry refresh
Read a cached value and assign a fresh relative lifetime in one operation.
Redis · Command
Returns a string value while optionally setting, changing, or removing the key's expiration. GETEX supports relative and absolute expiration forms and is a write command.
GETEX reads the string stored at a key and can change that key's expiration in the same command. Choose one mutually exclusive option: a relative lifetime with EX or PX, an absolute expiration with EXAT or PXAT, or PERSIST to remove the existing lifetime.
GETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST]| Name | Type | Description |
|---|---|---|
key | Redis key | Name of the string key to retrieve. |
EX seconds | optional integer | Relative expiration duration in seconds. |
PX milliseconds | optional integer | Relative expiration duration in milliseconds. |
EXAT unix-time-seconds | optional integer | Absolute Unix expiration time in seconds. |
PXAT unix-time-milliseconds | optional integer | Absolute Unix expiration time in milliseconds. |
PERSIST | optional flag | Remove the key's associated TTL. |
Bulk string reply containing the value of key; a nil reply if the key does not exist.
SET session:42 "user-data"
GETEX session:42 EX 300Stores a value, then reads it while assigning a five-minute expiration.
Read a cached value and assign a fresh relative lifetime in one operation.
Apply a millisecond-precision or second-precision deadline supplied by an application.
Use a shared Unix deadline when several processes must observe the same expiry instant.
Read a value while removing its existing TTL.
GETEX returns the string value for a key and optionally changes its expiration. Unlike GET, it is a write command because expiration metadata may be changed.
Follow-up: Why might GETEX be useful in a cache? It can retrieve an entry and refresh or assign its lifetime in one command, avoiding a separate expiration update.
EX sets a relative lifetime in seconds, while PX uses milliseconds. EXAT and PXAT instead specify absolute Unix expiration times; PERSIST removes the key's associated TTL.
Follow-up: Which option is appropriate for a deadline shared across clients? EXAT or PXAT, because both represent an absolute expiration instant.
If the key exists, GETEX returns its string value. If it does not exist, the command returns a nil or null reply, depending on the client representation.
Follow-up: What should application code do with a missing result? Treat it as a cache miss and avoid assuming the returned value is an empty string.
GETPERSISTTTL