Home > Backend Development > PHP Tutorial > PHP uses redis

PHP uses redis

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-08 09:32:55
Original
1036 people have browsed it
<?php
/*从平台获取数据库名*/
$dbname = "";
 
/*从环境变量里取host,port,user,pwd*/
$host = '';
$port = '';
$user = '';
$pwd = '';
 
try {
    /*建立连接后,在进行集合操作前,需要先进行auth验证*/
    $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());
    }
 
    /*接下来就可以对该库进行操作了,具体操作方法请参考phpredis官方文档*/
    $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());
}
?>
Copy after login

 

The above introduces the use of redis in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
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
Latest Issues
php redis connection problem
From 1970-01-01 08:00:00
0
0
0
About a small error in the redis manual
From 1970-01-01 08:00:00
0
0
0
python2.7 - django cannot connect to redis
From 1970-01-01 08:00:00
0
0
0
I can't connect to redis using php
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template