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");
执行之后查看服务器