How do I get the size of a set on Redis?

RedisSet

Redis Problem Overview


For lists I can do the operation:

LLEN KeyName

and it will return the size of a list in Redis. What is the equivalent command for sets? I can't seem to find this in any documentation.

Redis Solutions


Solution 1 - Redis

You are looking for the SCARD command:

> ## SCARD key > Returns the set cardinality (number of elements) of the set stored at > > Return value
Integer reply: the cardinality (number of elements) of the set, or 0 if key does not exist.

You can view all of the set commands on the documentation webpage.

Solution 2 - Redis

If it's a sorted set, you can use

ZCOUNT myset -inf +inf

or

ZCARD myset

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElijahView Question on Stackoverflow
Solution 1 - RedisTim CooperView Answer on Stackoverflow
Solution 2 - RedisDonaldView Answer on Stackoverflow