Redis的应用php
http://www.redis.cn/commands.htmlhtml
http://www.cnblogs.com/ikodota/archive/2012/03/05/php_redis_cn.htmlpython
http://redis.readthedocs.org/en/latest/index.htmlgit
安装redis及php-redis方法:github
1,安装redis,这个比较简单,直接yum安装便可web
yum install redis redis
service redis start 启动redissql
2,安装php-redis扩展数据库
这个有点复杂,须要用到phpize生成redis.so扩展,按照官方的文档vim
git clone https://github.com/nicolasff/phpredis.git
cd phpredis/
phpize
./configure
make && make install
使用phpize的时候须要用到php-devel开发包,可是咱们更新了php为最新版本的5.6,yum默认安装的包是5.3,会出现包冲突没法安装,可使用remi的源:
yum --enablerepo=remi,remi-php56 install php-devel
这样会更新php,安装phpize,再按上面的步骤运行
3,配置php
vim /etc/php.d/redis.ini 添加
extension=redis.so
service php-fpm restart 重启php-fpm便可
4,测试
编写测试php或者直接在phpinfo里面查看是否加载了redis
function MR() { $redis = new Redis(); $redis -> connect(C("REDIS_IP"), C("REDIS_PORT")); return $redis; } function test(){ $redis = MR(); $redis->set("test",1); $redis->get("test"); }
test();
5,应用场景
咱们开始使用redis是在一个点评系统,因为点评工做的巨大,须要极快速的响应,咱们没有使用Mysql,而是天天晚上将须要点评的数据存储到redis中,工做人员点评的数据读写彻底在redis中进行;而后在某个时间再进行数据组装,导入到Mysql;因为导入时使用的是批量导入,因此导入Mysql的速度也很是快;
因为redis是键值对的数据库,怎样将搜索条件存储到redis中,咱们利用有限的条件来组装成键(即SQL中产生的where),而后将相应项存储在redis的List中,使用lrange进行分页,基本上全部数据库操做均可以进行。
因为redis的响应很是快,因此很容易的知足了需求;
6,redis的监控
Redis-Live,redis-live使用的是python,须要另外开端口,仍是没用这个;