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
More of Predis
- How can I use the zscan command in PHP with Redis?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How do I use the hset array command in PHP with Redis?
- How can I use PHP and Redis to get multiple keys?
- How do I use the PHP Redis zrevrange command?
- How do I use the rpush command in PHP with Redis?
- How do I use yum to install php-redis?
- How do I install PHP, Redis, and XAMPP?
- How do I install and configure a PHP Redis DLL on a Windows machine?
See more codes...