Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis scan count: How to force SCAN to return all keys matching a pattern?

Tags:

redis

I am trying to find out values stored in a list of keys which match a pattern from redis. I tried using SCAN so that later on i can use MGET to get all the values, The problem is:

SCAN 0 MATCH "foo:bar:*" COUNT 1000 

does not return any value whereas

SCAN 0 MATCH "foo:bar:*" COUNT 10000 

returns the desired keys. How do i force SCAN to look through all the existing keys? Do I have to look into lua for this?

like image 513
DarthSpeedious Avatar asked Oct 16 '15 09:10

DarthSpeedious


People also ask

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.

What is count in scan for Redis?

SCAN COUNT The count command allows you to modify how many keys the SCAN command will fetch per call. By default, the SCAN command fetches ten keys. However, we can modify this by setting the count command. In this example, we set the cursor to return 15 elements instead of the default 10.

How do I scan using Redis?

The Redis SCAN command is used in order to incrementally iterate over a collection of elements. SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.

Does scan block Redis?

So yes, SCAN is blocking, but it's usually not a big deal, and is only blocking when Redis is actually processing the command.


1 Answers

With the code below you will scan the 1000 first object from cursor 0

SCAN 0 MATCH "foo:bar:*" COUNT 1000  

In result, you will get a new cursor to recall

SCAN YOUR_NEW_CURSOR MATCH "foo:bar:*" COUNT 1000 

To scan 1000 next object. Then when you increase COUNT from 1000 to 10000 and retrieve data you scan more keys then in your case match more keys.

To scan the entire list you need to recall SCAN until the cursor give in response return zero (i.e entire scan)

Use INFO command to get your amount of keys like

db0:keys=YOUR_AMOUNT_OF_KEYS,expires=0,avg_ttl=0

Then call

SCAN 0 MATCH "foo:bar:*" COUNT YOUR_AMOUNT_OF_KEYS 
like image 81
khanou Avatar answered Oct 05 '22 05:10

khanou