Contenido del Curso
Introduction to Redis
Introduction to Redis
Set
One of the key characteristics of a set is that it does not allow duplicate values. When you add a new element to a set, if the element already exists, it won't be added again.
Sets are commonly used for storing unique values, such as user IDs, IP addresses, or any other data where duplicates must be avoided.
Practical Use of Sets in Redis
Basic Commands for Working with Sets
Redis sets come with several commands that make it easy to add, remove, and check elements
Adding and Removing Elements
The SADD
command adds elements to a set, ignoring duplicates, and the SREM
command removes elements from a set.
Checking for an Element and Retrieving All Elements
To check if a specific element is in a set, use the SISMEMBER
command, which returns 1 if the element is present and 0 if it's not. To get all elements of the set, use the SMEMBERS
command.
Getting Information About the Set
To get the number of elements in a set, use the SCARD
command, which returns the number of elements in the set.
Operations with Multiple Sets
When working with multiple sets, you can use the commands SDIFF
, SINTER
, and SUNION
— SDIFF
returns elements that are in one set but not in the others, SINTER
finds common elements across all specified sets, and SUNION
returns the union of all elements from multiple sets.
1. What does the SADD
command do in Redis?
2. Which command should you use to retrieve all elements from a set?
¡Gracias por tus comentarios!