Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

redis

set

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.

like image 697
Elijah Avatar asked Feb 15 '14 01:02

Elijah


People also ask

How do I find Redis size?

To get the size of a database in Redis, use the DBSIZE command. This returns the total number of keys stored in the currently selected database. The previous command returns the number of keys in the database at index 0. Another command you can use to get the database size is the info command.

How can I get sets in Redis?

The Redis “get set” (GETSET) command allows you to set a key to a specified value and return the old value that was stored at the key. The Redis GETSET command is often used in conjunction with the INCR command to handle counting.

What is Redis size?

Redis can handle up to 2^32 keys, and was tested in practice to handle at least 250 million keys per instance. Every hash, list, set, and sorted set, can hold 2^32 elements.

What is set in Redis?

Redis Sets are an unordered collection of unique strings. Unique means sets does not allow repetition of data in a key. In Redis set add, remove, and test for the existence of members in O(1) (constant time regardless of the number of elements contained inside the Set).


3 Answers

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.

like image 53
Tim Cooper Avatar answered Oct 18 '22 03:10

Tim Cooper


If it's a sorted set, you can use

ZCOUNT myset -inf +inf

or

ZCARD myset
like image 42
Donald Avatar answered Oct 18 '22 02:10

Donald


  • zCard is short for cardinality (cardinality is the number of elements in a set). It gives you total number of members inside of a "sorted set".

  • Sometimes you might wanna extract how many members are inside of a range in a sorted set. For that you can use zCount.

    ZCOUNT cars 0 50  // inclusive
    

this will include 0 and 55. 0 <= .... <=50. But if you do not want to include them

ZCOUNT cars (0 (50
  • if it is regular set

    SCARD cars 
    
like image 2
Yilmaz Avatar answered Oct 18 '22 03:10

Yilmaz