Redis Compass (The Set Datastructure) –Part 7

Hi, we’ve really gone deep into redis datastrucures ranging from strings to lists to hash datastructures, in this section we’ll go a bit deep into the Set Datastructure.

 Set Datastructure

The set data-structure is a peculiar one in particular, it is an un-ordered collection of unique strings. It doesn’t support ordered retrieval in the sense that when the command to retrieve the values from a SET is executed, there is no binding condition to depict the order of which the values are to be returned, it could be returned in any order.  SET data-structure allows for various set theory operations such as intersection, union, difference, product e.t.c.  Let’s dig-in and play around with some of the various commands that redis provides us with to manipulate the SET data-structure.

SADD
In programming “speak terms”, the SADD command allows us to “initialize and populate” a SET.  It enables us to add both single and multiple elements to a SET.

SMEMBERS

Assuming we wanted to retrieve the values already contained in a SET structure, we simply use the SMEMBERS command.

SPOP

This is a fun command used when you want to pop (retrieve an element, at the same time deleting it) an element randomly from a SET.

SREM

As the name quite suggests, you use this when you want to remove a specific element from a set.  Take for example we wanted to remove the element “2” from our “cool” set, we simply do this:

SISMEMBER

This command checks if a value is present in a SET.  It returns “1” on the conclusion that the value exists else it returns “0” if the value doesn’t exist.

 

Conclusion

There are many more commands you can use to interact and manipulate the SET datastructure, to learn more the redis documentation is an excellent piece to look at.

Next we would take a look at ZSET.

2 comments on “Redis Compass (The Set Datastructure) –Part 7

Leave a Reply

Your email address will not be published. Required fields are marked *