EBOOK – REDIS IN ACTION

This book covers the use of Redis, an in-memory database/data structure server.

open all | close all

1.2.3 Sets in Redis

Figure 1.3An example of a SET with three items under the key, set-key

In Redis, SETs are similar to LISTs in that they’re a sequence of strings, but unlike LISTs, Redis SETs use a hash table to keep all strings unique (though there are no associated values). My visual representation of SETs will be similar to LISTs, and figure 1.3 shows an example SET with three items.

Because Redis SETs are unordered, we can’t push and pop items from the ends like we did with LISTs. Instead, we add and remove items by value with the SADD and SREM commands. We can also find out whether an item is in the SET quickly with SISMEMBER, or fetch the entire set with SMEMBERS (this can be slow for large SETs, so be careful). You can follow along with listing 1.3 in your Redis client console to get a feel for how SETs work, and table 1.5 describes the commands used here.

Table 1.5 Commands used on SET values

Command

What it does

SADD

Adds the item to the set

SMEMBERS

Returns the entire set of items

SISMEMBER

Checks if an item is in the set

SREM

Removes the item from the set, if it exists

Listing 1.3
The SADD, SMEMBERS, SISMEMBER, and SREM commands in Redis
redis 127.0.0.1:6379> sadd set-key item
(integer) 1
redis 127.0.0.1:6379> sadd set-key item2
(integer) 1
redis 127.0.0.1:6379> sadd set-key item3
(integer) 1
redis 127.0.0.1:6379> sadd set-key item
(integer) 0

When adding an item to a SET, Redis will return a 1 if the item is new to the set and 0 if it was already in the SET.

redis 127.0.0.1:6379> smembers set-key
1) "item"
2) "item2"
3) "item3"

We can fetch all of the items in the SET, which returns them as a sequence of items, which is turned into a Python set from Python.

redis 127.0.0.1:6379> sismember set-key item4
(integer) 0
redis 127.0.0.1:6379> sismember set-key item
(integer) 1

We can also ask Redis whether an item is in the SET, which turns into a Boolean in Python.

redis 127.0.0.1:6379> srem set-key item2
(integer) 1
redis 127.0.0.1:6379> srem set-key item2
(integer) 0

When we attempt to remove items, our commands return the number of items that were removed.

redis 127.0.0.1:6379> smembers set-key
1) "item"
2) "item3"
redis 127.0.0.1:6379>


As you can probably guess based on the STRING and LIST sections, SETs have many other uses beyond adding and removing items. Three commonly used operations with SETs include intersection, union, and difference (SINTER, SUNION, and SDIFF, respectively). We’ll get into more detail about SET commands in chapter 3, and over half of chapter 7 involves problems that can be solved almost entirely with Redis SETs. But let’s not get ahead of ourselves; we’ve still got two more structures to go. Keep reading to learn about Redis HASHes.