Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

HGETEX

Last updated
HGETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST] FIELDS numfields field [field ...]
Group
Hash
Complexity
O(N) where N is the number of fields given
Arity
-5 (at least 5 words, counting the command name)
Flags
write, fast
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

HGETEX returns the values of the listed fields of the hash at key, like HMGET, and at the same time changes the time to live of the fields that exist. Without an expiry option it only reads.

The values are read first. If the expiry you give is already in the past, the fields are returned and then deleted (and the key too, if no fields remain).

Option Effect
EX seconds Expire the fields after this many seconds.
PX milliseconds Expire the fields after this many milliseconds.
EXAT unix-time-seconds Expire the fields at this Unix time, in seconds.
PXAT unix-time-milliseconds Expire the fields at this Unix time, in milliseconds.
PERSIST Remove the fields’ time to live.
FIELDS numfields field ... The fields to read. Required, exactly numfields names.

Only one of EX, PX, EXAT, PXAT and PERSIST can be given. The options and the FIELDS block can come in any order after the key. Times must be non-negative integers.

Case RESP2 RESP3
Success Array with one entry per field: the value as a bulk string, or nil bulk string if the field does not exist Array of bulk strings, with null for a missing field
Key does not exist Array of nils, one per field Array of nulls

Errors:

  • WRONGTYPE if key holds a value that is not a hash (checked first).
  • ERR Only one of EX, PX, EXAT, PXAT or PERSIST arguments can be specified.
  • ERR value is not an integer or out of range or ERR invalid expire time, must be >= 0 for a bad time.
  • ERR invalid number of fields, ERR wrong number of arguments (fewer names than numfields), ERR FIELDS keyword specified multiple times, ERR missing FIELDS argument, or ERR unknown argument: <arg>.
  • ERR invalid expire time in 'hgetex' command if the time is too far in the future.
127.0.0.1:6379> HSET session:9 token "abc" user "42"
(integer) 2
127.0.0.1:6379> HGETEX session:9 EX 300 FIELDS 2 token nope
1) "abc"
2) (nil)
127.0.0.1:6379> HTTL session:9 FIELDS 1 token
1) (integer) 300
127.0.0.1:6379> HGETEX session:9 PERSIST FIELDS 1 token
1) "abc"
127.0.0.1:6379> HTTL session:9 FIELDS 1 token
1) (integer) -1

Same as Redis 8.0. Redis 7.4 and Valkey 8 do not have this command.

HMGET, HSETEX, HEXPIRE, HPERSIST, HGETDEL, GETEX