HEXPIRE
HEXPIRE key seconds [NX | XX | GT | LT] FIELDS numfields field [field ...]
- Available since:
- Redis Open Source 7.4.0
- Time complexity:
- O(N) where N is the number of specified fields
- ACL categories:
-
@write,@hash,@fast,
Set an expiration (TTL or time to live) on one or more fields of a given hash key. You must specify at least one field. Field(s) will automatically be deleted from the hash key when their TTLs expire.
Field expirations will only be cleared by commands that delete or overwrite the
contents of the hash fields, including HDEL and HSET
commands.
This means that all the operations that conceptually alter the value stored at a hash key's field without replacing it with a new one will leave the TTL untouched.
You can clear the TTL using the HPERSIST command, which turns the hash field back into a persistent field.
Note that calling HEXPIRE/HPEXPIRE with a zero TTL or
HEXPIREAT/HPEXPIREAT with a time in the past will result in the hash field being deleted.
Options
The HEXPIRE command supports a set of options:
NX-- For each specified field, set expiration only when the field has no expiration.XX-- For each specified field, set expiration only when the field has an existing expiration.GT-- For each specified field, set expiration only when the new expiration is greater than current one.LT-- For each specified field, set expiration only when the new expiration is less than current one.
A non-volatile field is treated as an infinite TTL for the purpose of GT and LT.
The NX, XX, GT, and LT options are mutually exclusive.
Refreshing expires
You can call HEXPIRE using as argument a field that already has an
existing TTL set.
In this case, the time to live is updated to the new value.
Redis Query Engine and field expiration
Starting with Redis 8, the Redis Query Engine has enhanced behavior when handling expiring hash fields. For detailed information about how FT.SEARCH and FT.AGGREGATE commands interact with expiring hash fields, see Key and field expiration behavior.
Example
redis> HEXPIRE no-key 20 NX FIELDS 2 field1 field2
(nil)
redis> HSET mykey field1 "hello" field2 "world"
(integer) 2
redis> HEXPIRE mykey 10 FIELDS 3 field1 field2 field3
1) (integer) 1
2) (integer) 1
3) (integer) -2
redis> HGETALL mykey
(empty array)
Return information
One of the following:
- Array reply. For each field:
- Integer reply:
-2if no such field exists in the provided hash key, or the provided key does not exist. - Integer reply:
0if the specified NX | XX | GT | LT condition has not been met. - Integer reply:
1if the expiration time was set/updated. - Integer reply:
2whenHEXPIRE/HPEXPIREis called with 0 seconds/milliseconds or whenHEXPIREAT/HPEXPIREATis called with a past Unix time in seconds/milliseconds.
- Integer reply:
- Simple error reply:
- if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.
- if the provided key exists but is not a hash.