Skip to main content
The HGETEX command returns the values of the specified fields and optionally sets their expiration time or TTL. This allows you to retrieve hash data while managing its lifetime in a single operation.

Arguments

key
string
required
The key of the hash.
options
object
required
Expiration options for the hash fields. The options are mutually exclusive - you can only use one at a time.
fields
...string[]
required
One or more field names to get.

Response

An object containing the field names and their values in the format {[fieldName: string]: TValue | null}. Returns null for fields that do not exist. If the hash doesn’t exist or all fields are non-existent, null is returned.
await redis.hset("user:123", { name: "John", email: "john@example.com" });

// Get fields and set expiration to 60 seconds
const result = await redis.hgetex("user:123", { ex: 60 }, "name", "email");
console.log(result); // { name: "John", email: "john@example.com" }

Use Cases

  • Session Management: Retrieve session data and extend its lifetime
  • Cache Refresh: Get cached data and update its TTL
  • Temporary Data: Access temporary data while managing its expiration
  • Rate Limiting: Fetch rate limit data and reset expiration