PHP7安装Redis扩展概述
本内容分享了在PHP7环境中部署Redis扩展的操作方案,供读者参考实践。
Linux平台安装步骤
Linux系统下PHP7的Redis扩展安装流程
1. 依次执行以下命令完成源码下载与解压
wget -c https://github.com/phpredis/phpredis/archive/php7.zip
unzip php7.zip
cd phpredis-php7
/YouPath/phpize
./configure --with-php-config=/YouPath/php-config
make
make install
2. 修改配置文件 php.ini
3. 重启 httpd 服务
4. 通过探针检查扩展状态
Windows平台安装步骤
Windows系统下PHP7的Redis扩展安装流程
此处提供适用于php5.3版本的redis php扩展压缩包(内含dll文件):https://github.com/nicolasff/phpredis/downloads
将解压后的dll文件移至php的ext目录,随后编辑php.ini,添加如下配置行:
extension=php_redis.dll
最后重启Apache服务器使配置生效
示例代码:
<?php
//获取投票的信息的ID
$aid = isset($_GET['aid']) ? ereg_replace("[^0-9]", "", $_GET['aid']) : 0;
//当前投票的数字,指的是在redis中的数据
$this_click_num = 0;
if($aid>2){
//设定写回的投票数的最大值,到了此值就写回mysql
$update_till_num = 50;
//创建redis对象
$r = new Redis();
$r->connect('127.0.0.1',6379);
//得到现在是第几个数据了
$this_click_num = $r->get('count_xin_newgame:'.$aid);
//点击数加1
$r->set('count_xin_newgame:'.$aid,$this_click_num+1);
if($this_click_num>=$update_till_num)
{
//如果点击数超过了设定数,那么就把数据写到mysql
if($this_click_num>$update_till_num)
require_once(dirname(__FILE__)."/db.php");
//更新数据库
$db->ExecuteNoneQuery(
"UPDATE `addonnewgame`
SET `game_num` = game_num + '{$update_till_num}'
WHERE `dede_addonnewgame`.`aid` ={$aid};"
);
//重置投票数目为0
$r->set('count_xin_newgame:'.$aid,0);
}
$r->setTimeout('count_xin_newgame:'.$aid,7*24*60*60);
exit($this_click_num);
}
?>
希望上述内容对您的PHP编程工作有所助益。