Using the set data type
The set data type is a collection of unique and unordered objects. It is often used in applications for membership testing, duplicates removal, and math operations (union, intersection, and difference). Redis value objects can be string set. In this recipe, we will store restaurants' tags in a Redis set and demonstrate basic commands Redis provides to manipulate a set.
Getting ready…
You need to finish the installation of the Redis Server, as we described in the Downloading and installing Redis recipe in Chapter 1, Getting Started with Redis, and connect to the Redis Server by redis-cli
.
How to do it...
To understand how to use the Set data type, take the following steps:
- Open a Terminal and connect to Redis with
redis-cli
- To add tags for the
"Original Buffalo Wings"
restaurant, use the followingSADD
command:
127.0.0.1:6379> SADD "Original Buffalo Wings" "affordable" "spicy" "busy" "great taste"(integer) 4
- To test if an element is in the set, use
SISMEMBER
:
127.0.0.1:6379...