How to count the number of keys matching a pattern?

前端 未结 5 490
忘了有多久
忘了有多久 2021-01-31 13:35

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 .

5条回答
  •  爱一瞬间的悲伤
    2021-01-31 13:58

    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.

提交回复
热议问题