在 https://www.runoob.com/redis/redis-install.html 能找到所有關於redis的信息,包括安裝、命令、在編程語言中的使用等等。這里就不講如何安裝redis了,因為在上面的網站中都能找到。下面直接講redis是如何在php中使用的,這里我選擇的是phpredis擴展。
1. 下載phpredis擴展
執行phpinfo()函數,根據下面截圖中的“NTS”和“VCn”選擇對應的壓縮包,http://pecl.php.net/package/redis。https://windows.php.net/downloads/pecl/releases/igbinary/,(igbinary) 另外注意,PHP版本也要對應好。
2. PHP配置安裝擴展
首先把壓縮包中的 php_igbinary.dll和php_redis.dll 文件放到PHP安裝目錄的 ext 目錄中
然后在 php.ini 添加如下配置
extension=php_igbinary.dll extension=php_redis.dll
3. 重啟apache,執行phpinfo()函數,會發現多了redis的擴展。
4. 開啟Redis服務,測試
$redis = new Redis(); //連接redis服務器 $redis->connect('127.0.0.1', '6379'); echo "Connection to server sucessfully <br/>"; //查看服務是否運行 echo "Server is running: " . $redis->ping();
結果如下,連接redis服務器成功
Connection to server sucessfully Server is running: +PONG