一、安装php7.0 redis扩展
安装配置步骤如下:
1.root@ubuntu:/tmp# git clone -b php7 https://github.com/phpredis/phpredis.git
可能没有预先安装git,只需要按照提示安装即可。
2.root@ubuntu:/tmp# mv phpredis/ /etc/
3.root@ubuntu:/tmp# cd /etc/phpredis
4.root@ubuntu://etc/phpredis# phpize
5.root@ubuntu://etc/phpredis# ./configure
6.root@ubuntu://etc/phpredis#make && make install
7.root@ubuntu://etc/phpredis#vi /etc/php/7.0/fpm/conf.d/redis.ini 中 写入(extension=/etc/phpredis/modules/redis.so)退出保存。此操作需要先创建fpm/conf.d/文件夹。
8.root@ubuntu://etc/phpredis#vi /etc/php/7.0/apache2/php.ini 中写入 (extension=/etc/phpredis/modules/redis.so)
9. 重启apache2 /etc/init.d/apache2 restart
安装过程中可能出现其他提示,只要按着提示做就行。
最后建一个test.php
<?php
echo phpinfo();
?>
通过浏览器访问,如果出现以下图,图中含有redis的说明,则说明安装成功
二、使用php去访问redis
<?php
//连接本地Redis服务
$redis=new Redis();
$redis->connect('localhost','6379'); //$redis->auth('admin123');//如果设置了密码,添加此行
//查看服务是否运行
$redis->ping();
//选择数据库
$redis->select(5);
//设置数据
$redis->set('school','WuRuan');
//设置多个数据
$redis->mset(array('name'=>'jack','age'=>24,'height'=>'1.78'));
//存储数据到列表中
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
//获取存储数据并输出
echo $redis->get('school');
echo '<br/>';
$gets=$redis->mget(array('name','age','height'));
var_dump($gets);
echo '<br/>';
$tl=$redis->lrange("tutorial-list", 0 ,5);
var_dump($tl);
?>
运行效果如图:
参考文章: http://blog.csdn.net/rorntuck7/article/details/52652070
my github:https://github.com/lensh