The example is very simple and has been explained in the comments, so there won’t be much nonsense here.
The code is as follows:
/*Get the database name from the platform*/
$dbname = "";
/*Get host, port, user, pwd from environment variables*/
$host = '';
$port = '';
$user = '';
$pwd = '';
try {
/*After establishing the connection, auth verification is required before performing the collection operation*/
$redis = new Redis();
$ret = $redis->connect($host, $port);
if ($ret === false) {
die($redis->getLastError());
}
$ret = $redis->auth($user . "-" . $pwd . "-" . $dbname);
if ($ret === false) {
die($redis->getLastError());
}
/*Then you can operate the library. For specific operation methods, please refer to the phpredis official documentation*/
$redis->flushdb();
$ret = $redis->set("key", "value");
if ($ret === false) {
die($redis->getLastError());
} else {
echo "OK".$redis->get("key");
}
} catch (RedisException $e) {
die("Uncaught exception " . $e->getMessage());
}
?>
The above is the entire content of the code described in this article, I hope you all like it.
http://www.bkjia.com/PHPjc/963977.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963977.htmlTechArticlePHP uses redis to share simple examples. This article mainly introduces PHP to use redis to share simple examples, mainly to show you. Next, how to use redis in PHP, friends in need can refer to it...