Upstash Documentation

HGETEX

Get fields and set their expiry.
2 min read

Use HGETEX to read hash fields and change their expiration in the same call.

Without an expiration option it simply returns the values, like HMGET. EX, PX, EXAT, and PXAT give every requested field a new lifetime or deadline, and PERSIST removes the expiration so the fields stop expiring altogether.

Doing both in one command is what makes sliding expirations possible per field: reading a session attribute can extend it, with no window in which another client sees the field without its refreshed lifetime. FIELDS <numfields> introduces the field list and the count must match.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
(EX <seconds> | PX <milliseconds> | EXAT <unix-time-seconds> | PXAT <unix-time-milliseconds> | PERSIST)NoNoChoose one form: EX (set a lifetime in seconds); PX (set a lifetime in milliseconds); EXAT (expire at a Unix timestamp in seconds); PXAT (expire at a Unix timestamp in milliseconds); PERSIST (remove the expiration). Left unchanged when omitted.
FIELDS <numfields> <field> [<field> ...]YesNoFields to target. Give the field count first, then that many field names.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Array of bulk-string values or null values, one per field
RESP3Array of bulk-string values or null values, one per field
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs