<?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()); } ?>