运行前必备
说明:
若使用 TencentOS ,则可以使用
yum install php-pecl-redis
命令安装 phpredis。示例代码
<?php/*** Tencent Cloud Redis Connection and Operation Example* Documentation: https://github.com/phpredis/phpredis*/// Redis connection configuration$host = "192.xx.xx.26"; // Redis instance private IP$port = 6379; // Redis port$instanceid = "crs-xxxxxxxx"; // Instance ID$pwd = "your password"; // Instance password// Create Redis client instance$redis = new Redis();// 1. Connect to Redis serverif (!$redis->connect($host, $port)) {die("Connection failed: " . $redis->getLastError());}// 2. Authentication (Tencent Cloud format: instanceid:password)$authString = $instanceid . ":" . $pwd;if (!$redis->auth($authString)) {die("Authentication failed: " . $redis->getLastError());}// 3. Key-value operation example$key = "redis";$value = "tencent";// Set key-value pairif (!$redis->set($key, $value)) {die("Set key failed: " . $redis->getLastError());}echo "Successfully set key: $key, value: $value\\n";// Get value by key$result = $redis->get($key);if ($result === false) {die("Get key failed: " . $redis->getLastError());}echo "Retrieved key: $key, value: " . $result . "\\n";// 4. Close connection when done (optional)$redis->close();?>
运行结果
说明:
