高数据库io情况下 选用如下队列消费
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46public function __construct(){ $this->redis = new Redis(); $this->ip = $_SERVER["REMOTE_ADDR"]; } //Redis队列入队 public function redisRpush(){ $this->redis->connect('127.0.0.1',6379); $ipadd = $this->getCity($this->ip); $data = [ 'user_ip'=>$this->ip, 'user_address'=>$ipadd, 'user_wechat'=>$usertext, 'formurl'=>$formurl, 'username'=>$username, 'userphone'=>$userphone, 'create_time'=>date('Y-m-d h:i:s',time()) ]; // 转json存入redis $this->redis->rpush("myqueue",json_encode($data)); $this->redis->close(); } //Redis队列消费(定时任务执行) public function reisLpop(){ $this->redis->connect('127.0.0.1',6379); //获取当前队列有多少组数据 $lent = $this->redis->lLen('myqueue');//查询待消费数据总数 //循环遍历插入 for($i = 0; $i<$lent; $i++){ //拿出队列的json格式值 $value = $this->redis->lpop('myqueue'); if($value){ //执行插入当前数据 $data = json_decode($value);//json数据转数组存入sql $insert = new Userwx($data); $insert->save(); } } $this->redis->close(); }
然后定时任务执行消费接口即可
采用linux或者workerman的Timer类
最后
以上就是眯眯眼百合最近收集整理的关于Redis消息队列 发布与消费的全部内容,更多相关Redis消息队列内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复