SCARD

Introduction and Use Case(s)

The SCARD command in Redis is used to get the number of members in a set. This is particularly useful for determining the size of a set quickly and efficiently. Typical scenarios for using SCARD include checking the count of unique elements, verifying the presence of entries before performing operations, or monitoring the growth of collections over time.

Syntax

  1. SCARD key

Parameter Explanations

  • key: The name of the set whose cardinality (number of members) you want to retrieve.

Return Values

  • If the set exists, SCARD returns the total number of elements (integer) in the set.
  • If the set does not exist, SCARD returns 0.

Example outputs:

  • For a non-empty set: (integer) 4
  • For an empty or non-existent set: (integer) 0

Code Examples

  1. dragonfly> SADD myset "one"
  2. (integer) 1
  3. dragonfly> SADD myset "two"
  4. (integer) 1
  5. dragonfly> SADD myset "three"
  6. (integer) 1
  7. dragonfly> SADD myset "four"
  8. (integer) 1
  9. dragonfly> SCARD myset
  10. (integer) 4
  11. dragonfly> DEL myset
  12. (integer) 1
  13. dragonfly> SCARD myset
  14. (integer) 0

Best Practices

  • Regularly use SCARD to monitor the size of sets, especially when working with dynamically changing data, to avoid unexpected performance issues.
  • Utilize SCARD before performing operations that rely on a minimum or maximum number of set members to ensure the expected preconditions are met.

Common Mistakes

  • Using SCARD on non-set data types will result in an error. Always make sure the key refers to a set.

    1. dragonfly> SET not_a_set "value"
    2. OK
    3. dragonfly> SCARD not_a_set
    4. (error) WRONGTYPE Operation against a key holding the wrong kind of value

FAQs

What happens if I use SCARD on a non-existent key?

If SCARD is used on a non-existent key, it simply returns 0, as there are no members in the set.

Is SCARD an O(1) operation?

Yes, SCARD operates in constant time O(1), making it efficient regardless of the size of the set.