·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> PHP 使用redis

PHP 使用redis

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
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());}?>