How to count the number of keys matching a pattern?

node-redis, redis

Solution

DISCLAIMER I hope this old answer haven't damaged any production systems, with millions of keys. If you still want to still count the matching keys of redis in production for some reason, better use scan with a match pattern.

If you simply search with KEYS, with your redis client, you will get a number list of all you matching keys, right?

e.g.

KEYS abc:*

will give you

1) abc:random-text-1
2) abc:random-text-2

or you can run the following:

./redis-cli KEYS "abc:*" | wc -l

and you will get `2` as an output.

Problem

How can I find the count of all the keys that has a matching pattern. For example, there are two keys `abc:random-text-1` and `abc:random-text-2` . The common pattern here is`abc:` . So, here the count is 2. How can I do this in redis?

Original source

Related problems