Skip to content
Redis

Sets

Manage unique collections and compute intersections.

#set#unique#setops

Code

redis
# Add and remove members
SADD tags:post:1 "mongo" "db" "nosql"
SADD tags:post:2 "postgres" "db" "sql"
SREM tags:post:1 "nosql"

# Membership and size
SISMEMBER tags:post:1 "db"
SCARD tags:post:1
SMEMBERS tags:post:1

# Set operations
SINTER tags:post:1 tags:post:2     # common tags
SUNION tags:post:1 tags:post:2     # all tags
SDIFF  tags:post:1 tags:post:2     # in 1 but not 2

# Random member and pop
SRANDMEMBER tags:post:1
SPOP tags:post:1