php整合redis,PHP多进程开发与Redis结合实践

业务逻辑介绍:

用户在 APP 上发帖子,然后存储到 Redis 的 List 列表中

利用 Linux 的 crontab 定时任务功能,按秒请求执行PHP脚本文件(processNewsRedisList.php)

调用 redis_process 处理API,进行存储到 Mysql 中

1.发帖子API

public function post_json() {

$image = $_FILES['image'];

$data = I();

$images = $this->post_upload($image);

$data['image'] = $images ? $images : '';

if( count($data) ){

$redis = new Redis();

$data['creation_time'] = time();

//把发过来的帖子存储redis

$result = $redis->lpush('news_list', json_encode($data));

// $redis->lpush('news_list', json_encode($data));

// while (TRUE){

// if ($redis->lsize('news_list') > 0){

// $info = $redis->rpop('news_list');

// $info = json_decode($info, true);

// $result = $this->_model->news_publish( $info );

// }else {

// sleep(1);break;

// }

// }

// $redis->close();

$data['creation_time'] = time();

// $result = $this->_model->news_publish( $data );

if( $result < 1 ){//redis方式

// if( !$result ){//model方式

$this->_data['data'] = '';

$this->_data['error'] = true;

$this->_data['message'] = L('_DATABASE_ERROR_');

}else{//success

$this->_data['data'] = $result;

$this->_data['error'] = false;

}

}

else{

$this->_data['data'] = '';

$this->_data['error'] = true;

$this->_data['message'] = L('_Invalid_Parameters_');

}

$this->response($this->_data);

}

2.processNewsRedisList.php

/**

*检查队列中帖子,并把帖子插入数据库表中

*/

function worker()

{

//再次克隆出子进程

$pid = pcntl_fork();

if ( $pid == -1 ) {

exit('fork error');

}

if ( $pid == 0 ) {

$redis = new Redis();

$redis->connect('127.0.0.1', 7200);

//业务逻辑代码

while(true) {//sleep(5);

if( $redis->lsize('news_list') > 0 ){

$info = $redis->rpop('news_list');

$info = json_decode($info, TRUE);

//使用curl调用API接口

$uri = "http://*.*.*.*:*/api.zmartec/News/redis_process";

$data = $info;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

curl_close($ch);

} else {

sleep(1);//队列中没有任务的时候,睡眠1s,让出cpu给其它进程

}

}

$redis->close();

}

}

/**

*子进程,负责设置子进程为领导小组

*

*并调用处理函数,处理任务

*/

function children()

{

$sid = posix_setsid();

echo $sid;

for( $i = 0; $i < 2; $i++ ) {

worker();

}

}

//克隆子进程,返回子进程的进程id

$pid = pcntl_fork();

if ( $pid == -1 ) {

exit('fork error');

}

if ( $pid == 0 ) {

children();

}else{

exit('parent exit');

}

?>

3.存储帖子到 Mysql

thinkphp 3.2框架:

public function redis_process()

{

$data = $_POST;

if ($this->_model->news_publish($data)) {

return true;

} esle {

return false;

}

}

public function news_publish( $data )

{

try{

return M('news')->add($data);

}catch(Exception $ex){

return FALSE;

}

}

不足有误之处敬请指出,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值