SPEXPIRE

SPEXPIRE key milliseconds [NX | XX | GT | LT] MEMBERS nummembers
  member [member ...]
Available since:
Redis Open Source 8.12.0
Time complexity:
O(N) where N is the number of specified members
ACL categories:
@write, @set, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility

This command works like SEXPIRE, but the expiration of a member is specified in milliseconds instead of seconds.

Required arguments

key

The name of the key that holds the set.

milliseconds

The time to live, in milliseconds. Each specified member is removed after this many milliseconds.

MEMBERS nummembers member [member ...]

The set members to specify expiration for. nummembers is the number of members, followed by that many member names.

Optional arguments

The following options modify the command's behavior. They are mutually exclusive.

NX

For each specified member, apply the expiration only when the member has no expiration.

XX

For each specified member, apply the expiration only when the member has an existing expiration.

GT

For each specified member, apply the expiration only when the new expiration is greater than the current one. A member with no expiration is treated as having an infinite TTL for the purposes of GT.

LT

For each specified member, apply the expiration only when the new expiration is less than the current one. A member with no expiration is treated as having an infinite TTL for the purposes of LT.

Examples

redis> SADD myset member1 member2
(integer) 2
redis> SPEXPIRE myset 2000 MEMBERS 2 member1 member2
1) (integer) 1
2) (integer) 1
redis> SPEXPIRE myset 500 LT MEMBERS 1 member1
1) (integer) 1
redis> SPTTL myset MEMBERS 2 member1 member2
1) (integer) 491
2) (integer) 1982

Details

See SEXPIRE for how member expiration interacts with other set commands. Calling SPEXPIRE with a TTL of 0 removes the member.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

One of the following:

  • Array reply. For each member:
    • Integer reply: -2 if no such member exists in the set, or the key does not exist.
    • Integer reply: 0 if the specified NX, XX, GT, or LT condition was not met.
    • Integer reply: 1 if the expiration time was set or updated.
    • Integer reply: 2 if the member was removed because the TTL is 0.
  • 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 key exists but is not a set.

See also

SEXPIRE | SEXPIREAT | SPEXPIREAT | SADDEX | STTL | SPTTL | SEXPIRETIME | SPEXPIRETIME | SPERSIST

RATE THIS PAGE
Back to top ↑