w3resource

Redis Sets: SINTERSTORE

Redis SINTERSTORE Command

Redis SINTERSTORE command is used to store the members of the set resulting from the intersection of all the specified sets in a specific key. Keys that do not exist are considered to be empty sets and if one of the keys being an empty set, the resulting set is also empty.

Here are some common uses and scenarios for the "SINTERSTORE" command:

  • Store Intersection Results: Calculate the intersection of multiple sets and store the result in a new set.
  • Persistent Common Elements: Save the common elements of specified sets for later use or reference.
  • Efficient Data Processing: Perform and store set intersection in a single command, optimizing data processing.
  • Data Analysis and Storage: Compare datasets and persist the common items for analysis or reporting.
  • Filtered Data Sets: Create new sets containing only the elements present in all specified sets.

Syntax:

SINTERSTORE DESTINATION_KEY KEY KEY1..KEYN

Available since:

1.0.0.

Return Value:

Integer replies the number of elements in the resulting set.

Return Value Type:

Integer

Example: Redis SINTERSTORE of two sets

mycolor1 = {R G B} 
mycolor2 = {G B Y} 
SINTERSTORE dest_key mycolor1 mycolor2 = {G B}

127.0.0.1:6379> SADD mycolor1 R G B
(integer) 3
127.0.0.1:6379> SADD mycolor2 G B Y
(integer) 3
127.0.0.1:6379> SINTERSTORE dest_key mycolor1 mycolor2
(integer) 2
127.0.0.1:6379> SMEMBERS dest_key
1) "G"
2) "B"

Example: Redis SINTERSTORE of three sets

mycolor1 = {R G B} 
mycolor2 = {G B Y} 
mycolor3 = {B W O} 
SINTERSTORE dest_key myset1 myset2 myset3 = {B} 

127.0.0.1:6379> SADD mycolor3 B W O
(integer) 3
127.0.0.1:6379> SINTERSTORE dest_key mycolor1 mycolor2 mycolor3
(integer) 1
127.0.0.1:6379> SMEMBERS dest_key
1) "B"

Previous: Redis Sets SINTER
Next: Redis Sets SISMEMBERS



Follow us on Facebook and Twitter for latest update.