Skip to main content
The HSETEX command sets the specified fields with their values and optionally sets their expiration time or TTL. It supports conditional operations to control when fields should be set.

Arguments

key
str
required
The key of the hash.
field
str
A single field name to set. Use with value parameter.
value
Any
A single value to set. Use with field parameter.
values
Dict[str, Any]
A dictionary of fields and their values to set. Either use field/value or values, but not both.
fnx
bool
Only set fields if the hash does not exist.
fxx
bool
Only set fields if the hash already exists.
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.
keepttl
bool
Retain the existing time to live (TTL) associated with the hash key when setting fields. If the hash has an expiration, it will be preserved.

Response

0 if no fields were set, 1 if all the fields were set.
# Set a single field with expiration
result = redis.hsetex("myhash", "field1", "Hello", ex=60)
assert result == 1

Use Cases

  • Session Management: Create sessions with automatic expiration
  • Cache with TTL: Store cached data that expires automatically
  • Temporary Data: Create temporary records with built-in cleanup
  • Rate Limiting: Store rate limit counters with automatic reset
  • Conditional Updates: Ensure data consistency with FNX/FXX options