Break the data matrix. Explore what Redis has to offer.
You’ll remember from chapter 1 that SETs hold unique items in an unordered fashion. You can quickly add, remove, and determine whether an item is in the SET. Among the many uses of SETs are storing who voted for an article and which articles belong to a specific group, as seen in chapter 1.
In this section, we’ll discuss some of the most frequently used commands that operate on SETs. You’ll learn about the standard operations for inserting, removing, and moving members between SETs, as well as commands to perform intersection, union, and differences on SETs. When finished with this section, you’ll be better prepared to fully understand how our search examples in chapter 7 work.
Let’s take a look at table 3.5 to see some of the more commonly used set commands.
|Command||Example use and description|
|SADD||SADD key-name item [item …] — Adds the items to the set and returns the number of items added that weren’t already present|
|SREM||SREM key-name item [item …] — Removes the items and returns the number of items that were removed|
|SISMEMBER||SISMEMBER key-name item — Returns whether the item is in the SET|
|SCARD||SCARD key-name — Returns the number of items in the SET|
|SMEMBERS||SMEMBERS key-name — Returns all of the items in the SET as a Python set|
|SRANDMEMBER||SRANDMEMBER key-name [count] — Returns one or more random items from the SET. When count is positive, Redis will return count distinct randomly chosen items, and when count is negative, Redis will return count randomly chosen items that may not be distinct.|
|SPOP||SPOP key-name — Removes and returns a random item from the SET|
|SMOVE||SMOVE source-key dest-key item — If the item is in the source, removes the item from the source and adds it to the destination, returning if the item was moved|
Some of those commands should be familiar from chapter 1, so let’s jump to the next listing to see some of these commands in action.
Using just these commands, we can keep track of unique events and items like we did in chapter 1 with voting and article groups. But the real power of SETs is in the commands that combine multiple SETs at the same time. Table 3.6 shows some of the ways that you can relate multiple SETs to each other.
|Command||Example use and description|
|SDIFF||SDIFF key-name [key-name …] — Returns the items in the first SET that weren’t in any of the other SETs (mathematical set difference operation)|
|SDIFFSTORE||SDIFFSTORE dest-key key-name [key-name …] — Stores at the dest-key the items in the first SET that weren’t in any of the other SETs (mathematical set difference operation)|
|SINTER||SINTER key-name [key-name …] — Returns the items that are in all of the SETs (mathematical set intersection operation)|
|SINTERSTORE||SINTERSTORE dest-key key-name [key-name …] — Stores at the dest-key the items that are in all of the SETs (mathematical set intersection operation)|
|SUNION||SUNION key-name [key-name …] — Returns the items that are in at least one of the SETs (mathematical set union operation)|
|SUNIONSTORE||SUNIONSTORE dest-key key-name [key-name …] — Stores at the dest-key the items that are in at least one of the SETs (mathematical set union operation)|
This group of commands are three fundamental SET operations, with both “return the result” and “store the result” versions. Let’s see a sample of what these commands are able to do.
If you’re comparing with Python sets, Redis SETs offer many of the same semantics and functionality, but are available remotely to potentially many clients. We’ll dig more deeply into what SETs are capable of in chapter 7, where we build a type of search engine with them.
Coming up next, we’ll talk about commands that manipulate HASHes, which allow us to group related keys and values together for easy fetching and updating.