Set the value of one or more fields of a given hash key, and optionally set their expiration time.
HSETEX key
[FNX | FXX]
[EX seconds |
PX milliseconds |
EXAT unix-time-seconds |
PXAT unix-time-milliseconds |
KEEPTTL]
FIELDS numfields field
value [field value …]
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 HSET command.
The HSETEX command supports a set of options that modify
its behavior:
unix-time-seconds — Set the specified Unix time in
seconds for when the fields will expire.unix-time-milliseconds — Set the specified Unix
time in milliseconds at which the fields will expire.Note for the following:
EX or PX
optional arguments will result in the specified fields immediately
expiring and being removed from the hash.EXAT or
PXAT optional arguments will result in the specified fields
immediately expiring and being removed from the hash.One of the following:
Integer reply:
0 if none of the provided fields’ values and/or expiration
times were set.
Integer reply:
1 if all the fields’ values and/or expiration times were
set.
One of the following:
Integer reply:
0 if none of the provided fields value and or expiration
time was set.
Integer reply:
1 if all the fields value and or expiration time was
set.
O(N) where N is the number of specified fields.
@fast @hash @write
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
HDEL, HEXISTS, HEXPIRE, HEXPIREAT, HEXPIRETIME, HGET, HGETALL, HGETEX, HINCRBY, HINCRBYFLOAT, HKEYS, HLEN, HMGET, HMSET, HPERSIST, HPEXPIRE, HPEXPIREAT, HPEXPIRETIME, HPTTL, HRANDFIELD, HSCAN, HSET, HSETNX, HSTRLEN, HTTL, HVALS.