hsetex.3valkey - Man Page
Set the value of one or more fields of a given hash key, and optionally set their expiration time.
Synopsis
HSETEX key [FNX | FXX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] FIELDS numfields field value [field value ...]
Description
The HSETEX command allows setting the value of one or more fields of a given hash key, and optionally manipulating their expiration time. The command will return 1 in case all provided fields have been set or 0 in case FNX or FXX were provided and non of the specified fields were set. Without providing any optional flags, this command behaves exactly like a normal valkey-hset(7) HSET command.
Options
The HSETEX command supports a set of options that modify its behavior:
- FNX — Only set the fields if none of them already exist.
- 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 for when 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.
Note for the following:
- The EX, PX, EXAT, PXAT, and KEEPTTL options are mutually exclusive.
- Setting a value on a volatile hash field (A field which has an assigned expiration time) will remove the expiration for that field.
- Providing `0' expiration TTL via
EXorPXoptional arguments will result in the specified fields immediately expiring and being removed from the hash. - Providing past expiration time via
EXATorPXAToptional arguments will result in the specified fields immediately expiring and being removed from the hash.
Reply
Resp2
One of the following:
- valkey-protocol(7) Integer reply:
0if none of the provided fields’ values and/or expiration times were set. - valkey-protocol(7) Integer reply:
1if all the fields’ values and/or expiration times were set.
Resp3
One of the following:
- valkey-protocol(7) Integer reply:
0if none of the provided fields value and or expiration time was set. - valkey-protocol(7) Integer reply:
1if all the fields value and or expiration time was set.
Complexity
O(1)
Acl Categories
@fast @hash @write
History
- Available since: 9.0.0
Examples
Add 3 new items without expiration time to a `myhash'
127.0.0.1:6379> HSETEX myhash FIELDS 3 f1 v1 f2 v2 f3 v3 (integer) 1
Unsuccessful attempt setting expiration time on EXISTING fields
127.0.0.1:6379> HSETEX myhash FNX EX 10 FIELDS 2 f2 v2 f3 v3 (integer) 0
Successful attempt setting expiration time on EXISTING fields
127.0.0.1:6379> HSETEX myhash FXX EX 10 FIELDS 2 f2 v2 f3 v3 (integer) 1
Verify hash fields expiration time:
127.0.0.1:6379> HTTL myhash FIELDS 3 f1 f2 f3 1) (integer) -1 2) (integer) 8 3) (integer) 8
Override all hash items will also persist the fields
127.0.0.1:6379> HSETEX myhash FIELDS 3 f1 v1 f2 v2 f3 v3 (integer) 1 127.0.0.1:6379> HTTL myhash FIELDS 3 f1 f2 f3 1) (integer) -1 2) (integer) -1 3) (integer) -1
Setting expiration time in the past will remove all the elements in the hash:
127.0.0.1:6379> HSETEX EX 0 myhash FIELDS 3 f1 v1 f2 v2 f3 v3 (integer) 1 127.0.0.1:6379> HTTL myhash FIELDS 3 f1 f2 f3 1) (integer) -2 2) (integer) -2 3) (integer) -2 127.0.0.1:6379> HLEN myhash (integer) 0 127.0.0.1:6379> EXISTS myhash (integer) 0
See Also
hdel(3valkey), hexists(3valkey), hexpire(3valkey), hexpireat(3valkey), hexpiretime(3valkey), hget(3valkey), hgetall(3valkey), hgetex(3valkey), hincrby(3valkey), hincrbyfloat(3valkey), hkeys(3valkey), hlen(3valkey), hmget(3valkey), hmset(3valkey), hpersist(3valkey), hpexpire(3valkey), hpexpireat(3valkey), hpexpiretime(3valkey), hpttl(3valkey), hrandfield(3valkey), hscan(3valkey), hset(3valkey), hsetnx(3valkey), hstrlen(3valkey), httl(3valkey), hvals(3valkey)