Search code examples
phprediskeykeystorehost

Select all key data for a specific pattern from Redis using PHP


I am using PHPRedis for this.

I need to create script that copies all of the keys with the pattern mobile* from one Redis host1 to host2.

I have got this working by selecting all keys from host1 with the pattern mobile*. Then looping over each of these keys using the get key method to return the data. I then set the key for host2 using the set method:

$auKeys = $redis->keys("mobile*");
foreach ($auKeys as $key) {
    $data = $redis->get($key);
    $redis2->set($key, $data, 6000);
    echo $key;
}

The problem is this takes around 5 minutes - I need to get it down to 2-3 minutes. Is there another way to do this?


Solution

  • The simplest route to SET you can take for a better performance is to PIPE the keys and hit the redis server once to execute all of them instead of a trip/key .

    https://github.com/phpredis/phpredis/issues/251

    $pipeline = $redis->multi($host, Redis::PIPELINE);
    
    //put result in our shared list
    foreach ($items as $item) {
      $pipeline->sAdd($key, $item);
    }
    
    $ret = $pipeline->exec();
    

    At the same time, there is also libraries out there if you are seeking a different way to trasnlate commands to Redis Protocol .

    redis bulk import using --pipe