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' ?>
More of Predis
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How can I troubleshoot a "PHP Redis went away" error?
- How can I use PHP and Redis to set a time-to-live (TTL) value?
- How can I use the zscan command in PHP with Redis?
- How can I set a timeout for a Redis connection using PHP?
- How do I use the PHP Redis zrevrange command?
- How can I use PHP and Redis to get a reverse range of scores?
- How can I use Redis with the Yii PHP framework?
- How do I install PHP, Redis, and XAMPP?
See more codes...