Redis sets
Introduction to Redis sets
A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:
- Track unique items (e.g., track all unique IP addresses accessing a given blog post).
- Represent relations (e.g., the set of all users with a given role).
- Perform common set operations such as intersection, unions, and differences.
Basic commands
SADD
adds a new member to a set.SREM
removes the specified member from the set.SISMEMBER
tests a string for set membership.SINTER
returns the set of members that two or more sets have in common (i.e., the intersection).SCARD
returns the size (a.k.a. cardinality) of a set.
See the complete list of set commands.
Examples
-
Store the sets of bikes racing in France and the USA. Note that if you add a member that already exists, it will be ignored.
> SADD bikes:racing:france bike:1 (integer) 1 > SADD bikes:racing:france bike:1 (integer) 0 > SADD bikes:racing:france bike:2 bike:3 (integer) 2 > SADD bikes:racing:usa bike:1 bike:4 (integer) 2
Are you tired of using redis-cli? Try RedisInsight - the developer GUI for Redis.package io.redis.examples; import redis.clients.jedis.UnifiedJedis; import org.junit.Test; import java.util.List; import java.util.Set; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; public class SetsExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res2); // >>> 0 long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3"); System.out.println(res3); // >>> 2 long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); System.out.println(res4); // >>> 2 jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1"); System.out.println(res5); // >>> true boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2"); System.out.println(res6); // >>> false jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa"); System.out.println(res7); // >>> [bike:1] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); long res8 = jedis.scard("bikes:racing:france"); System.out.println(res8); // >>> 3 long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); System.out.println(res9); // >>> 3 Set<String> res10 = jedis.smembers("bikes:racing:france"); System.out.println(res10); // >>> [bike:1, bike:2, bike:3] boolean res11 = jedis.sismember("bikes:racing:france", "bike:1"); System.out.println(res11); // >>> true List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4"); System.out.println(res12); // >>> [true,true,false] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res13); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4"); Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res14); // >>> [bike:1] Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4] Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res16); // >>> [] Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france"); System.out.println(res17); // >>> [bike:4] Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res18); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); long res19 = jedis.srem("bikes:racing:france", "bike:1"); System.out.println(res18); // >>> 1 String res20 = jedis.spop("bikes:racing:france"); System.out.println(res20); // >>> bike:3 Set<String> res21 = jedis.smembers("bikes:racing:france"); System.out.println(res21); // >>> [bike:2, bike:4, bike:5] String res22 = jedis.srandmember("bikes:racing:france"); System.out.println(res22); // >>> bike:4 jedis.close(); } }
import redis r = redis.Redis(decode_responses=True) res1 = r.sadd("bikes:racing:france", "bike:1") print(res1) # >>> 1 res2 = r.sadd("bikes:racing:france", "bike:1") print(res2) # >>> 0 res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3") print(res3) # >>> 2 res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4") print(res4) # >>> 2 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res5 = r.sismember("bikes:racing:usa", "bike:1") print(res5) # >>> 1 res6 = r.sismember("bikes:racing:usa", "bike:2") print(res6) # >>> 1 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res7 = r.sinter("bikes:racing:france", "bikes:racing:usa") print(res7) # >>> {'bike:1'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") res8 = r.scard("bikes:racing:france") print(res8) # >>> 3 res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") print(res9) # >>> 3 res10 = r.smembers("bikes:racing:france") print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'} res11 = r.sismember("bikes:racing:france", "bike:1") print(res11) # >>> 1 res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4") print(res12) # >>> [1, 1, 0] r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res13) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4") res13 = r.sinter( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res13) # >>> {'bike:1'} res14 = r.sunion( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'} res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy") print(res15) # >>> set() res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france") print(res16) # >>> {'bike:4'} res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res17) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") res18 = r.srem("bikes:racing:france", "bike:1") print(res18) # >>> 1 res19 = r.spop("bikes:racing:france") print(res19) # >>> bike:3 res20 = r.smembers("bikes:racing:france") print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'} res21 = r.srandmember("bikes:racing:france") print(res21) # >>> bike:4
-
Check whether bike:1 or bike:2 are racing in the US.
> SISMEMBER bikes:racing:usa bike:1 (integer) 1 > SISMEMBER bikes:racing:usa bike:2 (integer) 0
Are you tired of using redis-cli? Try RedisInsight - the developer GUI for Redis.package io.redis.examples; import redis.clients.jedis.UnifiedJedis; import org.junit.Test; import java.util.List; import java.util.Set; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; public class SetsExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res2); // >>> 0 long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3"); System.out.println(res3); // >>> 2 long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); System.out.println(res4); // >>> 2 jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1"); System.out.println(res5); // >>> true boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2"); System.out.println(res6); // >>> false jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa"); System.out.println(res7); // >>> [bike:1] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); long res8 = jedis.scard("bikes:racing:france"); System.out.println(res8); // >>> 3 long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); System.out.println(res9); // >>> 3 Set<String> res10 = jedis.smembers("bikes:racing:france"); System.out.println(res10); // >>> [bike:1, bike:2, bike:3] boolean res11 = jedis.sismember("bikes:racing:france", "bike:1"); System.out.println(res11); // >>> true List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4"); System.out.println(res12); // >>> [true,true,false] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res13); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4"); Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res14); // >>> [bike:1] Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4] Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res16); // >>> [] Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france"); System.out.println(res17); // >>> [bike:4] Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res18); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); long res19 = jedis.srem("bikes:racing:france", "bike:1"); System.out.println(res18); // >>> 1 String res20 = jedis.spop("bikes:racing:france"); System.out.println(res20); // >>> bike:3 Set<String> res21 = jedis.smembers("bikes:racing:france"); System.out.println(res21); // >>> [bike:2, bike:4, bike:5] String res22 = jedis.srandmember("bikes:racing:france"); System.out.println(res22); // >>> bike:4 jedis.close(); } }
import redis r = redis.Redis(decode_responses=True) res1 = r.sadd("bikes:racing:france", "bike:1") print(res1) # >>> 1 res2 = r.sadd("bikes:racing:france", "bike:1") print(res2) # >>> 0 res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3") print(res3) # >>> 2 res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4") print(res4) # >>> 2 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res5 = r.sismember("bikes:racing:usa", "bike:1") print(res5) # >>> 1 res6 = r.sismember("bikes:racing:usa", "bike:2") print(res6) # >>> 1 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res7 = r.sinter("bikes:racing:france", "bikes:racing:usa") print(res7) # >>> {'bike:1'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") res8 = r.scard("bikes:racing:france") print(res8) # >>> 3 res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") print(res9) # >>> 3 res10 = r.smembers("bikes:racing:france") print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'} res11 = r.sismember("bikes:racing:france", "bike:1") print(res11) # >>> 1 res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4") print(res12) # >>> [1, 1, 0] r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res13) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4") res13 = r.sinter( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res13) # >>> {'bike:1'} res14 = r.sunion( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'} res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy") print(res15) # >>> set() res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france") print(res16) # >>> {'bike:4'} res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res17) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") res18 = r.srem("bikes:racing:france", "bike:1") print(res18) # >>> 1 res19 = r.spop("bikes:racing:france") print(res19) # >>> bike:3 res20 = r.smembers("bikes:racing:france") print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'} res21 = r.srandmember("bikes:racing:france") print(res21) # >>> bike:4
-
Which bikes are competing in both races?
> SINTER bikes:racing:france bikes:racing:usa 1) "bike:1"
Are you tired of using redis-cli? Try RedisInsight - the developer GUI for Redis.package io.redis.examples; import redis.clients.jedis.UnifiedJedis; import org.junit.Test; import java.util.List; import java.util.Set; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; public class SetsExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res2); // >>> 0 long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3"); System.out.println(res3); // >>> 2 long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); System.out.println(res4); // >>> 2 jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1"); System.out.println(res5); // >>> true boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2"); System.out.println(res6); // >>> false jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa"); System.out.println(res7); // >>> [bike:1] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); long res8 = jedis.scard("bikes:racing:france"); System.out.println(res8); // >>> 3 long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); System.out.println(res9); // >>> 3 Set<String> res10 = jedis.smembers("bikes:racing:france"); System.out.println(res10); // >>> [bike:1, bike:2, bike:3] boolean res11 = jedis.sismember("bikes:racing:france", "bike:1"); System.out.println(res11); // >>> true List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4"); System.out.println(res12); // >>> [true,true,false] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res13); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4"); Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res14); // >>> [bike:1] Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4] Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res16); // >>> [] Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france"); System.out.println(res17); // >>> [bike:4] Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res18); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); long res19 = jedis.srem("bikes:racing:france", "bike:1"); System.out.println(res18); // >>> 1 String res20 = jedis.spop("bikes:racing:france"); System.out.println(res20); // >>> bike:3 Set<String> res21 = jedis.smembers("bikes:racing:france"); System.out.println(res21); // >>> [bike:2, bike:4, bike:5] String res22 = jedis.srandmember("bikes:racing:france"); System.out.println(res22); // >>> bike:4 jedis.close(); } }
import redis r = redis.Redis(decode_responses=True) res1 = r.sadd("bikes:racing:france", "bike:1") print(res1) # >>> 1 res2 = r.sadd("bikes:racing:france", "bike:1") print(res2) # >>> 0 res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3") print(res3) # >>> 2 res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4") print(res4) # >>> 2 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res5 = r.sismember("bikes:racing:usa", "bike:1") print(res5) # >>> 1 res6 = r.sismember("bikes:racing:usa", "bike:2") print(res6) # >>> 1 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res7 = r.sinter("bikes:racing:france", "bikes:racing:usa") print(res7) # >>> {'bike:1'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") res8 = r.scard("bikes:racing:france") print(res8) # >>> 3 res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") print(res9) # >>> 3 res10 = r.smembers("bikes:racing:france") print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'} res11 = r.sismember("bikes:racing:france", "bike:1") print(res11) # >>> 1 res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4") print(res12) # >>> [1, 1, 0] r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res13) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4") res13 = r.sinter( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res13) # >>> {'bike:1'} res14 = r.sunion( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'} res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy") print(res15) # >>> set() res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france") print(res16) # >>> {'bike:4'} res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res17) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") res18 = r.srem("bikes:racing:france", "bike:1") print(res18) # >>> 1 res19 = r.spop("bikes:racing:france") print(res19) # >>> bike:3 res20 = r.smembers("bikes:racing:france") print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'} res21 = r.srandmember("bikes:racing:france") print(res21) # >>> bike:4
-
How many bikes are racing in France?
> SCARD bikes:racing:france (integer) 3
Are you tired of using redis-cli? Try RedisInsight - the developer GUI for Redis.package io.redis.examples; import redis.clients.jedis.UnifiedJedis; import org.junit.Test; import java.util.List; import java.util.Set; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; public class SetsExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.sadd("bikes:racing:france", "bike:1"); System.out.println(res2); // >>> 0 long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3"); System.out.println(res3); // >>> 2 long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); System.out.println(res4); // >>> 2 jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1"); System.out.println(res5); // >>> true boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2"); System.out.println(res6); // >>> false jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa"); System.out.println(res7); // >>> [bike:1] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); long res8 = jedis.scard("bikes:racing:france"); System.out.println(res8); // >>> 3 long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); System.out.println(res9); // >>> 3 Set<String> res10 = jedis.smembers("bikes:racing:france"); System.out.println(res10); // >>> [bike:1, bike:2, bike:3] boolean res11 = jedis.sismember("bikes:racing:france", "bike:1"); System.out.println(res11); // >>> true List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4"); System.out.println(res12); // >>> [true,true,false] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res13); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4"); Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res14); // >>> [bike:1] Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4] Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); System.out.println(res16); // >>> [] Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france"); System.out.println(res17); // >>> [bike:4] Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); System.out.println(res18); // >>> [bike:2, bike:3] jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); long res19 = jedis.srem("bikes:racing:france", "bike:1"); System.out.println(res18); // >>> 1 String res20 = jedis.spop("bikes:racing:france"); System.out.println(res20); // >>> bike:3 Set<String> res21 = jedis.smembers("bikes:racing:france"); System.out.println(res21); // >>> [bike:2, bike:4, bike:5] String res22 = jedis.srandmember("bikes:racing:france"); System.out.println(res22); // >>> bike:4 jedis.close(); } }
import redis r = redis.Redis(decode_responses=True) res1 = r.sadd("bikes:racing:france", "bike:1") print(res1) # >>> 1 res2 = r.sadd("bikes:racing:france", "bike:1") print(res2) # >>> 0 res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3") print(res3) # >>> 2 res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4") print(res4) # >>> 2 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res5 = r.sismember("bikes:racing:usa", "bike:1") print(res5) # >>> 1 res6 = r.sismember("bikes:racing:usa", "bike:2") print(res6) # >>> 1 r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res7 = r.sinter("bikes:racing:france", "bikes:racing:usa") print(res7) # >>> {'bike:1'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") res8 = r.scard("bikes:racing:france") print(res8) # >>> 3 res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") print(res9) # >>> 3 res10 = r.smembers("bikes:racing:france") print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'} res11 = r.sismember("bikes:racing:france", "bike:1") print(res11) # >>> 1 res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4") print(res12) # >>> [1, 1, 0] r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res13) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3") r.sadd("bikes:racing:usa", "bike:1", "bike:4") r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4") res13 = r.sinter( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res13) # >>> {'bike:1'} res14 = r.sunion( "bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy" ) print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'} res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy") print(res15) # >>> set() res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france") print(res16) # >>> {'bike:4'} res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa") print(res17) # >>> {'bike:2', 'bike:3'} r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") res18 = r.srem("bikes:racing:france", "bike:1") print(res18) # >>> 1 res19 = r.spop("bikes:racing:france") print(res19) # >>> bike:3 res20 = r.smembers("bikes:racing:france") print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'} res21 = r.srandmember("bikes:racing:france") print(res21) # >>> bike:4
Tutorial
The SADD
command adds new elements to a set. It's also possible
to do a number of other operations against sets like testing if a given element
already exists, performing the intersection, union or difference between
multiple sets, and so forth.
> SADD bikes:racing:france bike:1 bike:2 bike:3
(integer) 3
> SMEMBERS bikes:racing:france
1) bike:3
2) bike:1
3) bike:2
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
public class SetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res2); // >>> 0
long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3");
System.out.println(res3); // >>> 2
long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
System.out.println(res4); // >>> 2
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1");
System.out.println(res5); // >>> true
boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2");
System.out.println(res6); // >>> false
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa");
System.out.println(res7); // >>> [bike:1]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
long res8 = jedis.scard("bikes:racing:france");
System.out.println(res8); // >>> 3
long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
System.out.println(res9); // >>> 3
Set<String> res10 = jedis.smembers("bikes:racing:france");
System.out.println(res10); // >>> [bike:1, bike:2, bike:3]
boolean res11 = jedis.sismember("bikes:racing:france", "bike:1");
System.out.println(res11); // >>> true
List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4");
System.out.println(res12); // >>> [true,true,false]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res13); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4");
Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res14); // >>> [bike:1]
Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4]
Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res16); // >>> []
Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france");
System.out.println(res17); // >>> [bike:4]
Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res18); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
long res19 = jedis.srem("bikes:racing:france", "bike:1");
System.out.println(res18); // >>> 1
String res20 = jedis.spop("bikes:racing:france");
System.out.println(res20); // >>> bike:3
Set<String> res21 = jedis.smembers("bikes:racing:france");
System.out.println(res21); // >>> [bike:2, bike:4, bike:5]
String res22 = jedis.srandmember("bikes:racing:france");
System.out.println(res22); // >>> bike:4
jedis.close();
}
}
import redis
r = redis.Redis(decode_responses=True)
res1 = r.sadd("bikes:racing:france", "bike:1")
print(res1) # >>> 1
res2 = r.sadd("bikes:racing:france", "bike:1")
print(res2) # >>> 0
res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3")
print(res3) # >>> 2
res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4")
print(res4) # >>> 2
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res5 = r.sismember("bikes:racing:usa", "bike:1")
print(res5) # >>> 1
res6 = r.sismember("bikes:racing:usa", "bike:2")
print(res6) # >>> 1
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res7 = r.sinter("bikes:racing:france", "bikes:racing:usa")
print(res7) # >>> {'bike:1'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
res8 = r.scard("bikes:racing:france")
print(res8) # >>> 3
res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
print(res9) # >>> 3
res10 = r.smembers("bikes:racing:france")
print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'}
res11 = r.sismember("bikes:racing:france", "bike:1")
print(res11) # >>> 1
res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4")
print(res12) # >>> [1, 1, 0]
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res13) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4")
res13 = r.sinter(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res13) # >>> {'bike:1'}
res14 = r.sunion(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'}
res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy")
print(res15) # >>> set()
res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france")
print(res16) # >>> {'bike:4'}
res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res17) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
res18 = r.srem("bikes:racing:france", "bike:1")
print(res18) # >>> 1
res19 = r.spop("bikes:racing:france")
print(res19) # >>> bike:3
res20 = r.smembers("bikes:racing:france")
print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'}
res21 = r.srandmember("bikes:racing:france")
print(res21) # >>> bike:4
Here I've added three elements to my set and told Redis to return all the elements. There is no order guarantee with a set. Redis is free to return the elements in any order at every call.
Redis has commands to test for set membership. These commands can be used on single as well as multiple items:
> SISMEMBER bikes:racing:france bike:1
(integer) 1
> SMISMEMBER bikes:racing:france bike:2 bike:3 bike:4
1) (integer) 1
2) (integer) 1
3) (integer) 0
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
public class SetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res2); // >>> 0
long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3");
System.out.println(res3); // >>> 2
long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
System.out.println(res4); // >>> 2
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1");
System.out.println(res5); // >>> true
boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2");
System.out.println(res6); // >>> false
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa");
System.out.println(res7); // >>> [bike:1]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
long res8 = jedis.scard("bikes:racing:france");
System.out.println(res8); // >>> 3
long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
System.out.println(res9); // >>> 3
Set<String> res10 = jedis.smembers("bikes:racing:france");
System.out.println(res10); // >>> [bike:1, bike:2, bike:3]
boolean res11 = jedis.sismember("bikes:racing:france", "bike:1");
System.out.println(res11); // >>> true
List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4");
System.out.println(res12); // >>> [true,true,false]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res13); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4");
Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res14); // >>> [bike:1]
Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4]
Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res16); // >>> []
Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france");
System.out.println(res17); // >>> [bike:4]
Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res18); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
long res19 = jedis.srem("bikes:racing:france", "bike:1");
System.out.println(res18); // >>> 1
String res20 = jedis.spop("bikes:racing:france");
System.out.println(res20); // >>> bike:3
Set<String> res21 = jedis.smembers("bikes:racing:france");
System.out.println(res21); // >>> [bike:2, bike:4, bike:5]
String res22 = jedis.srandmember("bikes:racing:france");
System.out.println(res22); // >>> bike:4
jedis.close();
}
}
import redis
r = redis.Redis(decode_responses=True)
res1 = r.sadd("bikes:racing:france", "bike:1")
print(res1) # >>> 1
res2 = r.sadd("bikes:racing:france", "bike:1")
print(res2) # >>> 0
res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3")
print(res3) # >>> 2
res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4")
print(res4) # >>> 2
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res5 = r.sismember("bikes:racing:usa", "bike:1")
print(res5) # >>> 1
res6 = r.sismember("bikes:racing:usa", "bike:2")
print(res6) # >>> 1
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res7 = r.sinter("bikes:racing:france", "bikes:racing:usa")
print(res7) # >>> {'bike:1'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
res8 = r.scard("bikes:racing:france")
print(res8) # >>> 3
res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
print(res9) # >>> 3
res10 = r.smembers("bikes:racing:france")
print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'}
res11 = r.sismember("bikes:racing:france", "bike:1")
print(res11) # >>> 1
res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4")
print(res12) # >>> [1, 1, 0]
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res13) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4")
res13 = r.sinter(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res13) # >>> {'bike:1'}
res14 = r.sunion(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'}
res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy")
print(res15) # >>> set()
res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france")
print(res16) # >>> {'bike:4'}
res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res17) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
res18 = r.srem("bikes:racing:france", "bike:1")
print(res18) # >>> 1
res19 = r.spop("bikes:racing:france")
print(res19) # >>> bike:3
res20 = r.smembers("bikes:racing:france")
print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'}
res21 = r.srandmember("bikes:racing:france")
print(res21) # >>> bike:4
We can also find the difference between two sets. For instance, we may want to know which bikes are racing in France but not in the USA:
> SADD bikes:racing:usa bike:1 bike:4
(integer) 2
> SDIFF bikes:racing:france bikes:racing:usa
1) "bike:3"
2) "bike:2"
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
public class SetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res2); // >>> 0
long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3");
System.out.println(res3); // >>> 2
long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
System.out.println(res4); // >>> 2
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1");
System.out.println(res5); // >>> true
boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2");
System.out.println(res6); // >>> false
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa");
System.out.println(res7); // >>> [bike:1]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
long res8 = jedis.scard("bikes:racing:france");
System.out.println(res8); // >>> 3
long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
System.out.println(res9); // >>> 3
Set<String> res10 = jedis.smembers("bikes:racing:france");
System.out.println(res10); // >>> [bike:1, bike:2, bike:3]
boolean res11 = jedis.sismember("bikes:racing:france", "bike:1");
System.out.println(res11); // >>> true
List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4");
System.out.println(res12); // >>> [true,true,false]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res13); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4");
Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res14); // >>> [bike:1]
Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4]
Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res16); // >>> []
Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france");
System.out.println(res17); // >>> [bike:4]
Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res18); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
long res19 = jedis.srem("bikes:racing:france", "bike:1");
System.out.println(res18); // >>> 1
String res20 = jedis.spop("bikes:racing:france");
System.out.println(res20); // >>> bike:3
Set<String> res21 = jedis.smembers("bikes:racing:france");
System.out.println(res21); // >>> [bike:2, bike:4, bike:5]
String res22 = jedis.srandmember("bikes:racing:france");
System.out.println(res22); // >>> bike:4
jedis.close();
}
}
import redis
r = redis.Redis(decode_responses=True)
res1 = r.sadd("bikes:racing:france", "bike:1")
print(res1) # >>> 1
res2 = r.sadd("bikes:racing:france", "bike:1")
print(res2) # >>> 0
res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3")
print(res3) # >>> 2
res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4")
print(res4) # >>> 2
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res5 = r.sismember("bikes:racing:usa", "bike:1")
print(res5) # >>> 1
res6 = r.sismember("bikes:racing:usa", "bike:2")
print(res6) # >>> 1
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res7 = r.sinter("bikes:racing:france", "bikes:racing:usa")
print(res7) # >>> {'bike:1'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
res8 = r.scard("bikes:racing:france")
print(res8) # >>> 3
res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
print(res9) # >>> 3
res10 = r.smembers("bikes:racing:france")
print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'}
res11 = r.sismember("bikes:racing:france", "bike:1")
print(res11) # >>> 1
res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4")
print(res12) # >>> [1, 1, 0]
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res13) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4")
res13 = r.sinter(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res13) # >>> {'bike:1'}
res14 = r.sunion(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'}
res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy")
print(res15) # >>> set()
res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france")
print(res16) # >>> {'bike:4'}
res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res17) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
res18 = r.srem("bikes:racing:france", "bike:1")
print(res18) # >>> 1
res19 = r.spop("bikes:racing:france")
print(res19) # >>> bike:3
res20 = r.smembers("bikes:racing:france")
print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'}
res21 = r.srandmember("bikes:racing:france")
print(res21) # >>> bike:4
There are other non trivial operations that are still easy to implement
using the right Redis commands. For instance we may want a list of all the
bikes racing in France, the USA, and some other races. We can do this using
the SINTER
command, which performs the intersection between different
sets. In addition to intersection you can also perform
unions, difference, and more. For example
if we add a third race we can see some of these commands in action:
> SADD bikes:racing:france bike:1 bike:2 bike:3
(integer) 3
> SADD bikes:racing:usa bike:1 bike:4
(integer) 2
> SADD bikes:racing:italy bike:1 bike:2 bike:3 bike:4
(integer) 4
> SINTER bikes:racing:france bikes:racing:usa bikes:racing:italy
1) "bike:1"
> SUNION bikes:racing:france bikes:racing:usa bikes:racing:italy
1) "bike:2"
2) "bike:1"
3) "bike:4"
4) "bike:3"
> SDIFF bikes:racing:france bikes:racing:usa bikes:racing:italy
(empty array)
> SDIFF bikes:racing:france bikes:racing:usa
1) "bike:3"
2) "bike:2"
> SDIFF bikes:racing:usa bikes:racing:france
1) "bike:4"
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
public class SetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res2); // >>> 0
long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3");
System.out.println(res3); // >>> 2
long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
System.out.println(res4); // >>> 2
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1");
System.out.println(res5); // >>> true
boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2");
System.out.println(res6); // >>> false
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa");
System.out.println(res7); // >>> [bike:1]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
long res8 = jedis.scard("bikes:racing:france");
System.out.println(res8); // >>> 3
long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
System.out.println(res9); // >>> 3
Set<String> res10 = jedis.smembers("bikes:racing:france");
System.out.println(res10); // >>> [bike:1, bike:2, bike:3]
boolean res11 = jedis.sismember("bikes:racing:france", "bike:1");
System.out.println(res11); // >>> true
List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4");
System.out.println(res12); // >>> [true,true,false]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res13); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4");
Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res14); // >>> [bike:1]
Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4]
Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res16); // >>> []
Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france");
System.out.println(res17); // >>> [bike:4]
Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res18); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
long res19 = jedis.srem("bikes:racing:france", "bike:1");
System.out.println(res18); // >>> 1
String res20 = jedis.spop("bikes:racing:france");
System.out.println(res20); // >>> bike:3
Set<String> res21 = jedis.smembers("bikes:racing:france");
System.out.println(res21); // >>> [bike:2, bike:4, bike:5]
String res22 = jedis.srandmember("bikes:racing:france");
System.out.println(res22); // >>> bike:4
jedis.close();
}
}
import redis
r = redis.Redis(decode_responses=True)
res1 = r.sadd("bikes:racing:france", "bike:1")
print(res1) # >>> 1
res2 = r.sadd("bikes:racing:france", "bike:1")
print(res2) # >>> 0
res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3")
print(res3) # >>> 2
res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4")
print(res4) # >>> 2
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res5 = r.sismember("bikes:racing:usa", "bike:1")
print(res5) # >>> 1
res6 = r.sismember("bikes:racing:usa", "bike:2")
print(res6) # >>> 1
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res7 = r.sinter("bikes:racing:france", "bikes:racing:usa")
print(res7) # >>> {'bike:1'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
res8 = r.scard("bikes:racing:france")
print(res8) # >>> 3
res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
print(res9) # >>> 3
res10 = r.smembers("bikes:racing:france")
print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'}
res11 = r.sismember("bikes:racing:france", "bike:1")
print(res11) # >>> 1
res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4")
print(res12) # >>> [1, 1, 0]
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res13) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4")
res13 = r.sinter(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res13) # >>> {'bike:1'}
res14 = r.sunion(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'}
res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy")
print(res15) # >>> set()
res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france")
print(res16) # >>> {'bike:4'}
res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res17) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
res18 = r.srem("bikes:racing:france", "bike:1")
print(res18) # >>> 1
res19 = r.spop("bikes:racing:france")
print(res19) # >>> bike:3
res20 = r.smembers("bikes:racing:france")
print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'}
res21 = r.srandmember("bikes:racing:france")
print(res21) # >>> bike:4
You'll note that the SDIFF
command returns an empty array when the
difference between all sets is empty. You'll also note that the order of sets
passed to SDIFF
matters, since the difference is not commutative.
When you want to remove items from a set, you can use the SREM
command to
remove one or more items from a set, or you can use the SPOP
command to
remove a random item from a set. You can also return a random item from a
set without removing it using the SRANDMEMBER
command:
> SADD bikes:racing:france bike:1 bike:2 bike:3 bike:4 bike:5
(integer) 5
> SREM bikes:racing:france bike:1
(integer) 1
> SPOP bikes:racing:france
"bike:3"
> SMEMBERS bikes:racing:france
1) "bike:2"
2) "bike:4"
3) "bike:5"
> SRANDMEMBER bikes:racing:france
"bike:2"
package io.redis.examples;
import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
public class SetsExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.sadd("bikes:racing:france", "bike:1");
System.out.println(res2); // >>> 0
long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3");
System.out.println(res3); // >>> 2
long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
System.out.println(res4); // >>> 2
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1");
System.out.println(res5); // >>> true
boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2");
System.out.println(res6); // >>> false
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa");
System.out.println(res7); // >>> [bike:1]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
long res8 = jedis.scard("bikes:racing:france");
System.out.println(res8); // >>> 3
long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
System.out.println(res9); // >>> 3
Set<String> res10 = jedis.smembers("bikes:racing:france");
System.out.println(res10); // >>> [bike:1, bike:2, bike:3]
boolean res11 = jedis.sismember("bikes:racing:france", "bike:1");
System.out.println(res11); // >>> true
List<Boolean> res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4");
System.out.println(res12); // >>> [true,true,false]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
Set<String> res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res13); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3");
jedis.sadd("bikes:racing:usa", "bike:1", "bike:4");
jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4");
Set<String> res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res14); // >>> [bike:1]
Set<String> res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4]
Set<String> res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy");
System.out.println(res16); // >>> []
Set<String> res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france");
System.out.println(res17); // >>> [bike:4]
Set<String> res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa");
System.out.println(res18); // >>> [bike:2, bike:3]
jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
long res19 = jedis.srem("bikes:racing:france", "bike:1");
System.out.println(res18); // >>> 1
String res20 = jedis.spop("bikes:racing:france");
System.out.println(res20); // >>> bike:3
Set<String> res21 = jedis.smembers("bikes:racing:france");
System.out.println(res21); // >>> [bike:2, bike:4, bike:5]
String res22 = jedis.srandmember("bikes:racing:france");
System.out.println(res22); // >>> bike:4
jedis.close();
}
}
import redis
r = redis.Redis(decode_responses=True)
res1 = r.sadd("bikes:racing:france", "bike:1")
print(res1) # >>> 1
res2 = r.sadd("bikes:racing:france", "bike:1")
print(res2) # >>> 0
res3 = r.sadd("bikes:racing:france", "bike:2", "bike:3")
print(res3) # >>> 2
res4 = r.sadd("bikes:racing:usa", "bike:1", "bike:4")
print(res4) # >>> 2
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res5 = r.sismember("bikes:racing:usa", "bike:1")
print(res5) # >>> 1
res6 = r.sismember("bikes:racing:usa", "bike:2")
print(res6) # >>> 1
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res7 = r.sinter("bikes:racing:france", "bikes:racing:usa")
print(res7) # >>> {'bike:1'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
res8 = r.scard("bikes:racing:france")
print(res8) # >>> 3
res9 = r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
print(res9) # >>> 3
res10 = r.smembers("bikes:racing:france")
print(res10) # >>> {'bike:1', 'bike:2', 'bike:3'}
res11 = r.sismember("bikes:racing:france", "bike:1")
print(res11) # >>> 1
res12 = r.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4")
print(res12) # >>> [1, 1, 0]
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
res13 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res13) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3")
r.sadd("bikes:racing:usa", "bike:1", "bike:4")
r.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4")
res13 = r.sinter(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res13) # >>> {'bike:1'}
res14 = r.sunion(
"bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"
)
print(res14) # >>> {'bike:1', 'bike:2', 'bike:3', 'bike:4'}
res15 = r.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy")
print(res15) # >>> set()
res16 = r.sdiff("bikes:racing:usa", "bikes:racing:france")
print(res16) # >>> {'bike:4'}
res17 = r.sdiff("bikes:racing:france", "bikes:racing:usa")
print(res17) # >>> {'bike:2', 'bike:3'}
r.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
res18 = r.srem("bikes:racing:france", "bike:1")
print(res18) # >>> 1
res19 = r.spop("bikes:racing:france")
print(res19) # >>> bike:3
res20 = r.smembers("bikes:racing:france")
print(res20) # >>> {'bike:2', 'bike:4', 'bike:5'}
res21 = r.srandmember("bikes:racing:france")
print(res21) # >>> bike:4
Limits
The max size of a Redis set is 2^32 - 1 (4,294,967,295) members.
Performance
Most set operations, including adding, removing, and checking whether an item is a set member, are O(1).
This means that they're highly efficient.
However, for large sets with hundreds of thousands of members or more, you should exercise caution when running the SMEMBERS
command.
This command is O(n) and returns the entire set in a single response.
As an alternative, consider the SSCAN
, which lets you retrieve all members of a set iteratively.
Alternatives
Sets membership checks on large datasets (or on streaming data) can use a lot of memory. If you're concerned about memory usage and don't need perfect precision, consider a Bloom filter or Cuckoo filter as an alternative to a set.
Redis sets are frequently used as a kind of index. If you need to index and query your data, consider the JSON data type and the Search and query features.
Learn more
- Redis Sets Explained and Redis Sets Elaborated are two short but thorough video explainers covering Redis sets.
- Redis University's RU101 explores Redis sets in detail.