如何计算符合模式的键数?

如何找到具有匹配模式的所有键的计数。

例如,有两个键 abc:random-text-1abc:random-text-2。这里常见的模式是 abc:。这里的计数是2。

我怎样才能做到这一点在重排?

89351 次浏览

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.

If it's a one-time thing, you can use KEYS as described by x_maras, but you shouldn't use that in your code since KEYS will scan every key in the entire database each time it's called.

If you want to do it frequently, there is no "good" way exactly as you've written because it will always be fairly inefficient to scan every key (even using SCAN, since it would be doing the same thing as KEYS just in a safer manner).

However, if the patterns you need are known ahead of time, you can keep a set of every key that matches the pattern.

SET abc:random-text-1 "blah"
SADD patterns:abc abc:randomtext-1


SET abc:random-text-2 "more blah"
SADD patterns:abc abc:randomtext-2


SCARD patterns:abc
// (integer) 2


SORT patterns:abc BY nosort GET *
// 1) "blah"
// 2) "more blah"

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"

From here:

eval "return #redis.pcall('keys', 'abc:*')" 0

It's not O(1), but at least the count is done on the server side.

From the command line, redis-cli --scan --pattern 'abc:*' | wc -l