PHP 使用redis简单示例分享
示例很简单,注释里也都做了说明,这里就不多废话了。
代码如下:
<?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()); } ?>
以上就是本文所述的代码的全部内容了,希望大家能够喜欢。
相关推荐
王道革 2020-11-25
wangdonghello 2020-11-03
Langeldep 2020-11-16
chenhualong0 2020-11-16
聚合室 2020-11-16
koushr 2020-11-12
MRFENGG 2020-11-11
guoyanga 2020-11-10
fackyou00 2020-11-10
Orangesss 2020-11-03
dongCSDN 2020-10-31
rainandtear 2020-10-30
Quietboy 2020-10-30
liuyulong 2020-10-29
fansili 2020-10-29
温攀峰 2020-10-23
jackbon 2020-10-19
kaixinfelix 2020-10-04