Introduction to Redis Sets
Commands for Sets in Redis
redis 127.0.0.1:6379> SADD marks 10
(integer) 1
redis 127.0.0.1:6379> sadd marks 20 30 0
(integer) 3
redis 127.0.0.1:6379> smembers marks
1 "0"
2 "10"
3 "20"
4 "30"
redis 127.0.0.1:6379> sadd marks 10
(integer) 0
redis 127.0.0.1:6379> smembers marks
1 "0"
2 "10"
3 "20"
4 "30"
redis 127.0.0.1:6379> smembers marks
1 "0"
2 "10"
3 "20"
4 "30"
redis 127.0.0.1:6379> del marks
(integer) 1
redis 127.0.0.1:6379> smembers marks
(empty list or set)
redis 127.0.0.1:6379> sadd subjects history geography tami;
(integer) 3
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
3 "history"
redis 127.0.0.1:6379> sismember subjects history
(integer) 1
redis 127.0.0.1:6379> sismember subjects english
(integer) 0
redis 127.0.0.1:6379> scard subjects
(integer) 3
redis 127.0.0.1:6379> spop subjects
"history"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
2 "geography"
redis 127.0.0.1:6379> spop subjects
"geography"
redis 127.0.0.1:6379> smembers subjects
1 "tami;"
redis 127.0.0.1:6379> sadd sub1 eng maths phy
(integer) 3
redis 127.0.0.1:6379> sadd sub2 eng chem geo
(integer) 3
redis 127.0.0.1:6379> smembers sub1
1 "phy"
2 "maths"
3 "eng"
redis 127.0.0.1:6379> smembers sub2
1 "geo"
2 "chem"
3 "eng"
redis 127.0.0.1:6379> sunion sub1 sub2
1 "maths"
2 "phy"
3 "eng"
4 "geo"
5 "chem"
redis 127.0.0.1:6379> sinter sub1 sub2
1 "eng"
redis 127.0.0.1:6379> sdiff sub1 sub2
1 "maths"
2 "phy"
redis 127.0.0.1:6379> smove sub1 sub2 phy
(integer) 1
redis 127.0.0.1:6379> smembers sub2
1 "geo"
2 "phy"
3 "chem"
4 "eng"
redis 127.0.0.1:6379> smembers sub1
1 "maths"
2 "eng"
redis 127.0.0.1:6379> smove sub2 sub1 phy
(integer) 1
redis 127.0.0.1:6379> smembers sub1
1 "phy"
2 "maths"
3 "eng"
redis 127.0.0.1:6379> srandmember sub1
"phy"
redis 127.0.0.1:6379> srandmember sub1
"maths"
redis 127.0.0.1:6379> srandmember sub1
"eng"
redis 127.0.0.1:6379> smembers sub1
1 "phy"
2 "maths"
3 "eng"
redis 127.0.0.1:6379> smembers sub2
1 "geo"
2 "chem"
3 "eng"
redis 127.0.0.1:6379> sinterstore sub1 sub2 sub3
(integer) 0
redis 127.0.0.1:6379> smembers sub3
(empty list or set)
redis 127.0.0.1:6379> smembers sub1
(empty list or set)
redis 127.0.0.1:6379> smembers sub2
1 "geo"
2 "chem"
3 "eng"
redis 127.0.0.1:6379> smembers sub3
(empty list or set)
redis 127.0.0.1:6379> sadd sub1 eng
(integer) 1
redis 127.0.0.1:6379> sadd sub1 maths
(integer) 1
redis 127.0.0.1:6379> sadd sub1 phys
(integer) 1
redis 127.0.0.1:6379> smembers sub1
1 "phys"
2 "maths"
3 "eng"
redis 127.0.0.1:6379> smembers sub2
1 "geo"
2 "chem"
3 "eng"
redis 127.0.0.1:6379> sinterstore result sub1 sub2
(integer) 1
redis 127.0.0.1:6379> smembers result
1 "eng"
redis 127.0.0.1:6379> sdiffstore result sub1 sub2
(integer) 2
redis 127.0.0.1:6379> smembers result
1 "phys"
2 "maths"
redis 127.0.0.1:6379> sunionstore result sub1 sub2
(integer) 5
redis 127.0.0.1:6379> smembers result
1 "phys"
2 "maths"
3 "eng"
4 "geo"
5 "chem"
redis 127.0.0.1:6379> del smembers
(integer) 0
redis 127.0.0.1:6379> del result
(integer) 1
redis 127.0.0.1:6379> smembers sub1
1 "phys"
2 "maths"
3 "eng"