Mattia
Mattia

Reputation: 43

Is it possible to have two Predis connection to different databases of the same Redis instance?

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:

Do you have any idea why this happens?

Upvotes: 2

Views: 2425

Answers (2)

Mattia
Mattia

Reputation: 43

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!

Upvotes: 1

Niloct
Niloct

Reputation: 10015

Instantiate clients with new:

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

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

Upvotes: 1

Related Questions