2

I'm trying to create two Predis\Client instances on the same PHP script to separate data belonging to different logical domains.

I do this as follows:

$param1 = [
    'host'     => 'localhost',
    'port'     => 6379,
    'database' => 1,
];

$param2 = [
    'host'     => 'localhost',
    'port'     => 6379,
    'database' => 3,
];

[... some code ...]

$redis1 = new Predis\Client($param1);
$redis2 = new Predis\Client($param2);

Here is the problem:

  • $redis1 correctly stores data into database 1
  • $redis2 stores data into database 0 instead of 3

Do you have any idea why this happens?

Mattia
  • 43
  • 6

2 Answers2

1

Instantiate clients with new:

$redis1 = new Predis\Client([
    'host'     => 'localhost',
    'port'     => 6379,
    'database' => 1,
]);

$redis2 = new Predis\Client([
    'host'     => 'localhost',
    'port'     => 6379,
    'database' => 3,
]);
Niloct
  • 9,491
  • 3
  • 44
  • 57
1

I found the answer.

For some reason $param2 was erased to null elsewhere in the code.

Predis\Client doesn't fail but connect with default parameters!

Mattia
  • 43
  • 6
  • Yeah I set up a test here and went straight to the working outcome. Php issues errors without correct instantiation syntax, glad that you figured it out by yourself. – Niloct Mar 07 '17 at 03:10
  • 1
    Thanks @Niloct. In my opinion `Predis\Client` should fail. It seems weird to me that you can find yourself writing in the wrong database. – Mattia Mar 07 '17 at 03:15
  • I've only used phpredis back then because it is a compiled php extension and was faster. Have you tried it ? – Niloct Mar 07 '17 at 03:22