Redis – Sets

  • Post author:
  • Post category:Redis
  • Post comments:0 Comments

Redis Sets are an unordered collection of unique strings. Unique means sets does not allow repetition of data in a key.

In Redis set add, remove, and test for the existence of members in O(1) (constant time regardless of the number of elements contained inside the Set). The maximum length of a list is 232 – 1 elements (4294967295, more than 4 billion of elements per set).

Example

redis 127.0.0.1:6379> SADD Adglob redis 
(integer) 1 
redis 127.0.0.1:6379> SADD Adglob mongodb 
(integer) 1 
redis 127.0.0.1:6379> SADD Adglob mysql 
(integer) 1 
redis 127.0.0.1:6379> SADD Adglob mysql 
(integer) 0 
redis 127.0.0.1:6379> SMEMBERS Adglob  
1) "mysql" 
2) "mongodb" 
3) "redis"

In the above example, three values are inserted in Redis set named ‘Adglob’ by the command SADD.

Redis Sets Commands

Following table lists some basic commands related to sets.

Sr.NoCommand & Description
1SADD keyhttps://adglob.in/blog/redis-set-sadd-command/
Adds one or more members to a set
2SCARD keyhttps://adglob.in/blog/redis-set-scard-command/
Gets the number of members in a set
3SDIFF https://adglob.in/blog/redis-set-sdiff-command/
Subtracts multiple sets
4SDIFFSTORE https://adglob.in/blog/redis-set-sdiffstore-command/
Subtracts multiple sets and stores the resulting set in a key
5SINTER key1 https://adglob.in/blog/redis-set-sinter-command/
Intersects multiple sets
6SINTERSTORE destination key1 https://adglob.in/blog/redis-set-sinterstore-command/
Intersects multiple sets and stores the resulting set in a key
7SISMEMBER key https://adglob.in/blog/redis-set-sismember-command/
Determines if a given value is a member of a set
8SMEMBERS keyhttps://adglob.in/blog/redis-set-sismember-command-2/
Gets all the members in a set
9SMOVE https://adglob.in/blog/redis-set-smove-command/
Moves a member from one set to another
10SPOP keyhttps://adglob.in/blog/redis-set-spop-command/
Removes and returns a random member from a set
11SRANDMEMBER key https://adglob.in/blog/redis-set-srandmember-command/
Gets one or multiple random members from a set
12SREM keyhttps://adglob.in/blog/redis-set-srem-command/
Removes one or more members from a set
13SUNIONhttps://adglob.in/blog/redis-set-sunion-command/
Adds multiple sets
14SUNIONSTORE https://adglob.in/blog/redis-set-sunionstore-command/
Adds multiple sets and stores the resulting set in a key
15SSCAN key https://adglob.in/blog/redis-set-sscan-command/
Incrementally iterates set elements

Leave a Reply