简体   繁体   中英

How to see key count of matching pattern in Azure Redis Cache Console

I want to just see the total number of keys available in Azure Redis cache that matches the given pattern. I tried the following command it is showing the count after displaying all the keys (which caused server load), But I need only the count.

>SCAN 0 COUNT 10000000 MATCH "{UID}*"

Except command SCAN , the command KEYS pattern can return the same result as your current command SCAN 0 COUNT 10000000 MATCH "{UID}*" .

However, for your real needs to get the number of keys matching a pattern, there is an issue add COUNT command from the Redis offical GitHub repo which had answered by the author antirez for you, as the content below.

Hi, KEYS is only intended for debugging since it is O(N) and performs a full keyspace scan. COUNT has the same problem but without the excuse of being useful for debugging... (since you can simply use redis-cli keys... | grep...). So feature not accepted. Thanks for your interest.

So you can not directly get the count of KEYS pattern , but there are some possible solutions for you.

  1. Count the keys return from command KEYS pattern in your programming language for the small number of keys with a pattern, like doing redis-cli KEYS "{UID}*" | wc -l redis-cli KEYS "{UID}*" | wc -l on the host server of Redis.

  2. To use the command EVAL script numkeys key \[key...\] arg \[arg...\] to run a Lua script to count the keys with pattern, there are two scripts you can try.

    2.1. Script 1

     return #redis.call("keys", "{UID}*")

    2.2. Script 2

     return table.getn(redis.call('keys', ARGV[1]))

    The completed command in redis-cli is EVAL "return table.getn(redis.call('keys', ARGV[1]))" 0 {UID}*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM