pecl install redis
php.ini:extension=redis.so
php -m | grep redis
Instance ID:Password format, and custom accounts use the Username@Password format. Instances with password-free authentication do not require a password.<?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();?>
<?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();?>
<?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.?>
<?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();?>
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 |
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários