HSETEX(3valkey)
β¨ HSETEX β¨
okay bestie, so this is HSETEX from the Valkey Command Manual (that's hsetex(3valkey), for the record) and it is genuinely one of my faves π
πΈ NAME πΈ
hsetex β set the value of one or more fields of a given hash key, and optionally set their expiration time. it's giving "HSET but with a glow-up" π
π¦ 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 π
so HSETEX lets you set the value of one or more fields on a given hash key, and optionally mess with their expiration time. that's the whole vibe β¨
here's the tea on what it returns: it gives back 1 if all the provided fields got set, or 0 if FNX or FXX was provided and none of the specified fields were set.
and like, if you don't pass any optional flags at all? it behaves exactly like a normal valkey-hset(7) HSET command. literally identical, no cap π«Ά
π OPTIONS π
HSETEX supports a lil set of options that change up its behavior:
FNXβ only set the fields if none of them already exist πFXXβ only set the fields if all of them already exist πEXseconds β set the specified expiration time in seconds β±οΈPXmilliseconds β set the specified expiration time in milliseconds β±οΈEXATunix-time-seconds β set the specified Unix time in seconds for when the fields will expire πPXATunix-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, bestie, pls read this part, it matters:
- The
EX,PX,EXAT,PXAT, andKEEPTTLoptions are mutually exclusive β pick ONE, not all of them, okay? π - Setting a value on a volatile hash field (a field which has an assigned expiration time) will remove the expiration for that field.
- Providing
0expiration TTL viaEXorPXoptional arguments will result in the specified fields immediately expiring and being removed from the hash. gone. poof. π - Providing a past expiration time via
EXATorPXAToptional arguments will result in the specified fields immediately expiring and being removed from the hash. same energy π
π REPLY π
RESP2
one of the following, bestie:
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(N) where N is the number of specified fields. linear, we love a predictable queen π
π§ 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 (it said no π):
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 (slay π₯):
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 β this 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 π
the whole friend group, fr fr:
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)
and that's the whole page bestie, hope it ate π this one's version 9.0.0, dated 2025-10-21 β go forth and expire some fields (responsibly) π¦β¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms