Introduction to Business logic:
- The user posts on the APP and then stores it in the Redis list
- Execute php script file (processnewsredislist.php) in seconds request with Linux crontab Timer task function
- Call the Redis_process processing API to store it in Mysql
1. Posting Sub 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 (); Store the posted posts for 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);//$re Sult = $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 mode//if (! $rEsult) {//model mode $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
<?php/*** Check the posts in the queue and insert the post into the database table */function Worker () {//clone the child process again $pid = Pcntl_fork (); if ($pid = =-1) {exit (' fork error '); } if ($pid = = 0) {$redis = new Redis (); $redis->connect (' 127.0.0.1 ', 7200); Business logic code while (true) {//sleep (5); if ($redis->lsize (' news_list ') > 0) {$info = $redis->rpop (' news_list '); $info = Json_decode ($info, TRUE); Use Curl to invoke API interface $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);//When there is no task in the queue, sleeping 1s, yielding cpu to other processes}} $redis->close (); }}/*** child process, responsible for setting child process to lead group * * and call handler function, processing Task */function children () {$sid = Posix_setsid (); Echo $sid; for ($i = 0; $i < 2; $i + +) {worker (); }}//clones the subprocess, returns the process of the subprocess Id$pid = Pcntl_fork (), if ($pid = =-1) {exit (' fork error ');} if ($pid = = 0) {children ();} Else{exit (' parent exit '); >
3. Store posts to Mysql
thinkphp 3.2框架:public function redis_process(){ $data = $_POST; $this->_model->news_publish($data); return true; }public function news_publish( $data ){ try{ return M(‘news‘)->add($data); }catch(Exception $ex){ return FALSE; } }
Please note, thank you for your lack of error.
"Four" PHP multi-process development and Redis integration practice