Redis 集合
Redis 集合简介
Redis 集合是唯一字符串(成员)的无序集合。您可以使用 Redis 集合来有效地
- 跟踪唯一项目(例如,跟踪访问给定博客文章的所有唯一 IP 地址)。
- 表示关系(例如,具有给定角色的所有用户的集合)。
- 执行常见的集合操作,例如交集、并集和差集。
基本命令
SADD
将新成员添加到集合中。SREM
从集合中删除指定的成员。SISMEMBER
测试字符串是否属于集合。SINTER
返回两个或多个集合共有的成员集(即交集)。SCARD
返回集合的大小(也称为基数)。
查看完整的集合命令列表。
示例
-
存储在法国和美国比赛的自行车集合。请注意,如果您添加已经存在的成员,它将被忽略。
> 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
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。""" Code samples for Set doc pages: https://redis.ac.cn/docs/latest/develop/data-types/sets/ """ 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) # >>> 0 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) # >>> [] 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
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res1) // >>> 1 const res2 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res2) // >>> 0 const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3']) console.log(res3) // >>> 2 const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) console.log(res4) // >>> 2 await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1') console.log(res5) // >>> true const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2') console.log(res6) // >>> false await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa') console.log(res7) // >>> {'bike:1'} await client.del('bikes:racing:france') await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) const res8 = await client.sCard('bikes:racing:france') console.log(res8) // >>> 3 const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) console.log(res9) // >>> 3 const res10 = await client.sMembers('bikes:racing:france') console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3'] const res11 = await client.sIsMember('bikes:racing:france', 'bike:1') console.log(res11) // >>> true const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4']) console.log(res12) // >>> [true, true, false] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res13) // >>> [ 'bike:2', 'bike:3' ] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4']) const res14 = await client.sInter( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res14) // >>> ['bike:1'] const res15 = await client.sUnion( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4'] const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']) console.log(res16) // >>> [] const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france']) console.log(res17) // >>> ['bike:4'] const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res18) // >>> ['bike:2', 'bike:3'] debugger; await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']) const res19 = await client.sRem('bikes:racing:france', 'bike:1') console.log(res19) // >>> 1 const res20 = await client.sPop('bikes:racing:france') console.log(res20) // >>> bike:3 or other random value const res21 = await client.sMembers('bikes:racing:france') console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result const res22 = await client.sRandMember('bikes:racing:france') console.log(res22) // >>> bike:4 or other random value
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(); } }
using System.Data.Common; using NRedisStack.Tests; using StackExchange.Redis; public class SetsExample { [SkipIfRedis(Is.OSSCluster)] public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res1); // >>> 1 long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res2); // >>> 0 long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" }); Console.WriteLine(res3); // >>> 2 long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" }); Console.WriteLine(res4); // >>> 2 // Tests for 'sadd' step. bool res5 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res5); // >>> True bool res6 = db.SetContains("bikes:racing:usa", "bike:2"); Console.WriteLine(res6); // >>> False // Tests for 'sismember' step. long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res9)); // >>> bike:1 // Tests for 'sinter' step. long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res11 = db.SetLength("bikes:racing:france"); Console.WriteLine(res11); // >>> 3 // Tests for 'scard' step. long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); RedisValue[] res13 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1 // Tests for 'sadd_smembers' step. bool res14 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res14); // >>> true bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" }); Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False // Tests for 'smismember' step. long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3 // Tests for 'sdiff' step. long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" }); RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res22)); // >>> bike:1 RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4 RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res24)); // >>> <empty set> RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" }); Console.WriteLine(string.Join(", ", res25)); // >>> bike:4 RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3 // Tests for 'multisets' step. long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); bool res28 = db.SetRemove("bikes:racing:france", "bike:1"); Console.WriteLine(res28); // >>> True RedisValue res29 = db.SetPop("bikes:racing:france"); Console.WriteLine(res29); // >>> bike:3 RedisValue[] res30 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5 RedisValue res31 = db.SetRandomMember("bikes:racing:france"); Console.WriteLine(res31); // >>> bike:4 // Tests for 'srem' step. } }
-
检查 bike:1 或 bike:2 是否在美国比赛。
> SISMEMBER bikes:racing:usa bike:1 (integer) 1 > SISMEMBER bikes:racing:usa bike:2 (integer) 0
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。""" Code samples for Set doc pages: https://redis.ac.cn/docs/latest/develop/data-types/sets/ """ 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) # >>> 0 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) # >>> [] 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
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res1) // >>> 1 const res2 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res2) // >>> 0 const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3']) console.log(res3) // >>> 2 const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) console.log(res4) // >>> 2 await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1') console.log(res5) // >>> true const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2') console.log(res6) // >>> false await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa') console.log(res7) // >>> {'bike:1'} await client.del('bikes:racing:france') await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) const res8 = await client.sCard('bikes:racing:france') console.log(res8) // >>> 3 const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) console.log(res9) // >>> 3 const res10 = await client.sMembers('bikes:racing:france') console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3'] const res11 = await client.sIsMember('bikes:racing:france', 'bike:1') console.log(res11) // >>> true const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4']) console.log(res12) // >>> [true, true, false] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res13) // >>> [ 'bike:2', 'bike:3' ] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4']) const res14 = await client.sInter( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res14) // >>> ['bike:1'] const res15 = await client.sUnion( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4'] const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']) console.log(res16) // >>> [] const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france']) console.log(res17) // >>> ['bike:4'] const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res18) // >>> ['bike:2', 'bike:3'] debugger; await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']) const res19 = await client.sRem('bikes:racing:france', 'bike:1') console.log(res19) // >>> 1 const res20 = await client.sPop('bikes:racing:france') console.log(res20) // >>> bike:3 or other random value const res21 = await client.sMembers('bikes:racing:france') console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result const res22 = await client.sRandMember('bikes:racing:france') console.log(res22) // >>> bike:4 or other random value
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(); } }
using System.Data.Common; using NRedisStack.Tests; using StackExchange.Redis; public class SetsExample { [SkipIfRedis(Is.OSSCluster)] public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res1); // >>> 1 long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res2); // >>> 0 long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" }); Console.WriteLine(res3); // >>> 2 long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" }); Console.WriteLine(res4); // >>> 2 // Tests for 'sadd' step. bool res5 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res5); // >>> True bool res6 = db.SetContains("bikes:racing:usa", "bike:2"); Console.WriteLine(res6); // >>> False // Tests for 'sismember' step. long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res9)); // >>> bike:1 // Tests for 'sinter' step. long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res11 = db.SetLength("bikes:racing:france"); Console.WriteLine(res11); // >>> 3 // Tests for 'scard' step. long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); RedisValue[] res13 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1 // Tests for 'sadd_smembers' step. bool res14 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res14); // >>> true bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" }); Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False // Tests for 'smismember' step. long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3 // Tests for 'sdiff' step. long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" }); RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res22)); // >>> bike:1 RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4 RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res24)); // >>> <empty set> RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" }); Console.WriteLine(string.Join(", ", res25)); // >>> bike:4 RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3 // Tests for 'multisets' step. long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); bool res28 = db.SetRemove("bikes:racing:france", "bike:1"); Console.WriteLine(res28); // >>> True RedisValue res29 = db.SetPop("bikes:racing:france"); Console.WriteLine(res29); // >>> bike:3 RedisValue[] res30 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5 RedisValue res31 = db.SetRandomMember("bikes:racing:france"); Console.WriteLine(res31); // >>> bike:4 // Tests for 'srem' step. } }
-
哪些自行车在两场比赛中都参加了比赛?
> SINTER bikes:racing:france bikes:racing:usa 1) "bike:1"
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。""" Code samples for Set doc pages: https://redis.ac.cn/docs/latest/develop/data-types/sets/ """ 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) # >>> 0 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) # >>> [] 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
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res1) // >>> 1 const res2 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res2) // >>> 0 const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3']) console.log(res3) // >>> 2 const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) console.log(res4) // >>> 2 await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1') console.log(res5) // >>> true const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2') console.log(res6) // >>> false await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa') console.log(res7) // >>> {'bike:1'} await client.del('bikes:racing:france') await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) const res8 = await client.sCard('bikes:racing:france') console.log(res8) // >>> 3 const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) console.log(res9) // >>> 3 const res10 = await client.sMembers('bikes:racing:france') console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3'] const res11 = await client.sIsMember('bikes:racing:france', 'bike:1') console.log(res11) // >>> true const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4']) console.log(res12) // >>> [true, true, false] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res13) // >>> [ 'bike:2', 'bike:3' ] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4']) const res14 = await client.sInter( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res14) // >>> ['bike:1'] const res15 = await client.sUnion( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4'] const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']) console.log(res16) // >>> [] const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france']) console.log(res17) // >>> ['bike:4'] const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res18) // >>> ['bike:2', 'bike:3'] debugger; await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']) const res19 = await client.sRem('bikes:racing:france', 'bike:1') console.log(res19) // >>> 1 const res20 = await client.sPop('bikes:racing:france') console.log(res20) // >>> bike:3 or other random value const res21 = await client.sMembers('bikes:racing:france') console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result const res22 = await client.sRandMember('bikes:racing:france') console.log(res22) // >>> bike:4 or other random value
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(); } }
using System.Data.Common; using NRedisStack.Tests; using StackExchange.Redis; public class SetsExample { [SkipIfRedis(Is.OSSCluster)] public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res1); // >>> 1 long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res2); // >>> 0 long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" }); Console.WriteLine(res3); // >>> 2 long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" }); Console.WriteLine(res4); // >>> 2 // Tests for 'sadd' step. bool res5 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res5); // >>> True bool res6 = db.SetContains("bikes:racing:usa", "bike:2"); Console.WriteLine(res6); // >>> False // Tests for 'sismember' step. long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res9)); // >>> bike:1 // Tests for 'sinter' step. long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res11 = db.SetLength("bikes:racing:france"); Console.WriteLine(res11); // >>> 3 // Tests for 'scard' step. long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); RedisValue[] res13 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1 // Tests for 'sadd_smembers' step. bool res14 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res14); // >>> true bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" }); Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False // Tests for 'smismember' step. long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3 // Tests for 'sdiff' step. long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" }); RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res22)); // >>> bike:1 RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4 RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res24)); // >>> <empty set> RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" }); Console.WriteLine(string.Join(", ", res25)); // >>> bike:4 RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3 // Tests for 'multisets' step. long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); bool res28 = db.SetRemove("bikes:racing:france", "bike:1"); Console.WriteLine(res28); // >>> True RedisValue res29 = db.SetPop("bikes:racing:france"); Console.WriteLine(res29); // >>> bike:3 RedisValue[] res30 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5 RedisValue res31 = db.SetRandomMember("bikes:racing:france"); Console.WriteLine(res31); // >>> bike:4 // Tests for 'srem' step. } }
-
法国有多少辆自行车参加比赛?
> SCARD bikes:racing:france (integer) 3
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。""" Code samples for Set doc pages: https://redis.ac.cn/docs/latest/develop/data-types/sets/ """ 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) # >>> 0 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) # >>> [] 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
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res1) // >>> 1 const res2 = await client.sAdd('bikes:racing:france', 'bike:1') console.log(res2) // >>> 0 const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3']) console.log(res3) // >>> 2 const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) console.log(res4) // >>> 2 await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1') console.log(res5) // >>> true const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2') console.log(res6) // >>> false await client.del('bikes:racing:france') await client.del('bikes:racing:usa') await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa') console.log(res7) // >>> {'bike:1'} await client.del('bikes:racing:france') await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) const res8 = await client.sCard('bikes:racing:france') console.log(res8) // >>> 3 const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) console.log(res9) // >>> 3 const res10 = await client.sMembers('bikes:racing:france') console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3'] const res11 = await client.sIsMember('bikes:racing:france', 'bike:1') console.log(res11) // >>> true const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4']) console.log(res12) // >>> [true, true, false] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res13) // >>> [ 'bike:2', 'bike:3' ] await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4']) const res14 = await client.sInter( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res14) // >>> ['bike:1'] const res15 = await client.sUnion( ['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] ) console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4'] const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']) console.log(res16) // >>> [] const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france']) console.log(res17) // >>> ['bike:4'] const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) console.log(res18) // >>> ['bike:2', 'bike:3'] debugger; await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']) const res19 = await client.sRem('bikes:racing:france', 'bike:1') console.log(res19) // >>> 1 const res20 = await client.sPop('bikes:racing:france') console.log(res20) // >>> bike:3 or other random value const res21 = await client.sMembers('bikes:racing:france') console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result const res22 = await client.sRandMember('bikes:racing:france') console.log(res22) // >>> bike:4 or other random value
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(); } }
using System.Data.Common; using NRedisStack.Tests; using StackExchange.Redis; public class SetsExample { [SkipIfRedis(Is.OSSCluster)] public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res1); // >>> 1 long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" }); Console.WriteLine(res2); // >>> 0 long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" }); Console.WriteLine(res3); // >>> 2 long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" }); Console.WriteLine(res4); // >>> 2 // Tests for 'sadd' step. bool res5 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res5); // >>> True bool res6 = db.SetContains("bikes:racing:usa", "bike:2"); Console.WriteLine(res6); // >>> False // Tests for 'sismember' step. long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res9)); // >>> bike:1 // Tests for 'sinter' step. long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res11 = db.SetLength("bikes:racing:france"); Console.WriteLine(res11); // >>> 3 // Tests for 'scard' step. long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); RedisValue[] res13 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1 // Tests for 'sadd_smembers' step. bool res14 = db.SetContains("bikes:racing:france", "bike:1"); Console.WriteLine(res14); // >>> true bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" }); Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False // Tests for 'smismember' step. long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3 // Tests for 'sdiff' step. long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" }); long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" }); RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res22)); // >>> bike:1 RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4 RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" }); Console.WriteLine(string.Join(", ", res24)); // >>> <empty set> RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" }); Console.WriteLine(string.Join(", ", res25)); // >>> bike:4 RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" }); Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3 // Tests for 'multisets' step. long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); bool res28 = db.SetRemove("bikes:racing:france", "bike:1"); Console.WriteLine(res28); // >>> True RedisValue res29 = db.SetPop("bikes:racing:france"); Console.WriteLine(res29); // >>> bike:3 RedisValue[] res30 = db.SetMembers("bikes:racing:france"); Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5 RedisValue res31 = db.SetRandomMember("bikes:racing:france"); Console.WriteLine(res31); // >>> bike:4 // Tests for 'srem' step. } }
教程
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
"""
Code samples for Set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sets/
"""
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) # >>> 0
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) # >>> []
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
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res1) // >>> 1
const res2 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res2) // >>> 0
const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3'])
console.log(res3) // >>> 2
const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
console.log(res4) // >>> 2
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1')
console.log(res5) // >>> true
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> false
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa')
console.log(res7) // >>> {'bike:1'}
await client.del('bikes:racing:france')
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
const res8 = await client.sCard('bikes:racing:france')
console.log(res8) // >>> 3
const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
console.log(res9) // >>> 3
const res10 = await client.sMembers('bikes:racing:france')
console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3']
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1')
console.log(res11) // >>> true
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [true, true, false]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res13) // >>> [ 'bike:2', 'bike:3' ]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
const res14 = await client.sInter(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res14) // >>> ['bike:1']
const res15 = await client.sUnion(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4']
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'])
console.log(res16) // >>> []
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france'])
console.log(res17) // >>> ['bike:4']
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res18) // >>> ['bike:2', 'bike:3']
debugger;
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'])
const res19 = await client.sRem('bikes:racing:france', 'bike:1')
console.log(res19) // >>> 1
const res20 = await client.sPop('bikes:racing:france')
console.log(res20) // >>> bike:3 or other random value
const res21 = await client.sMembers('bikes:racing:france')
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result
const res22 = await client.sRandMember('bikes:racing:france')
console.log(res22) // >>> bike:4 or other random value
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();
}
}
using System.Data.Common;
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetsExample
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res1); // >>> 1
long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res2); // >>> 0
long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" });
Console.WriteLine(res3); // >>> 2
long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" });
Console.WriteLine(res4); // >>> 2
// Tests for 'sadd' step.
bool res5 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res5); // >>> True
bool res6 = db.SetContains("bikes:racing:usa", "bike:2");
Console.WriteLine(res6); // >>> False
// Tests for 'sismember' step.
long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res9)); // >>> bike:1
// Tests for 'sinter' step.
long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res11 = db.SetLength("bikes:racing:france");
Console.WriteLine(res11); // >>> 3
// Tests for 'scard' step.
long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
RedisValue[] res13 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1
// Tests for 'sadd_smembers' step.
bool res14 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res14); // >>> true
bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" });
Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False
// Tests for 'smismember' step.
long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3
// Tests for 'sdiff' step.
long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" });
RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res22)); // >>> bike:1
RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4
RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res24)); // >>> <empty set>
RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" });
Console.WriteLine(string.Join(", ", res25)); // >>> bike:4
RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3
// Tests for 'multisets' step.
long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
bool res28 = db.SetRemove("bikes:racing:france", "bike:1");
Console.WriteLine(res28); // >>> True
RedisValue res29 = db.SetPop("bikes:racing:france");
Console.WriteLine(res29); // >>> bike:3
RedisValue[] res30 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5
RedisValue res31 = db.SetRandomMember("bikes:racing:france");
Console.WriteLine(res31); // >>> bike:4
// Tests for 'srem' step.
}
}
我已经在我的集合中添加了三个元素,并告诉 Redis 返回所有元素。集合没有顺序保证。Redis 可以自由地在每次调用时以任何顺序返回元素。
Redis 有命令可以测试集合成员资格。这些命令可以用于单个项目和多个项目。
> 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
"""
Code samples for Set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sets/
"""
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) # >>> 0
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) # >>> []
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
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res1) // >>> 1
const res2 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res2) // >>> 0
const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3'])
console.log(res3) // >>> 2
const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
console.log(res4) // >>> 2
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1')
console.log(res5) // >>> true
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> false
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa')
console.log(res7) // >>> {'bike:1'}
await client.del('bikes:racing:france')
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
const res8 = await client.sCard('bikes:racing:france')
console.log(res8) // >>> 3
const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
console.log(res9) // >>> 3
const res10 = await client.sMembers('bikes:racing:france')
console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3']
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1')
console.log(res11) // >>> true
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [true, true, false]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res13) // >>> [ 'bike:2', 'bike:3' ]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
const res14 = await client.sInter(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res14) // >>> ['bike:1']
const res15 = await client.sUnion(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4']
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'])
console.log(res16) // >>> []
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france'])
console.log(res17) // >>> ['bike:4']
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res18) // >>> ['bike:2', 'bike:3']
debugger;
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'])
const res19 = await client.sRem('bikes:racing:france', 'bike:1')
console.log(res19) // >>> 1
const res20 = await client.sPop('bikes:racing:france')
console.log(res20) // >>> bike:3 or other random value
const res21 = await client.sMembers('bikes:racing:france')
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result
const res22 = await client.sRandMember('bikes:racing:france')
console.log(res22) // >>> bike:4 or other random value
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();
}
}
using System.Data.Common;
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetsExample
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res1); // >>> 1
long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res2); // >>> 0
long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" });
Console.WriteLine(res3); // >>> 2
long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" });
Console.WriteLine(res4); // >>> 2
// Tests for 'sadd' step.
bool res5 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res5); // >>> True
bool res6 = db.SetContains("bikes:racing:usa", "bike:2");
Console.WriteLine(res6); // >>> False
// Tests for 'sismember' step.
long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res9)); // >>> bike:1
// Tests for 'sinter' step.
long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res11 = db.SetLength("bikes:racing:france");
Console.WriteLine(res11); // >>> 3
// Tests for 'scard' step.
long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
RedisValue[] res13 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1
// Tests for 'sadd_smembers' step.
bool res14 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res14); // >>> true
bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" });
Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False
// Tests for 'smismember' step.
long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3
// Tests for 'sdiff' step.
long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" });
RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res22)); // >>> bike:1
RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4
RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res24)); // >>> <empty set>
RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" });
Console.WriteLine(string.Join(", ", res25)); // >>> bike:4
RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3
// Tests for 'multisets' step.
long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
bool res28 = db.SetRemove("bikes:racing:france", "bike:1");
Console.WriteLine(res28); // >>> True
RedisValue res29 = db.SetPop("bikes:racing:france");
Console.WriteLine(res29); // >>> bike:3
RedisValue[] res30 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5
RedisValue res31 = db.SetRandomMember("bikes:racing:france");
Console.WriteLine(res31); // >>> bike:4
// Tests for 'srem' step.
}
}
我们还可以找到两个集合之间的差异。例如,我们可能想知道哪些自行车在法国比赛,但在美国没有比赛。
> SADD bikes:racing:usa bike:1 bike:4
(integer) 2
> SDIFF bikes:racing:france bikes:racing:usa
1) "bike:3"
2) "bike:2"
"""
Code samples for Set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sets/
"""
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) # >>> 0
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) # >>> []
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
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res1) // >>> 1
const res2 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res2) // >>> 0
const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3'])
console.log(res3) // >>> 2
const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
console.log(res4) // >>> 2
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1')
console.log(res5) // >>> true
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> false
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa')
console.log(res7) // >>> {'bike:1'}
await client.del('bikes:racing:france')
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
const res8 = await client.sCard('bikes:racing:france')
console.log(res8) // >>> 3
const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
console.log(res9) // >>> 3
const res10 = await client.sMembers('bikes:racing:france')
console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3']
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1')
console.log(res11) // >>> true
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [true, true, false]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res13) // >>> [ 'bike:2', 'bike:3' ]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
const res14 = await client.sInter(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res14) // >>> ['bike:1']
const res15 = await client.sUnion(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4']
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'])
console.log(res16) // >>> []
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france'])
console.log(res17) // >>> ['bike:4']
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res18) // >>> ['bike:2', 'bike:3']
debugger;
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'])
const res19 = await client.sRem('bikes:racing:france', 'bike:1')
console.log(res19) // >>> 1
const res20 = await client.sPop('bikes:racing:france')
console.log(res20) // >>> bike:3 or other random value
const res21 = await client.sMembers('bikes:racing:france')
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result
const res22 = await client.sRandMember('bikes:racing:france')
console.log(res22) // >>> bike:4 or other random value
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();
}
}
using System.Data.Common;
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetsExample
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res1); // >>> 1
long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res2); // >>> 0
long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" });
Console.WriteLine(res3); // >>> 2
long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" });
Console.WriteLine(res4); // >>> 2
// Tests for 'sadd' step.
bool res5 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res5); // >>> True
bool res6 = db.SetContains("bikes:racing:usa", "bike:2");
Console.WriteLine(res6); // >>> False
// Tests for 'sismember' step.
long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res9)); // >>> bike:1
// Tests for 'sinter' step.
long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res11 = db.SetLength("bikes:racing:france");
Console.WriteLine(res11); // >>> 3
// Tests for 'scard' step.
long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
RedisValue[] res13 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1
// Tests for 'sadd_smembers' step.
bool res14 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res14); // >>> true
bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" });
Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False
// Tests for 'smismember' step.
long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3
// Tests for 'sdiff' step.
long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" });
RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res22)); // >>> bike:1
RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4
RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res24)); // >>> <empty set>
RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" });
Console.WriteLine(string.Join(", ", res25)); // >>> bike:4
RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3
// Tests for 'multisets' step.
long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
bool res28 = db.SetRemove("bikes:racing:france", "bike:1");
Console.WriteLine(res28); // >>> True
RedisValue res29 = db.SetPop("bikes:racing:france");
Console.WriteLine(res29); // >>> bike:3
RedisValue[] res30 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5
RedisValue res31 = db.SetRandomMember("bikes:racing:france");
Console.WriteLine(res31); // >>> bike:4
// Tests for 'srem' step.
}
}
还有其他非平凡的操作,使用正确的 Redis 命令仍然很容易实现。例如,我们可能想要一个在法国、美国和其他一些比赛中比赛的所有自行车的列表。我们可以使用 SINTER
命令来完成此操作,该命令执行不同集合之间的交集。除了交集,您还可以执行并集、差集等等。例如,如果我们添加第三场比赛,我们可以看到其中一些命令在运行。
> 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"
"""
Code samples for Set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sets/
"""
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) # >>> 0
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) # >>> []
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
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res1) // >>> 1
const res2 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res2) // >>> 0
const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3'])
console.log(res3) // >>> 2
const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
console.log(res4) // >>> 2
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1')
console.log(res5) // >>> true
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> false
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa')
console.log(res7) // >>> {'bike:1'}
await client.del('bikes:racing:france')
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
const res8 = await client.sCard('bikes:racing:france')
console.log(res8) // >>> 3
const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
console.log(res9) // >>> 3
const res10 = await client.sMembers('bikes:racing:france')
console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3']
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1')
console.log(res11) // >>> true
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [true, true, false]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res13) // >>> [ 'bike:2', 'bike:3' ]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
const res14 = await client.sInter(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res14) // >>> ['bike:1']
const res15 = await client.sUnion(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4']
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'])
console.log(res16) // >>> []
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france'])
console.log(res17) // >>> ['bike:4']
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res18) // >>> ['bike:2', 'bike:3']
debugger;
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'])
const res19 = await client.sRem('bikes:racing:france', 'bike:1')
console.log(res19) // >>> 1
const res20 = await client.sPop('bikes:racing:france')
console.log(res20) // >>> bike:3 or other random value
const res21 = await client.sMembers('bikes:racing:france')
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result
const res22 = await client.sRandMember('bikes:racing:france')
console.log(res22) // >>> bike:4 or other random value
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();
}
}
using System.Data.Common;
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetsExample
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res1); // >>> 1
long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res2); // >>> 0
long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" });
Console.WriteLine(res3); // >>> 2
long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" });
Console.WriteLine(res4); // >>> 2
// Tests for 'sadd' step.
bool res5 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res5); // >>> True
bool res6 = db.SetContains("bikes:racing:usa", "bike:2");
Console.WriteLine(res6); // >>> False
// Tests for 'sismember' step.
long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res9)); // >>> bike:1
// Tests for 'sinter' step.
long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res11 = db.SetLength("bikes:racing:france");
Console.WriteLine(res11); // >>> 3
// Tests for 'scard' step.
long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
RedisValue[] res13 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1
// Tests for 'sadd_smembers' step.
bool res14 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res14); // >>> true
bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" });
Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False
// Tests for 'smismember' step.
long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3
// Tests for 'sdiff' step.
long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" });
RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res22)); // >>> bike:1
RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4
RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res24)); // >>> <empty set>
RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" });
Console.WriteLine(string.Join(", ", res25)); // >>> bike:4
RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3
// Tests for 'multisets' step.
long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
bool res28 = db.SetRemove("bikes:racing:france", "bike:1");
Console.WriteLine(res28); // >>> True
RedisValue res29 = db.SetPop("bikes:racing:france");
Console.WriteLine(res29); // >>> bike:3
RedisValue[] res30 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5
RedisValue res31 = db.SetRandomMember("bikes:racing:france");
Console.WriteLine(res31); // >>> bike:4
// Tests for 'srem' step.
}
}
您会注意到,当所有集合之间的差为空时,SDIFF
命令返回一个空数组。您还会注意到,传递给 SDIFF
的集合顺序很重要,因为差集不是可交换的。
当您要从集合中删除项目时,可以使用 SREM
命令从集合中删除一个或多个项目,或者可以使用 SPOP
命令从集合中删除一个随机项目。您还可以使用 SRANDMEMBER
命令从集合中返回一个随机项目,而不会删除它。
> 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"
"""
Code samples for Set doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/sets/
"""
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) # >>> 0
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) # >>> []
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
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res1) // >>> 1
const res2 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res2) // >>> 0
const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3'])
console.log(res3) // >>> 2
const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
console.log(res4) // >>> 2
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1')
console.log(res5) // >>> true
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> false
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa')
console.log(res7) // >>> {'bike:1'}
await client.del('bikes:racing:france')
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
const res8 = await client.sCard('bikes:racing:france')
console.log(res8) // >>> 3
const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
console.log(res9) // >>> 3
const res10 = await client.sMembers('bikes:racing:france')
console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3']
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1')
console.log(res11) // >>> true
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [true, true, false]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res13) // >>> [ 'bike:2', 'bike:3' ]
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
const res14 = await client.sInter(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res14) // >>> ['bike:1']
const res15 = await client.sUnion(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4']
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'])
console.log(res16) // >>> []
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france'])
console.log(res17) // >>> ['bike:4']
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res18) // >>> ['bike:2', 'bike:3']
debugger;
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'])
const res19 = await client.sRem('bikes:racing:france', 'bike:1')
console.log(res19) // >>> 1
const res20 = await client.sPop('bikes:racing:france')
console.log(res20) // >>> bike:3 or other random value
const res21 = await client.sMembers('bikes:racing:france')
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result
const res22 = await client.sRandMember('bikes:racing:france')
console.log(res22) // >>> bike:4 or other random value
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();
}
}
using System.Data.Common;
using NRedisStack.Tests;
using StackExchange.Redis;
public class SetsExample
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res1); // >>> 1
long res2 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1" });
Console.WriteLine(res2); // >>> 0
long res3 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3" });
Console.WriteLine(res3); // >>> 2
long res4 = db.SetAdd("bikes:racing:usa", new RedisValue[] { "bike:1", "bike:4" });
Console.WriteLine(res4); // >>> 2
// Tests for 'sadd' step.
bool res5 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res5); // >>> True
bool res6 = db.SetContains("bikes:racing:usa", "bike:2");
Console.WriteLine(res6); // >>> False
// Tests for 'sismember' step.
long res7 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res8 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res9 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res9)); // >>> bike:1
// Tests for 'sinter' step.
long res10 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res11 = db.SetLength("bikes:racing:france");
Console.WriteLine(res11); // >>> 3
// Tests for 'scard' step.
long res12 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
RedisValue[] res13 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res13)); // >>> bike:3, bike:2, bike:1
// Tests for 'sadd_smembers' step.
bool res14 = db.SetContains("bikes:racing:france", "bike:1");
Console.WriteLine(res14); // >>> true
bool[] res15 = db.SetContains("bikes:racing:france", new RedisValue[] { "bike:2", "bike:3", "bike:4" });
Console.WriteLine(string.Join(", ", res15)); // >>> True, True, False
// Tests for 'smismember' step.
long res16 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res17 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
RedisValue[] res18 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res18)); // >>> bike:2, bike:3
// Tests for 'sdiff' step.
long res19 = db.SetAdd("{bikes:racing}:france", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
long res20 = db.SetAdd("{bikes:racing}:usa", new RedisValue[] { "bike:1", "bike:4" });
long res21 = db.SetAdd("{bikes:racing}:italy", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4" });
RedisValue[] res22 = db.SetCombine(SetOperation.Intersect, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res22)); // >>> bike:1
RedisValue[] res23 = db.SetCombine(SetOperation.Union, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res23)); // >>> bike:1, bike:2, bike:3, bike:4
RedisValue[] res24 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa", "{bikes:racing}:italy" });
Console.WriteLine(string.Join(", ", res24)); // >>> <empty set>
RedisValue[] res25 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:usa", "{bikes:racing}:france" });
Console.WriteLine(string.Join(", ", res25)); // >>> bike:4
RedisValue[] res26 = db.SetCombine(SetOperation.Difference, new RedisKey[] { "{bikes:racing}:france", "{bikes:racing}:usa" });
Console.WriteLine(string.Join(", ", res26)); // >>> bike:2, bike:3
// Tests for 'multisets' step.
long res27 = db.SetAdd("bikes:racing:france", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
bool res28 = db.SetRemove("bikes:racing:france", "bike:1");
Console.WriteLine(res28); // >>> True
RedisValue res29 = db.SetPop("bikes:racing:france");
Console.WriteLine(res29); // >>> bike:3
RedisValue[] res30 = db.SetMembers("bikes:racing:france");
Console.WriteLine(string.Join(", ", res30)); // >>> bike:2, bike:4, bike:5
RedisValue res31 = db.SetRandomMember("bikes:racing:france");
Console.WriteLine(res31); // >>> bike:4
// Tests for 'srem' step.
}
}
限制
Redis 集合的最大大小为 2^32 - 1 (4,294,967,295) 个成员。
性能
大多数集合操作,包括添加、删除和检查项目是否为集合成员,都是 O(1)。这意味着它们非常高效。但是,对于具有数十万个或更多成员的大型集合,在运行 SMEMBERS
命令时应谨慎。此命令为 O(n),并在单个响应中返回整个集合。作为替代方案,请考虑 SSCAN
,它允许您迭代地检索集合的所有成员。
替代方案
大型数据集(或流式数据)上的集合成员资格检查可能会使用大量内存。如果您担心内存使用情况并且不需要完全精确,请考虑使用 布隆过滤器或布谷鸟过滤器 作为集合的替代方案。
Redis 集合通常用作索引的一种。如果您需要索引和查询数据,请考虑使用 JSON 数据类型和 搜索和查询 功能。
了解更多
- Redis 集合解释 和 Redis 集合阐述 是两个简短但全面的视频解释器,涵盖了 Redis 集合。
- Redis 大学的 RU101 详细探讨了 Redis 集合。