Number of items in Redis set
Asked Answered
P

3

53

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...

Professorship answered 5/8, 2013 at 10:58 Comment(0)
A
82

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.

Astroid answered 5/8, 2013 at 11:3 Comment(1)
For errant seo: if you have a list (not a set) it's llen.Squally
A
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

Acred answered 29/6, 2020 at 8:30 Comment(0)
U
0

for python you can use scard.

lebgth=db.scard(key)
Unconcern answered 29/9, 2022 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.