Skip to main content
The HGETEX command returns the values of the specified fields and optionally sets their expiration time or TTL. This allows you to retrieve hash data while managing its lifetime in a single operation.

Arguments

key
str
required
The key of the hash.
fields
*List[str]
required
One or more field names to get.
ex
int
Set expiration time in seconds.
px
int
Set expiration time in milliseconds.
exat
int
Set expiration as Unix timestamp in seconds.
pxat
int
Set expiration as Unix timestamp in milliseconds.
persist
bool
Remove expiration from the hash.

Response

A list of values corresponding to the requested fields in the same order. Returns None for fields that do not exist. If the hash doesn’t exist, None is returned.
redis.hset("user:123", values={"name": "John", "email": "john@example.com"})

# Get fields and set expiration to 60 seconds
result = redis.hgetex("user:123", "name", "email", ex=60)
assert result == ["John", "john@example.com"]

Use Cases

  • Session Management: Retrieve session data and extend its lifetime
  • Cache Refresh: Get cached data and update its TTL
  • Temporary Data: Access temporary data while managing its expiration
  • Rate Limiting: Fetch rate limit data and reset expiration