1

In Redis, I have keys in the following format :

25-1521624987
25-1521624000
25-1521624900
30-1521624900
30-1521624000
35-1521624100

I have written the following code to extract the Redis records with keys that begin with number 25.

 $tokens = $rdb->keys("^25-*");
 print_r($tokens);

But the result I get with the above code is an empty array.
Although the regex works in Redis Desktop Manager.

What is the correct way to get the required result in PHP redis?

Rishav Medhi
  • 13
  • 2
  • 4

1 Answers1

1

From the documentation (https://github.com/phpredis/phpredis#keys-getkeys)...

$allKeys = $redis->keys('*');   // all keys will match this.
$keyWithUserPrefix = $redis->keys('user*');

So with this I would have to assume it is NOT using regex expressions and you should use...

 $tokens = $rdb->keys("25-*");
 print_r($tokens);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks. Your above solution worked. I guess it's fault of the documentation. It should have mentioned that the pattern is not a regex expression. Having seen a * in the pattern made me thing it is a regex and I tried all types of regex to make it work. – Rishav Medhi Mar 22 '18 at 08:07
  • I think some systems build there own layer on top of redis to allow you to use regex, but I think they just read all the keys and then apply the regex locally. – Nigel Ren Mar 22 '18 at 08:12