51

What's the easiest way to getting the number (count) of items in Redis set? Preferably without the need to dump whole set and count the lines... So far, I have found only BITCOUNT, which I have not found that useful...

Pavel S.
  • 11,892
  • 18
  • 75
  • 113

3 Answers3

80

The SCARD command returns the cardinality (i.e. number of items) of a Redis set.

http://redis.io/commands/scard

There is a similar command (ZCARD) for sorted sets.

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
0

Also if you have a sorted set, you can use the ZCOUNT command to get the number of elements in the sorted set at key with a score between min and max.

Example:

ZCOUNT myzset 2 15

return count of elements with score >= 2 and <= 15

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
0

for python you can use scard.

lebgth=db.scard(key)
Hiren Namera
  • 390
  • 1
  • 10