Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com> Co-authored-by: Chen, Junjie <chenjunjie@huawei.com> Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
8.4 KiB
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 Elastic Cloud Server User Guide.
- If the ECS runs the Linux OS, ensure that the PHP compilation environment has been installed on the ECS.
Procedure
- View the IP address/domain name and port number of the DCS Redis instance to be accessed.
For details, see Viewing Details of a DCS Instance.
- Log in to the ECS.
- Install the PHP development package and CLI tool. Run the following yum command:
yum install php-devel php-common php-cli
- After the installation is complete, check the version number to ensure that the installation is successful.
php --version
- Download the Predis package to the /usr/share/php directory.
- Run the following command to download the Predis source file:
wget https://github.com/predis/predis/archive/refs/tags/v1.1.10.tar.gz
- Run the following commands to decompress the source Predis package:
- Rename the decompressed Predis directory predis and move it to /usr/share/php/.
mv predis-1.1.10 predis
- Run the following command to download the Predis source file:
- 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 or 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 the IP address/domain name and port 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".
- Example of using redis.php to connect to a single-node, master/standby, or Proxy Cluster DCS Redis instance:
- Run the php redis.php command to access the DCS instance.