Summary
Command Name | SADD |
Usage | Add member(s) to set |
Group | set |
ACL Category | @write @set @fast |
Time Complexity | O(1) |
Flag | WRITE DENYOOM FAST |
Arity | -3 |
Notes
- Time complexity for adding 1 member item is O(1).
- Time complexity for adding N number of member items is O(N).
Signature
SADD <key> <member> [ <member>... ]
Usage
Add one or more items/members to a set. Multiple member elements are supported in the command. So we can add one or more members to the same set at the same time.
Notes
- If the key does not exist, then it is created and then members are added to it.
- If the item/member element already exists in the set, then it is ignored.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Name of the key of the set | key | key |
<member> | Value of the item/member | member | string |
Return Value
Return value | Case for the return value | Type |
---|---|---|
Number of added members | On successful execution, this command returns the number of members of the list | integer |
error | If applied to the wrong data type, or the value is out of the 64-bit signed integer range | error |
Notes
- If the command is applied to a key that is not a hash, then the following error is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Examples
Here are a few examples of the usage examples of the Redis SADD command-
# Redis SADD command examples
# Add members to set
127.0.0.1:6379> sadd bigboxset "first item" "second item" "third item" "just another item"
(integer) 4
# Check set members
127.0.0.1:6379> smembers bigboxset
1) "first item"
2) "second item"
3) "third item"
4) "just another item"
# Add members to set
# Trying to add some already existing members. The existing members are ignored by the command.
127.0.0.1:6379> sadd bigboxset "second item" "New item one" "first item" "New item two"
(integer) 2
# Check set members
127.0.0.1:6379> smembers bigboxset
1) "first item"
2) "second item"
3) "third item"
4) "just another item"
5) "New item one"
6) "New item two"
# Try to add member using SADD, to a non-existing key
# Key is created and members are added
127.0.0.1:6379> sadd nonexistingset one two three
(integer) 3
# Check set members
127.0.0.1:6379> smembers nonexistingset
1) "one"
2) "two"
3) "three"
# Set a string key
127.0.0.1:6379> set bigboxstr "some string value"
OK
# Try to use SADD on the string key
# We get an error
127.0.0.1:6379> sadd bigboxstr "some element"
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Code Implementations
Redis SADD command example implementation in Golang, NodeJS, Java, C#, PHP, Python, Ruby-
// Redis SADD command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
/**
* Add members to set
* Command: sadd bigboxset "first item" "second item" "third item" "just another item"
* Result: (integer) 4
*/
saddResult, err := rdb.SAdd(ctx, "bigboxset", "first item", "second item", "third item", "just another item").Result()
if err != nil {
fmt.Println("Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Error: " + err.Error())
}
fmt.Println("Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: ", saddResult)
/**
* Check set members
* Command: smembers bigboxset
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
*/
smembersResult, err := rdb.SMembers(ctx, "bigboxset").Result()
if err != nil {
fmt.Println("Command: smembers bigboxset | Error: " + err.Error())
}
fmt.Println("Command: smembers bigboxset | Result: ", smembersResult)
/**
* Add members to set
* Trying to add some already existing members. The existing members are ignored by the command.
*
* Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
* Result: (integer) 2
*/
saddResult, err = rdb.SAdd(ctx, "bigboxset", "second item", "New item one", "first item", "New item two").Result()
if err != nil {
fmt.Println("Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Error: " + err.Error())
}
fmt.Println("Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: ", saddResult)
/**
* Check set members
* Command: smembers bigboxset
*
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
* 5) "New item one"
* 6) "New item two"
*/
smembersResult, err = rdb.SMembers(ctx, "bigboxset").Result()
if err != nil {
fmt.Println("Command: smembers bigboxset | Error: " + err.Error())
}
fmt.Println("Command: smembers bigboxset | Result: ", smembersResult)
/**
* Try to add member using SADD, to a non-existing key
* Key is created and members are added
*
* Command: sadd nonexistingset one two three
* Result: (integer) 3
*/
saddResult, err = rdb.SAdd(ctx, "nonexistingset", "one", "two", "three").Result()
if err != nil {
fmt.Println("Command: sadd nonexistingset one two three | Error: " + err.Error())
}
fmt.Println("Command: sadd nonexistingset one two three | Result: ", saddResult)
/**
* Check set members
*
* Command: smembers nonexistingset
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
*/
smembersResult, err = rdb.SMembers(ctx, "nonexistingset").Result()
if err != nil {
fmt.Println("Command: smembers nonexistingset | Error: " + err.Error())
}
fmt.Println("Command: smembers nonexistingset | Result: ", smembersResult)
/**
* Set a string key
* Command: set bigboxstr "some string value"
* Result: OK
*/
setResult, err := rdb.Set(ctx, "bigboxstr", "some string value", 0).Result()
if err != nil {
fmt.Println("Command: set bigboxstr "some string value" | Error: " + err.Error())
}
fmt.Println("Command: set bigboxstr "some string value" | Result: " + setResult)
/**
* Try to use SADD on the string key
* We get an error
*
* Command: sadd bigboxstr "some element"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
saddResult, err = rdb.SAdd(ctx, "bigboxstr", "some element").Result()
if err != nil {
fmt.Println("Command: sadd bigboxstr "some element" | Error: " + err.Error())
}
fmt.Println("Command: sadd bigboxstr "some element" | Result: ", saddResult)
}
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: [first item second item third item just another item]
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: [first item second item third item just another item New item one New item two]
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: [one two three]
Command: set bigboxstr "some string value" | Result: OK
Command: sadd bigboxstr "some element" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: sadd bigboxstr "some element" | Result: 0
Notes
- Use “
SAdd
” method from redis-go module. - Signature of the method is-
SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd
// Redis SADD command example in JavaScript(NodeJS)
import { createClient } from "redis";
// Create redis client
const redisClient = createClient({
url: "redis://default:@localhost:6379",
});
redisClient.on("error", (err) =>
console.log("Error while connecting to Redis", err)
);
// Connect Redis client
await redisClient.connect();
/**
* Add members to set
* Command: sadd bigboxset "first item" "second item" "third item" "just another item"
* Result: (integer) 4
*/
let commandResult = await redisClient.sAdd("bigboxset", [
"first item",
"second item",
"third item",
"just another item",
]);
console.log(
'Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: ' +
commandResult
);
/**
* Check set members
* Command: smembers bigboxset
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
*/
commandResult = await redisClient.sMembers("bigboxset");
console.log("Command: smembers bigboxset | Result: ", commandResult);
/**
* Add members to set
* Trying to add some already existing members. The existing members are ignored by the command.
*
* Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
* Result: (integer) 2
*/
commandResult = await redisClient.sAdd("bigboxset", [
"second item",
"New item one",
"first item",
"New item two",
]);
console.log(
'Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: ' +
commandResult
);
/**
* Check set members
* Command: smembers bigboxset
*
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
* 5) "New item one"
* 6) "New item two"
*/
commandResult = await redisClient.sMembers("bigboxset");
console.log("Command: smembers bigboxset | Result: ", commandResult);
/**
* Try to add member using SADD, to a non-existing key
* Key is created and members are added
*
* Command: sadd nonexistingset one two three
* Result: (integer) 3
*/
commandResult = await redisClient.sAdd("nonexistingset", [
"one",
"two",
"three",
]);
console.log(
"Command: sadd nonexistingset one two three | Result: " + commandResult
);
/**
* Check set members
*
* Command: smembers nonexistingset
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
*/
commandResult = await redisClient.sMembers("nonexistingset");
console.log(
"Command: smembers nonexistingset | Result: ", commandResult
);
/**
* Set a string key
* Command: set bigboxstr "some string value"
* Result: OK
*/
commandResult = await redisClient.set("bigboxstr", "some string value");
console.log(
'Command: set bigboxstr "some string value" | Result: ' + commandResult
);
/**
* Try to use SADD on the string key
* We get an error
*
* Command: sadd bigboxstr "some element"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.sAdd("bigboxstr", "some element");
console.log(
'Command: sadd bigboxstr "some element" | Result: ' + commandResult
);
} catch (err) {
console.log('Command: sadd bigboxstr "some element" | Error: ', err);
}
process.exit(0);
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: [ 'first item', 'second item', 'third item', 'just another item' ]
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: [
'first item',
'second item',
'third item',
'just another item',
'New item one',
'New item two'
]
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: [ 'one', 'two', 'three' ]
Command: set bigboxstr "some string value" | Result: OK
Command: sadd bigboxstr "some element" | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use the function “
sAdd
” of node-redis. - Signature of the method-
function sAdd((key: RedisCommandArgument, members: RedisCommandArgument | Array<RedisCommandArgument>)
// Redis SADD command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Set;
public class SAdd {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Add members to set
* Command: sadd bigboxset "first item" "second item" "third item" "just another item"
* Result: (integer) 4
*/
long saddResult = jedis.sadd("bigboxset", "first item", "second item", "third item", "just another item");
System.out.println("Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: " + saddResult);
/**
* Check set members
* Command: smembers bigboxset
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
*/
Set<String> smembersResult = jedis.smembers("bigboxset");
System.out.println("Command: smembers bigboxset | Result: " + smembersResult.toString());
/**
* Add members to set
* Trying to add some already existing members. The existing members are ignored by the command.
*
* Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
* Result: (integer) 2
*/
saddResult = jedis.sadd("bigboxset", "second item", "New item one", "first item", "New item two");
System.out.println("Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: " + saddResult);
/**
* Check set members
* Command: smembers bigboxset
*
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
* 5) "New item one"
* 6) "New item two"
*/
smembersResult = jedis.smembers("bigboxset");
System.out.println("Command: smembers bigboxset | Result: " + smembersResult.toString());
/**
* Try to add member using SADD, to a non-existing key
* Key is created and members are added
*
* Command: sadd nonexistingset one two three
* Result: (integer) 3
*/
saddResult = jedis.sadd("nonexistingset", "one", "two", "three");
System.out.println("Command: sadd nonexistingset one two three | Result: " + saddResult);
/**
* Check set members
*
* Command: smembers nonexistingset
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
*/
smembersResult = jedis.smembers("nonexistingset");
System.out.println("Command: smembers nonexistingset | Result: " + smembersResult.toString());
/**
* Set a string key
* Command: set bigboxstr "some string value"
* Result: OK
*/
String setResult = jedis.set("bigboxstr", "some string value");
System.out.println("Command: set bigboxstr "some string value" | Result: " + setResult);
/**
* Try to use SADD on the string key
* We get an error
*
* Command: sadd bigboxstr "some element"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
saddResult = jedis.sadd("bigboxstr", "some element");
System.out.println("Command: sadd bigboxstr "some element" | Result: " + saddResult);
} catch (Exception e) {
System.out.println("Command: sadd bigboxstr "some element" | Error: " + e.getMessage());
}
}
}
}
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: [third item, just another item, second item, first item]
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: [third item, just another item, second item, New item two, New item one, first item]
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: [one, two, three]
Command: set bigboxstr "some string value" | Result: OK
Command: sadd bigboxstr "some element" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
sadd
” from Jedis package. - The signature of the method is-
public long sadd(final String key, final String... members)
// Redis SADD command examples in C#
using StackExchange.Redis;
namespace SAdd
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Add members to set
* Command: sadd bigboxset "first item" "second item" "third item" "just another item"
* Result: (integer) 4
*/
long saddResult = rdb.SetAdd("bigboxset", new RedisValue[] { "first item", "second item", "third item", "just another item" });
Console.WriteLine("Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: " + saddResult);
/**
* Check set members
* Command: smembers bigboxset
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
*/
RedisValue[] smembersResult = rdb.SetMembers("bigboxset");
Console.WriteLine("Command: smembers bigboxset | Result: " + String.Join(",", smembersResult));
/**
* Add members to set
* Trying to add some already existing members. The existing members are ignored by the command.
*
* Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
* Result: (integer) 2
*/
saddResult = rdb.SetAdd("bigboxset", new RedisValue[] { "second item", "New item one", "first item", "New item two" });
Console.WriteLine("Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: " + saddResult);
/**
* Check set members
* Command: smembers bigboxset
*
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
* 5) "New item one"
* 6) "New item two"
*/
smembersResult = rdb.SetMembers("bigboxset");
Console.WriteLine("Command: smembers bigboxset | Result: " + String.Join(",", smembersResult));
/**
* Try to add member using SADD, to a non-existing key
* Key is created and members are added
*
* Command: sadd nonexistingset one two three
* Result: (integer) 3
*/
saddResult = rdb.SetAdd("nonexistingset", new RedisValue[] { "one", "two", "three" });
Console.WriteLine("Command: sadd nonexistingset one two three | Result: " + saddResult);
/**
* Check set members
*
* Command: smembers nonexistingset
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
*/
smembersResult = rdb.SetMembers("nonexistingset");
Console.WriteLine("Command: smembers nonexistingset | Result: " + String.Join(",", smembersResult));
/**
* Set a string key
* Command: set bigboxstr "some string value"
* Result: OK
*/
bool setResult = rdb.StringSet("bigboxstr", "some string value");
Console.WriteLine("Command: set bigboxstr "some string value" | Result: " + setResult);
/**
* Try to use SADD on the string key
* We get an error
*
* Command: sadd bigboxstr "some element"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
bool saddResult2 = rdb.SetAdd("bigboxstr", "some element");
Console.WriteLine("Command: sadd bigboxstr "some element" | Result: " + saddResult2);
}
catch (Exception e)
{
Console.WriteLine("Command: sadd bigboxstr "some element" | Error: " + e.Message);
}
}
}
}
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: first item,second item,third item,just another item
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: first item,second item,third item,just another item,New item one,New item two
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: one,two,three
Command: set bigboxstr "some string value" | Result: True
Command: sadd bigboxstr "some element" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “
” from StackExchange.Redis.SetAdd
- Signatures of the method are-
bool SetAdd(RedisKey key, RedisValue value, CommandFlags flags = CommandFlags.None)
long SetAdd(RedisKey key, RedisValue[] values, CommandFlags flags = CommandFlags.None)
<?php
// Redis SADD command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Add members to set
* Command: sadd bigboxset "first item" "second item" "third item" "just another item"
* Result: (integer) 4
*/
$commandResult = $redisClient->sadd("bigboxset", ["first item", "second item", "third item", "just another item"]);
echo "Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: " . $commandResult . "n";
/**
* Check set members
* Command: smembers bigboxset
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
*/
$commandResult = $redisClient->smembers("bigboxset");
echo "Command: smembers bigboxset | Result: ";
print_r($commandResult);
/**
* Add members to set
* Trying to add some already existing members. The existing members are ignored by the command.
*
* Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
* Result: (integer) 2
*/
$commandResult = $redisClient->sadd("bigboxset", ["second item", "New item one", "first item", "New item two"]);
echo "Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: " . $commandResult . "n";
/**
* Check set members
* Command: smembers bigboxset
*
* Result:
* 1) "first item"
* 2) "second item"
* 3) "third item"
* 4) "just another item"
* 5) "New item one"
* 6) "New item two"
*/
$commandResult = $redisClient->smembers("bigboxset");
echo "Command: smembers bigboxset | Result: ";
print_r($commandResult);
/**
* Try to add member using SADD, to a non-existing key
* Key is created and members are added
*
* Command: sadd nonexistingset one two three
* Result: (integer) 3
*/
$commandResult = $redisClient->sadd("nonexistingset", ["one", "two", "three"]);
echo "Command: sadd nonexistingset one two three | Result: " . $commandResult . "n";
/**
* Check set members
*
* Command: smembers nonexistingset
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
*/
$commandResult = $redisClient->smembers("nonexistingset");
echo "Command: smembers nonexistingset | Result: ";
print_r($commandResult);
/**
* Set a string key
* Command: set bigboxstr "some string value"
* Result: OK
*/
$commandResult = $redisClient->set("bigboxstr", "some string value");
echo "Command: set bigboxstr "some string value" | Result: " . $commandResult . "n";
/**
* Try to use SADD on the string key
* We get an error
*
* Command: sadd bigboxstr "some element"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->sadd("bigboxstr", ["some element"]);
echo "Command: sadd bigboxstr "some element" | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: sadd bigboxstr "some element" | Error: " . $e->getMessage() . "n";
}
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: Array
(
[0] => first item
[1] => second item
[2] => third item
[3] => just another item
)
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: Array
(
[0] => first item
[1] => second item
[2] => third item
[3] => just another item
[4] => New item one
[5] => New item two
)
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: Array
(
[0] => one
[1] => two
[2] => three
)
Command: set bigboxstr "some string value" | Result: OK
Command: sadd bigboxstr "some element" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “
sadd
” of predis. - Signature of the method is-
sadd(string $key, array $members): int
# Redis SADD command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(
host="localhost", port=6379, username="default", password="", decode_responses=True
)
# Add members to set
# Command: sadd bigboxset "first item" "second item" "third item" "just another item"
# Result: (integer) 4
commandResult = redisClient.sadd(
"bigboxset", "first item", "second item", "third item", "just another item"
)
print(
'Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: {}'.format(
commandResult
)
)
# Check set members
# Command: smembers bigboxset
# Result:
# 1) "first item"
# 2) "second item"
# 3) "third item"
# 4) "just another item"
commandResult = redisClient.smembers("bigboxset")
print("Command: smembers bigboxset | Result: {}".format(commandResult))
# Add members to set
# Trying to add some already existing members. The existing members are ignored by the command.
# Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
# Result: (integer) 2
commandResult = redisClient.sadd(
"bigboxset", "second item", "New item one", "first item", "New item two"
)
print(
'Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: {}'.format(
commandResult
)
)
# Check set members
# Command: smembers bigboxset
# Result:
# 1) "first item"
# 2) "second item"
# 3) "third item"
# 4) "just another item"
# 5) "New item one"
# 6) "New item two"
commandResult = redisClient.smembers("bigboxset")
print("Command: smembers bigboxset | Result: {}".format(commandResult))
# Try to add member using SADD, to a non-existing key
# Key is created and members are added
# Command: sadd nonexistingset one two three
# Result: (integer) 3
commandResult = redisClient.sadd("nonexistingset", "one", "two", "three")
print("Command: sadd nonexistingset one two three | Result: {}".format(commandResult))
# Check set members
# Command: smembers nonexistingset
# Result:
# 1) "one"
# 2) "two"
# 3) "three"
commandResult = redisClient.smembers("nonexistingset")
print("Command: smembers nonexistingset | Result: {}".format(commandResult))
# Set a string key
# Command: set bigboxstr "some string value"
# Result: OK
commandResult = redisClient.set("bigboxstr", "some string value")
print('Command: set bigboxstr "some string value" | Result: {}'.format(commandResult))
# Try to use SADD on the string key
# We get an error
# Command: sadd bigboxstr "some element"
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.sadd("bigboxstr", "some element")
print('Command: sadd bigboxstr "some element" | Result: {}'.format(commandResult))
except Exception as error:
print('Command: sadd bigboxstr "some element" | Error: ', error)
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: {'second item', 'just another item', 'first item', 'third item'}
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: {'second item', 'New item two', 'first item', 'just another item', 'third item', 'New item one'}
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: {'three', 'two', 'one'}
Command: set bigboxstr "some string value" | Result: True
Command: sadd bigboxstr "some element" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
sadd
” from redis-py. - Signature of the method is –
def sadd(self, name: str, *values: FieldT) -> Union[Awaitable[int], int]
# Redis SADD command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Add members to set
# Command: sadd bigboxset "first item" "second item" "third item" "just another item"
# Result: (integer) 4
commandResult = redis.sadd(
"bigboxset", ["first item", "second item", "third item", "just another item"]
)
print(
"Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: ", commandResult, "n")
# Check set members
# Command: smembers bigboxset
# Result:
# 1) "first item"
# 2) "second item"
# 3) "third item"
# 4) "just another item"
commandResult = redis.smembers("bigboxset")
print("Command: smembers bigboxset | Result: ", commandResult, "n")
# Add members to set
# Trying to add some already existing members. The existing members are ignored by the command.
# Command: sadd bigboxset "second item" "New item one" "first item" "New item two"
# Result: (integer) 2
commandResult = redis.sadd(
"bigboxset", "second item", ["New item one", "first item", "New item two"]
)
print("Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: ", commandResult, "n")
# Check set members
# Command: smembers bigboxset
# Result:
# 1) "first item"
# 2) "second item"
# 3) "third item"
# 4) "just another item"
# 5) "New item one"
# 6) "New item two"
commandResult = redis.smembers("bigboxset")
print("Command: smembers bigboxset | Result: ", commandResult, "n")
# Try to add member using SADD, to a non-existing key
# Key is created and members are added
# Command: sadd nonexistingset one two three
# Result: (integer) 3
commandResult = redis.sadd("nonexistingset", ["one", "two", "three"])
print("Command: sadd nonexistingset one two three | Result: ", commandResult, "n")
# Check set members
# Command: smembers nonexistingset
# Result:
# 1) "one"
# 2) "two"
# 3) "three"
commandResult = redis.smembers("nonexistingset")
print("Command: smembers nonexistingset | Result: ", commandResult, "n")
# Set a string key
# Command: set bigboxstr "some string value"
# Result: OK
commandResult = redis.set("bigboxstr", "some string value")
print("Command: set bigboxstr "some string value" | Result: ", commandResult, "n")
# Try to use SADD on the string key
# We get an error
# Command: sadd bigboxstr "some element"
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.sadd("bigboxstr", "some element")
print("Command: sadd bigboxstr "some element" | Result: ", commandResult, "n")
rescue => e
print('Command: sadd bigboxstr "some element" | Error: ', e, "n")
end
Output:
Command: sadd bigboxset "first item" "second item" "third item" "just another item" | Result: 4
Command: smembers bigboxset | Result: ["first item", "second item", "third item", "just another item"]
Command: sadd bigboxset "second item" "New item one" "first item" "New item two" | Result: 2
Command: smembers bigboxset | Result: ["first item", "second item", "third item", "just another item", "New item one", "New item two"]
Command: sadd nonexistingset one two three | Result: 3
Command: smembers nonexistingset | Result: ["one", "two", "three"]
Command: set bigboxstr "some string value" | Result: OK
Command: sadd bigboxstr "some element" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “
hincrby
” from the redis-rb. - Signature of the method is-
# @param [String] key
# @param [String, Array<String>] member one member, or array of members
# @return [Integer] The number of members that were successfully added
def sadd(key, *members)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
SMEMBERS | Command Details |
SREM | Command Details |