9951 explained code solutions for 126 technologies


predisHow can I use Predis with a cluster in PHP?


Using Predis with a cluster in PHP is fairly straightforward. The following example code block shows how to connect to a cluster with Predis:

<?php
require 'predis/autoload.php';

$client = new Predis\Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379
]);
?>

The code above will connect to a single node cluster. To connect to a cluster, you need to provide an array of nodes to the $client object. For example:

<?php
require 'predis/autoload.php';

$client = new Predis\Client([
    'tcp://127.0.0.1:6379',
    'tcp://127.0.0.1:6380'
]);
?>

This will connect to a two node cluster. You can add more nodes to the array if needed.

Once you have connected to the cluster, you can use the $client object to perform operations on the cluster. For example, to set a key-value pair in the cluster:

<?php
$client->set('key', 'value');
?>

To retrieve the value of a key from the cluster:

<?php
$value = $client->get('key');
echo $value; // Outputs 'value'
?>

Helpful links

Edit this code on GitHub