How to count the number of keys matching a pattern?

前端 未结 5 491
忘了有多久
忘了有多久 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

    By considering the performance, I would not recommend you use KEYS

    Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.

    I would suggest you considering scan, if your redis version > 2.8.0. But it rely on which data type you are going to use.

    Here is an simple example from redis doc:

    redis 127.0.0.1:6379> sadd myset 1 2 3 foo foobar feelsgood
    (integer) 6
    redis 127.0.0.1:6379> sscan myset 0 match f*
    1) "0"
    2) 1) "foo"
       2) "feelsgood"
       3) "foobar"
    

提交回复
热议问题