Updated on 2024-03-04 GMT+08:00

Predis

Access a DCS Redis instance through Predis on an ECS in the same VPC. For more information about how to use other Redis clients, visit the Redis official website.

Prerequisites

  • A DCS Redis instance has been created and is in the Running state.
  • An ECS has been created. For details about how to create an ECS, see Purchasing an ECS.
  • If the ECS runs the Linux OS, ensure that the PHP compilation environment has been installed on the ECS.

Procedure

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

    For details, see Viewing Instance Details.

  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.