SADDEX
SADDEX key [MNX | MXX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] MEMBERS nummembers member [member ...]
- Available since:
- Redis Open Source 8.12.0
- Time complexity:
- O(N) where N is the number of members being added.
- ACL categories:
-
@write,@set,@fast, - Compatibility:
- Redis Software and Redis Cloud compatibility
Add one or more members to a set, and optionally specify their expiration time or time to live (TTL). If the key doesn't exist, Redis creates a new set.
Required arguments
key
The name of the key that holds the set.
MEMBERS nummembers member [member ...]
The members to add. nummembers is the number of members, followed by that many member names.
Optional arguments
The following options modify the command's behavior. MNX and MXX are mutually exclusive. The EX, PX, EXAT, PXAT, and KEEPTTL options are also mutually exclusive.
MNX
Only add the members if none of them already exist in the set.
MXX
Only update the members if all of them already exist in the set.
EX seconds
Set the specified expiration time, in seconds.
PX milliseconds
Set the specified expiration time, in milliseconds.
EXAT unix-time-seconds
Set the specified Unix time, in seconds, at which the members expire.
PXAT unix-time-milliseconds
Set the specified Unix time, in milliseconds, at which the members expire.
KEEPTTL
Retain the TTL of members that already exist in the set. New members are added with no expiration.
Examples
redis> SADDEX myset EX 300 MEMBERS 2 member1 member2
(integer) 1
redis> STTL myset MEMBERS 2 member1 member2
1) (integer) 300
2) (integer) 300
redis> SADDEX myset MNX EX 60 MEMBERS 2 member2 member3
(integer) 0
redis> SADDEX myset KEEPTTL MEMBERS 2 member1 member3
(integer) 1
redis> STTL myset MEMBERS 3 member1 member2 member3
1) (integer) 300
2) (integer) 300
3) (integer) -1
redis> SADDEX myset MEMBERS 1 member1
(integer) 1
redis> STTL myset MEMBERS 1 member1
1) (integer) -1
redis> SADDEX myset MXX PX 1 MEMBERS 2 member2 member4
(integer) 0
redis> SADDEX myset EX 10 KEEPTTL MEMBERS 1 member1
(error) ERR Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments can be specified
Details
SADDEX applies the expiration option to every member you list, both new and existing. Without an expiration option, it adds the members with no TTL and discards the TTL of any member that already has one. Use KEEPTTL to keep existing TTLs instead. SADD never changes a member's TTL.
The MNX and MXX conditions apply to the members as a group. If any member fails the condition, SADDEX changes nothing and returns 0.
An expiration of 0, or a Unix time in the past, removes the listed members from the set instead of adding them. If that leaves the set empty, Redis deletes the key.
See SEXPIRE for how member expiration interacts with other set commands.
Redis Software and Redis Cloud compatibility
| Redis Software |
Redis Cloud |
Notes |
|---|---|---|
| ❌ Standard |
❌ Standard |
Return information
One of the following:
- Integer reply:
0if no member was added, updated, or removed. This includes the case where theMNXorMXXcondition was not met. - Integer reply:
1if at least one member was added, updated, or removed. - 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 | SPEXPIRE | SEXPIREAT | SPEXPIREAT | STTL | SPTTL | SEXPIRETIME | SPEXPIRETIME | SPERSIST