Updated on 2024-10-28 GMT+08:00

Connecting to Redis on predis (PHP)

This section describes how to connect to Redis on predis. For more information about how to use other Redis clients, visit the Redis official website.

The following operations are based on an example of accessing a Redis instance on a client on an elastic cloud server (ECS).

Prerequisites

  • A Redis instance is created, and is in the Running state. To create a Redis instance, see Buying a DCS Redis Instance.
  • An ECS has been created. For details about how to create an ECS, see Purchasing a Custom ECS
  • If the ECS runs the Linux OS, ensure that the PHP compilation environment has been installed on the ECS.
  • The client and the Redis instance must be interconnected before connecting to the instance. For details, see Network Conditions for Accessing DCS Redis.

Connecting to Redis on predis

  1. View the IP address/domain name and port number of the DCS Redis instance to be accessed.

    For details, see Viewing and Modifying Basic Settings of a DCS Instance.

  2. Log in to the ECS.
  3. Install the PHP development package and CLI tool. Run the following yum command:

    yum install php-devel php-common php-cli

  4. After the installation is complete, check the version number to ensure that the installation is successful.

    php --version

  5. Download the Predis package to the /usr/share/php directory.

    1. Run the following command to download the Predis source file:
      wget https://github.com/predis/predis/archive/refs/tags/v2.2.2.tar.gz

      This version is used as an example. To download Predis clients of other versions, visit the Redis or PHP official website.

    2. Run the following commands to decompress the source Predis package:
      tar -zxvf predis-2.2.2.tar.gz
    3. Rename the decompressed Predis directory predis and move it to /usr/share/php/.
      mv predis-2.2.2 predis

  6. Edit a file used to connect to Redis.

    • Example of using redis.php to connect to a single-node, master/standby, or Proxy Cluster DCS Redis instance:
      <?php
          require 'predis/autoload.php';
          Predis\Autoloader::register();
          $client = new Predis\Client([
            'scheme' => 'tcp' ,
            'host'     => '{redis_instance_address}' ,
            'port'     =>{port} ,
            'password' => '{password}' 
          ]);
          $client->set('foo', 'bar');
          $value = $client->get('foo');
          echo $value;
      ?>
    • Example code for using redis-cluster.php to connect to Redis Cluster:
      <?php
         require 'predis/autoload.php';
              $servers = array(
               'tcp://{redis_instance_address}:{port}' 
              );
             $options = array('cluster' => 'redis');
             $client = new Predis\Client($servers, $options);
             $client->set('foo', 'bar');
             $value = $client->get('foo');
             echo $value;
      ?>

    {redis_instance_address} indicates the actual IP address/domain name of the DCS instance and {port} is the actual port number of DCS instance. For details about how to obtain the IP address/domain name and port, see 1. Change them as required. {password} indicates the password used to log in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. If password-free access is required, delete the line that contains "password".

  7. Run the php redis.php command to access the DCS instance.