HSETEX
HSETEX key [FNX | FXX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] FIELDS numfields field value [field value ...]
- Available since:
- Redis Open Source 8.0.0
- Time complexity:
- O(N) where N is the number of fields being set.
- ACL categories:
-
@write,@hash,@fast, - Compatibility:
- Redis Software and Redis Cloud compatibility
Set the value of one or more fields of a given hash key and optionally set their expiration time or time-to-live (TTL). If the given key already holds a value, it is overwritten and any previous TTLs associated with the key are discarded.
Required arguments
key
The name of the key that holds the hash.
FIELDS numfields field value [field value ...]
The hash field-value pairs to set. numfields is the number of fields, followed by that many field-value pairs.
Optional arguments
The following options modify the command's behavior. The EX, PX, EXAT, PXAT, and KEEPTTL options are mutually exclusive.
FNX
Only set the fields if none of them already exist. FNX and FXX are mutually exclusive.
FXX
Only set the fields if all of them already exist.
EX seconds
Set the specified expiration time, in seconds.
PX milliseconds
Set the specified expiration time, in milliseconds.
EXAT unix-time-seconds
Set the specified Unix time, in seconds, at which the fields will expire.
PXAT unix-time-milliseconds
Set the specified Unix time, in milliseconds, at which the fields will expire.
KEEPTTL
Retain the TTL associated with the fields.
Examples
redis> HSETEX mykey EXAT 1740470400 FIELDS 2 field1 "Hello" field2 "World"
(integer) 1
redis> HTTL mykey FIELDS 2 field1 field2
1) (integer) 55627
2) (integer) 55627
redis> HSETEX mykey FNX EX 60 FIELDS 2 field1 "Hello" field2 "World"
(integer) 0
redis> HSETEX mykey FXX EX 60 KEEPTTL FIELDS 2 field1 "hello" field2 "world"
(error) ERR Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments can be specified
redis> HSETEX mykey FXX KEEPTTL FIELDS 2 field1 "hello" field2 "world"
(integer) 1
redis> HTTL mykey FIELDS 2 field1 field2
1) (integer) 55481
2) (integer) 55481
Redis Software and Redis Cloud compatibility
| Redis Software |
Redis Cloud |
Notes |
|---|---|---|
| ✅ Standard |
✅ Standard |
Return information
One of the following:
- Integer reply:
0if no fields were set. - Integer reply:
1if all the fields wereset.