1、PHP客戶端
一、官方提供了幾款PHP客戶端,包括amphp/redis、phpredis、Predis、Rediska。推薦使用官方推薦的兩款客戶端,phpredis、Predis
二、phpredis是一個二進制版本的PHP客戶端,效率要比Predis高。這個版本支持做爲Session的Handler。這個擴展的優勢在於無需加載任何外部文件,使用比較方便。
三、Predis是一個靈活和特性完備(PHP>5.3)的支持Redis的PHP客戶端。優勢就是無須安裝PHP擴展包,直接加載使用便可,在保證PHP版本在5.3版本之上的狀況下,即便遷移服務器也不受影響。
四、缺點就是性能相較於phpredis較差。php
2、phpredis客戶端的安裝
一、phpredis的github地址:https://github.com/phpredis/phpredis
直接git clone https://github.com/phpredis/phpredis.git
二、cd phpredis
三、phpize
./configure [--enable-redis-igbinary] [--enable-redis-msgpack] [--enable-redis-lzf [--with-liblzf[=DIR]]]
make && make installnginx
官方對configure幾個參數的說明
If you would like phpredis to serialize your data using the igbinary library, run configure with --enable-redis-igbinary. If you would like to use the msgpack serializer, run configure with --enable-redis-msgpack (note: Requires php-msgpack >= 2.0.3) The extension also may compress data before sending it to Redis server, if you run configure with --enable-redis-lzf. If you want to use lzf library pre-installed into your system use --with-liblzf configuration option to specify the path where to search files. make install copies redis.so to an appropriate location, but you still need to enable the module in the PHP config file. To do so, either edit your php.ini or add a redis.ini file in /etc/php5/conf.d with the following contents: extension=redis.so.git
若是不使用參數能夠直接./configure
執行之後會報一個configure: error: Cannot find php-config. Please use --with-php-config=PATH錯誤,沒有找到php-config
因此須要使用./configure --with-php-config=PATH,其中PATH是你php-config命令所在的目錄github
四、看到以下圖所示信息說明擴展包文件已經搭好,而且放在/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/目錄下redis
五、須要將redis.so加載到php.ini中
vim /usr/local/php/etc/php.inivim
六、重啓php-fpm和nginx
lnmp php-fpm restart
lnmp nginx restart
七、使用php -m命令查看服務器
說明安裝成功app
八、打開phpinfophp-fpm
九、測試寫入數據性能
1)新建redisdemo.php
2)寫入以下代碼
$redis = new Redis(); $a = $redis->connect("主機IP",6379); var_dump($a); //打印結果:bool(true) 說明連接成功 $redis->set("test","test");
執行之後查看服務器