Home >Backend Development >PHP Tutorial >PHP uses redis simple example sharing_PHP tutorial

PHP uses redis simple example sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:07814browse

Sharing a simple example of PHP using redis

This article mainly introduces the sharing of a simple example of PHP using redis. It is mainly to show you how to use redis in PHP. If necessary Friends, please refer to it.

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.

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn