Reputation: 111
I can get some keys with this command:
keys *_products_*
However that command returns all the keys, where I just need the count of those. How can I get It?
Upvotes: 11
Views: 26706
Reputation: 1715
Use this command line:
redis-cli --scan --pattern '*_products_*' | wc -l
Upvotes: 1
Reputation: 11549
DBSIZE Return the number of keys in the database
http://redis.io/commands/dbsize
Upvotes: 13
Reputation: 50122
Both (evil) KEYS
and the much preferred SCAN
do not return counts, only key names. You could wrap them in a Lua script to just return the count.
However.
Doing KEYS
(or SCAN
) in real time is very expensive in terms of performance - it means that you're iterating over the entire keyspace. What you want to do is keep the result ready beforehand, and then just fetch it. Usually in such cases, you'd use a Redis Set in which you'd store each relevant key name - SADD to it whenever you create a key that matches the patter *products*
and delete from it whenever a key is removed.
But.
Since you're only interesting in a count, you can replace that Set with a simple counter.
Upvotes: 3
Reputation: 3794
You can use DBSIZE or INFO KEYSPACE
But if you want all the keys with a certain pattern in the name you need to use KEYS or SCAN And you need to pay attention to KEYS
, running it in production can affect the performance so it should be used with caution.
Upvotes: 11