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 do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How do I use yum to install php-redis?
- How can I use PHP to increment values in Redis using ZINCRBY?
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How do I use PHP and Redis together to create a transaction?
- How can I install and configure Redis on an Ubuntu server running PHP?
- How can I configure a PHP application to use Redis with a specific timeout?
- How can I set a timeout for a Redis connection using PHP?
- How can I use Redis to rate limit requests in PHP?
See more codes...