Skip to main content
The HSETEX command sets the specified fields with their values and optionally sets their expiration time or TTL. It supports conditional operations to control when fields should be set.

Arguments

key
string
required
The key of the hash.
options
object
Options for conditional setting and expiration.
fields
{ [fieldName]: TValue }
required
An object of fields and their values to set.

Response

The number of fields that were set. Returns 0 if conditions are not met (e.g., conditional: "FNX" but hash exists, or conditional: "FXX" but hash doesn’t exist).
// Set fields with 1 hour expiration
await redis.hsetex("user:123", { expiration: { ex: 3600 } }, { 
  name: "John", 
  email: "john@example.com" 
});

Use Cases

  • Session Management: Create sessions with automatic expiration
  • Cache with TTL: Store cached data that expires automatically
  • Temporary Data: Create temporary records with built-in cleanup
  • Rate Limiting: Store rate limit counters with automatic reset
  • Conditional Updates: Ensure data consistency with FNX/FXX options