SADD
Add one or more members to a set. Members that already exist in the set will be ignored.
If the set does not exist, a new set containing only the added members as members is created.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”SADD key member [member ...]Parameter Description
- key: The key of the set
- member: One or more members to add
Detailed Explanation
Section titled “Detailed Explanation”The SADD command is used to add one or more members to a Redis set. Sets are unordered and non-repeating collections of strings, and each member is unique.
Redisun Usage
Section titled “Redisun Usage”In redisun, the SADD command is implemented through the SAddCommand class and the sadd method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Add a single member to the setint added = redisun.sadd("myset", "member1");System.out.println("Added members: " + added); // Output: 1
// Add multiple members to the setadded = redisun.sadd("myset", "member2", "member3");System.out.println("Added members: " + added); // Output: 2
// Add duplicate members, only new members will be addedadded = redisun.sadd("myset", "member1", "member4");System.out.println("Added members: " + added); // Output: 1
// Asynchronous versionCompletableFuture<Integer> future = redisun.asyncSadd("myset", "member5");future.thenAccept(count -> System.out.println("Async added members: " + count));- If the set does not exist, a new set will be automatically created and members will be added
- Members that already exist in the set will be ignored and not added repeatedly
- The command returns the number of new elements successfully added to the set
- Each member in the set is unique, duplicates are not allowed
- Sets are suitable for deduplication scenarios such as tags, friend lists, etc.