tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

PHP Connection Sample

Baixar
Modo Foco
Tamanho da Fonte
Última atualização: 2026-09-07 14:31:32
Traduzido por IA
This document provides an example of using a PHP client to connect to a Tencent Cloud Distributed Cache instance.

Preparations

Environment Requirement

PHP 7.0 or later.
The phpredis extension is installed (version 5.0 or later recommended).

Installing a client

Install the phpredis extension via PECL:
pecl install redis
After installation, add the following configuration to php.ini:
extension=redis.so
Verify the installation:
php -m | grep redis

Obtaining Connection Information

1. Log in to the Distributed Cache console.
2. In the instance list, click the target instance ID to go to the instance details page.
3. In the Network Information section, obtain the instance's VIP address and port (default is 6379).
4. In the Configuration Information section, obtain the connection password for the instance.
Default accounts use the Instance ID:Password format, and custom accounts use the Username@Password format. Instances with password-free authentication do not require a password.

Connection Example

In proxy mode, clients connect to an instance through the unified VIP address provided by the Proxy layer. Proxy automatically handles request routing and failover, so clients do not need to be aware of backend topology changes.

Basic Connection Example

<?php
// Connection parameters
$host = "192.xx.xx.2"; // Instance VIP address
$port = 6379; // Instance port
$password = "crs-xxxx:password"; // Default account password format: Instance ID:Password

// Create a connection.
$redis = new Redis();

// Connect to the instance and set the timeout to 2 seconds.
$redis->connect($host, $port, 2.0);

// Authentication
$redis->auth($password);

// Select a database (proxy mode supports multiple DBs).
$redis->select(0);

// Write data.
$redis->set("name", "Distributed Cache");

// Read data.
$value = $redis->get("name");
echo "get name: " . $value . "\\n";

// Set a Key with an expiration time.
$redis->setex("session:user123", 3600, "session_data");

// Hash operations
$redis->hSet("user:1001", "name", "Zhang San");
$redis->hSet("user:1001", "age", "28");
$userInfo = $redis->hGetAll("user:1001");
echo "user info: ";
print_r($userInfo);

// Close the connection.
$redis->close();
?>

Custom Account Connection Example

<?php
$host = "192.xx.xx.2";
$port = 6379;
$username = "myuser";
$password = "myuser@MyPassword"; // Proxy mode custom account: the entire "username@password" string is passed as the password.

$redis = new Redis();
$redis->connect($host, $port, 2.0);
$redis->auth($password);

$redis->set("key", "value");
echo $redis->get("key") . "\\n";

$redis->close();
?>

Connection Pool Example

In high-concurrency scenarios, use persistent connections (pconnect) to reuse TCP connections and reduce connection establishment overhead.
<?php
$host = "192.xx.xx.2";
$port = 6379;
$password = "crs-xxxx:password";

// Use a persistent connection. The fourth parameter is the persistent connection flag.
$redis = new Redis();
$redis->pconnect($host, $port, 2.0, "persistent_id_01");
$redis->auth($password);

// Set the read/write timeout.
$redis->setOption(Redis::OPT_READ_TIMEOUT, 2);

// Business operations
$redis->set("counter", 0);
$redis->incr("counter");
$count = $redis->get("counter");
echo "counter: " . $count . "\\n";

// Persistent connections do not need to be closed explicitly. They are managed by the PHP lifecycle.
?>

SSL Encryption Connection Example

<?php
$host = "192.xx.xx.2";
$port = 6379;
$password = "crs-xxxx:password";
$caCertPath = "/path/to/ca.pem"; // Path to the CA certificate file

$redis = new Redis();

// Configure TLS/SSL parameters.
$redis->connect("tls://" . $host, $port, 2.0, null, 0, 0, [
'stream' => [
'verify_peer' => true,
'verify_peer_name' => false,
'cafile' => $caCertPath,
]
]);

$redis->auth($password);

$redis->set("ssl_test", "encrypted_connection");
echo $redis->get("ssl_test") . "\\n";

$redis->close();
?>

Connection Parameter Description

Parameter
Description
Recommended Value
host
Instance connection address. In proxy mode, it is a VIP address; in direct connection mode, it is a shard node address.
-
port
Instance port number
6379
timeout
Connection timeout (s)
2.0
read_timeout
Read timeout (s)
2.0
persistent
Whether to use persistent connections
Enable it in high-concurrency scenarios.
password
Authentication password
Default account format: Instance ID:Password

FAQs

Connection Timeout

Confirm that the CVM where the client resides and the distributed cache database instance are in the same VPC.
Confirm that the security group rule allows access to the corresponding port.
Confirm that the connection address and port are correct.

Ajuda e Suporte

Esta página foi útil?

comentários