How do you check if a specific item exists in a Redis set, and how do you remove an item from it?
Short answer
You use SISMEMBER to check whether a specific value is currently a member of a set, returning a simple true or false style result, and SREM to remove one or more specific values from the set, leaving the rest of its contents completely unaffected.
Advertisement
AModel answer
You use SISMEMBER to check whether a specific value is currently a member of a set, returning a simple true or false style result, and SREM to remove one or more specific values from the set, leaving the rest of its contents completely unaffected.
Example
SISMEMBER post_tags:1 'redis' SREM post_tags:1 'caching'
Real-world example
A content management system checks whether a specific tag is already applied to a post using SISMEMBER before showing an add tag button, and removes a tag using SREM when an editor decides it no longer applies.
Likely follow-ups
- What is the time complexity of checking membership in a set?
- What happens if you try to remove an item that is not actually in the set?
Related topics