Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of keys matching a pattern?

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 isabc: . So, here the count is 2.

How can I do this in redis?

like image 870
shanks Avatar asked Dec 06 '13 07:12

shanks


People also ask

How to find total number of keys in Redis?

The first command you can use to get the total number of keys in a Redis database is the DBSIZE command. This simple command should return the total number of keys in a selected database as an integer value.

How do I get all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

How does Redis cursor work?

Redis SCAN SCAN is a cursor-based iteration command, allowing a client to go over all the elements in a table. This cursor-based scanner accepts an integer cursor on each call, and returns a batch of items and the cursor value to be used in the next call to SCAN.


1 Answers

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.

like image 138
warvariuc Avatar answered Oct 18 '22 16:10

warvariuc