阿里云PHP Redis代码示例

时间:2024-07-05 20:33:38

测试代码示例

<?php
/* 这里替换为连接的实例host和port */
$host = "localhost";
$port = 6379; /* 这里替换为实例id和实例password */
$user = "test_username";
$pwd = "test_password"; $redis = new Redis();
if ($redis->connect($host, $port) == false) {
die($redis->getLastError());
} /* user:password 拼接成AUTH的密码 */
if ($redis->auth($user . ":" . $pwd) == false) {
die($redis->getLastError());
} /* 认证后就可以进行数据库操作,详情文档参考https://github.com/phpredis/phpredis */
if ($redis->set("foo", "bar") == false) {
die($redis->getLastError());
} $value = $redis->get("foo");
echo $value; ?>